TGC Codebase Backup



Fully commented pong, great for learners by AluminumPork

22nd Nov 2003 23:53
Summary

A very simple 2 player pong game. Extremely commented and explained. I hope to help some beginners with this one.



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    REM /===============Very simple Pong game=================
	REM |-------------Written by Forrest Vodden---------------|
	REM |-----------------------------------------------------|
	REM |-Note: The speed of the ball will not change in this-|
	REM |---------------------example-------------------------|
	REM =====================================================/

	REM /-----------/Controls-------------
	REM |----Player 1----------------------|
	REM |-------=Up - "W"=-----------------|
	REM |-----=Down - "S"=-----------------|
	REM |----------------------------------|
	REM |----Player 2----------------------|
	REM |-------=Up - Up Key=--------------|
	REM |-----=Down - Down Key=------------|
	REM |----------------------------------|
	REM -----------Controls/-------------/


	REM -=-=-=-Draw a box (paddle) to the screen

BOX 1,1,10,50

	REM -=-=-=-Grab the box and place it into an image slot

GET image 1, 1,1,10,50

	REM -=-=-=-Clear the screen

CLS 

	REM -=-=-=-Draw a circle (ball) to the screen

CIRCLE 5,5,5

	REM -=-=-=-Grab the circle and place it into an image slot

GET image 2, 0,0,15,15

	REM -=-=-=-Clear the screen once again
CLS

	REM -=-=-=-We are now done creating our player objects, now we must
	REM -=-=-=-set up the variables needed for the player and the ball.

	REM -=-=-=-Setup the array for player 1 x and player 1 y
	REM -=-=-=-(the position of their paddles)

DIM player_x(2)
DIM player_y(2)

player_x(1) = 30 
player_x(2) = 600
player_y(1) = 240
player_y(2) = 240

	REM -=-=-=-Setup the array for player 1 score and player 2 score
	REM -=-=-=-(the amount of points for each player)

DIM player_score(2)

	REM -=-=-=-Set the Ball X and Ball Y to be in the middle of the screen

ball_x = 320
ball_y = 240

	REM -=-=-=-Set the speed of the ball to 2. Every the DO-LOOP loops the
	REM -=-=-=-ball will move in whatever direction in increments of 2 pixels.

ball_speed = 7

	REM -=-=-=-Set the direction variables

ballx_dir = 0
bally_dir = 0

	REM -=-=-=-Get a random number between 1 and 0 for each of the axis

random_direction_x = RND(1)
random_direction_y = RND(1)

ballx_dir = random_direction_x
bally_dir = random_direction_y

           REM -=-=-=-Set text to opaque so text erases itself when written over

SET TEXT OPAQUE

          REM -=-=-=-Print the words Player One score in the left, top corner
          REM -=-=-=-and print the words Player Two score in the right, top corner

TEXT 1,1, "Player One Score: "
TEXT 500,1, "Player Two Score: "

	REM -=-=-=-Start the game (DO-LOOP)

