Posted: 23rd Nov 2003 6:48
This is a quick example of a super simple multiplayer pong game. It is fully commented and would be great for anyone wanting to learn about simple sprite movement, arrays, for/next and other simple ideas. Just thought I'd put it out there!

+ Code Snippet
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 -=-=-=-Start the game (DO-LOOP)
SET TEXT OPAQUE

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

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
Posted: 23rd Nov 2003 19:40
Nice code you have there alluminumPork.

Only one problem I had. Whenever a player scored, it would only flash their score, so you weren't able to see it.

Luckily, I've fixed it.

-added sync rate 60
-added 2 print commands to display player_score(1) and player_score(2)
-added a little bar at the top to(hopefully) make the interface look nicer.
-Changed
IF ball_y < 0 then bally_dir = 1
to
IF ball_y < 15 then bally_dir = 1
-Version edited by yellow on 7th line

+ Code Snippet
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 =====================================================/
                   `Version edited by Yellow
    

   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 Sets the screen refresh rate. Also keeps things moving at a more consistant rate.
sync on : sync rate 60
backdrop on : color backdrop rgb(0,0,0)

`Adds a plain for the scoreboard
make object plain 452,1000,10 : position object 452,0,118,200:lock object on 452

   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 = 3

   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 &lt; 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 prints the player score on the screen
   set cursor 500,0
   print "player 2 Score"+str$(player_score(2))
   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 &gt; 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

   Rem prints the players score on the screen
   set cursor 0,0
   print "player 1 Score"+str$(player_score(1))

   IF ball_y &lt; 15 then bally_dir = 1

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

   IF ball_y &gt; 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) &lt; 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) &gt; 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


Really did a nice job, and hopefully this will point new programmers in the right direction.

kudos.
8)

[edit] I ran this in DBP and it worked fine.
Posted: 24th Nov 2003 1:31
I ran this in DBP and it worked fine.

Yah, same here
Posted: 7th Dec 2003 1:03
This is one of those posts that should be stickied here, or moved to the Newcomer's section and stickied there.

*bump*


Posted: 7th Dec 2003 1:30
I don't understand this part
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
Posted: 9th Dec 2003 2:56
Commando300:
It is creating a image right inside the program for the paddle, instead of importing a all ready drawn image.
Posted: 9th Dec 2003 19:16
but he use the same box coordanates to put it in an image slot?
Posted: 11th Dec 2003 0:05
get image image_number, x1,y1,x2,y2

get image grabs an image from the screen within the coordinates specified. They have to match the BOX's coordinates if you want to capture it into an image.
Posted: 13th Dec 2003 12:34
How would u put a background in this code??

Whot piece of code wqould u need 2 adjust?

Any1 know?
Posted: 13th Dec 2003 12:34
Also havin difficulty changing the images that r used 2 hit the ball?
Posted: 3rd Jan 2004 13:59
to put a backround on, you can load an image at the start of the code, and then paste it right before all teh drawing commands in the main do loop, very simple

"BOX 1,1,10,50

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

GET image 1, 1,1,10,50"

that puts a box onto the screen, then grabs the image to an image slot. you can replace "box 1,1,10,50" with "paste image x,1,1" where image x is an image you made and loaded, that way you can make a paddle in paint