DO

	REM -=-=-=-Player Control

	REM -=-=-=-Keystate code (17) is the letter W
	REM -=-=-=-Keystate code (31) is the letter S	

	IF keystate(17) = 1 then player_y(1) = player_y(1) - 10
	IF keystate(31) = 1 then player_y(1) = player_y(1) + 10

	IF upkey() = 1 then player_y(2) = player_y(2) - 10
	IF downkey() = 1 then player_y(2) = player_y(2) + 10	



	REM -=-=-=-Check for directions, and move the ball in that direction.
	REM -=-=-=-A 0 on the X-Axis means move the ball left by ball_speed (2)
	REM -=-=-=-A 1 on the X-Axis means move the ball right by ball_speed (2)
	REM -=-=-=-A 0 on the Y-Axis means move the ball up by ball_speed (2)
	REM -=-=-=-A 1 on the Y-Axis means move the ball down by ball_speed (2)	


	IF ballx_dir = 0 then DEC ball_x, ball_speed
	
	IF ballx_dir = 1 then INC ball_x, ball_speed
	
	IF bally_dir = 0 then DEC ball_y, ball_speed
	
	IF bally_dir = 1 then INC ball_y, ball_speed
		

	REM -=-=-=-Check if the ball has his an edge of the screen and 
	REM -=-=-=-if so, switch it direction.

	REM -=-=-=-If ball goes off screen to left increase player two's score	
	REM -=-=-=-and set throw_ball to 1, in order to trigger the throw new
	REM -=-=-=-ball event. It also deletes the ball sprite.


	IF ball_x < 1 then player_score(2) = player_score(2) + 1:TEXT 500,1, "Player One Score: " + STR$(player_score(2)):throw_ball = 1:DELETE sprite 3
	
	REM -=-=-=-Use 635 instead of 640 because the width and height of
	REM -=-=-=-the ball is 5.
	
	REM -=-=-=-If ball goes off screen to right increase player one's score	
	REM -=-=-=-and set throw_ball to 1, in order to trigger the throw new
	REM -=-=-=-ball event. It also deletes the ball sprite.

	IF ball_x > 635 then player_score(1) = player_score(1) + 1:TEXT 1,1, "Player One Score: " + STR$(player_score(1)):throw_ball = 1: DELETE sprite 3

	IF ball_y < 1 then bally_dir = 1

	REM -=-=-=-Use 475 instead of 480 because the width and height of 
	REM -=-=-=-the ball is 5.

	IF ball_y > 475 then bally_dir = 0

	
	REM -=-=-=-Make sure either of the paddles don't go off the screen

	REM -=-=-=-Start a FOR/NEXT loop from 1 to 2 the check player 
	REM -=-=-=-positions for player 1 and 2

	FOR check_y_pos = 1 to 2
		IF player_y(check_y_pos) < 1 then player_y(check_y_pos) = 1

	REM -=-=-=-Use 430 instead of 480 because the height of the paddle
	REM -=-=-=-is 50 pixels.

		IF player_y(check_y_pos) > 430 then player_y(check_y_pos) = 430

	NEXT check_y_pos	

	REM -=-=-=-Finally we get to place the paddles and the ball on the 
	REM -=-=-=-screen as sprites.


	REM -=-=-=-Create a new sprite for the ball, and if the ball already 
	REM -=-=-=-exists them move it to its new location. We'll number
	REM -=-=-=-the ball sprite 1, although it could be any number.
	REM -=-=-=-The command gets IMAGE number 2 which we grabbed
	REM -=-=-=-earlier and places it into SPRITE 3.

	SPRITE 3, ball_x, ball_y, 2

	REM -=-=-=-Create new sprites for player 1 paddle and player 2 paddle.
	REM -=-=-=-If the paddles already exist then simply move them to their
	REM -=-=-=-new locations. This command grabs from IMAGE 1 that we got
	REM -=-=-=-earlier and places it into SPRITE 1 and 2.

	SPRITE 1, player_x(1), player_y(1), 1
	SPRITE 2, player_x(2), player_y(2), 1


	REM -=-=-=-Now it's time to check if a ball has bounced off a paddle.
	REM -=-=-=-For this we use the command SPRITE HIT. It's pretty 
	REM -=-=-=-straight forward.

	
	REM -=-=-=-Do a FOR/NEXT from 1 to 2 in order to check collisions
	REM -=-=-=-against player 1 and player 2.

	FOR check_collision = 1 to 2
		
	REM -=-=-=-Check if SPRITE check_collision (1 or 2) has hit SPRITE 3
	REM (the ball). The SPRITE HIT command is simple.

		IF SPRITE HIT(check_collision, 3) = 1
			
	REM -=-=-=-Fire the ball in the opposite direction of the player it hit.

			IF check_collision = 1 then ballx_dir = 1
			IF check_collision = 2 then ballx_dir = 0
		ENDIF
	NEXT check_collision

	REM -=-=-=-The throw new ball event.

	IF throw_ball = 1

	REM -=-=-=-Pause for 1 second to give the player a chance to see
	REM -=-=-=-what is going on.

	REM -=-=-=-Every 1000 units represent 1 second

	SLEEP 1000	
	
	REM -=-=-=-Set ball_x to middle of screen
	REM -=-=-=-Set ball_y to middle of screen	

		ball_x = 320
		ball_y = 240
	
	REM -=-=-=-Set movement on the X-Axis for the ball, either right or left
	REM -=-=-=-Set movement on the Y-Axis for the ball, either right or left		

		ballx_dir = RND(1)
		bally_dir = RND(1)

	REM -=-=-=-Set throw_ball to 0 so not trigger it again until needed
		throw_ball = 0
	ENDIF		


SYNC
LOOP