Jump to content
IGNORED

Pong, ball movement


Retro Lord
 Share

Recommended Posts

I'm having a bit of trouble with the ball movement in a pong game. I've set the ball movement up like this and it just gets stuck in the wall. There must be a better way?

 rem pushing the button starts the balls movement
 if a=0 && joy0fire then a=1
 rem ball going up and left
 if a=1 then ballx=ballx-1 : bally=bally-1
 rem ball going up and right
 if a=2 then ballx=ballx+1 : bally=bally-1

 rem ball going down and left
 if a=3 then ballx=ballx-1 : bally=bally+1
 rem ball going down and right
 if a=4 then ballx=ballx+1 : bally=bally+1

 if collision(ball,playfield) && a=1 then a=3
 if collision(ball,playfield) && a=2 then a=4
 if collision(ball,playfield) && a=3 then a=4
 if collision(ball,playfield) && a=4 then a=5

Entire code:

 set kernel_options no_blank_lines

 player1x=85
 player1y=80

 player0x=85
 player0y=70

 ballx=85 : bally=30



main

 playfield:
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 X..............................X
 X..............................X
 X..............................X
 X..............................X
 X..............................X
 X..............................X
 X..............................X
 X..............................X
 X..............................X
 X..............................X
end


 player0:
        %11111111
        %00000000
        %00000000
        %00000000
        %00000000
        %00000000
        %00000000
        %00000000
end

 player1:
        %11111111
        %00000000
        %00000000
        %00000000
        %00000000
        %00000000
        %00000000
        %00000000
end


 COLUPF=$0E
 COLUP0=$0E
 COLUP1=$0E
 NUSIZ0=$30
 NUSIZ1=$30

   ballheight = 2 : rem * Ball 4 pixels high.

   CTRLPF = $21 : rem * Ball 4 pixels wide.


 drawscreen

 if a=0 && joy0fire then a=1
 rem ball going up and left
 if a=1 then ballx=ballx-1 : bally=bally-1
 rem ball going up and right
 if a=2 then ballx=ballx+1 : bally=bally-1

 rem ball going down and left
 if a=3 then ballx=ballx-1 : bally=bally+1
 rem ball going down and right
 if a=4 then ballx=ballx+1 : bally=bally+1

 if collision(ball,playfield) && a=1 then a=3
 if collision(ball,playfield) && a=2 then a=4
 if collision(ball,playfield) && a=3 then a=4
 if collision(ball,playfield) && a=4 then a=5




 if joy0left then player0x=player0x-1
 if joy0right then player0x=player0x+1

 if joy1left then player1x=player1x-1
 if joy1right then player1x=player1x+1

 goto main
Link to comment
Share on other sites

Got it working with the playfield now but it's a bit wonky when it comes to hitting the paddles, any pointers or ideas?

 if a=0 && joy0fire then a=1
 rem ball going up and left
 if a=1 then ballx=ballx-1 : bally=bally-1
 rem ball going up and right
 if a=2 then ballx=ballx+1 : bally=bally-1

 rem ball going down and left
 if a=3 then ballx=ballx-1 : bally=bally+1
 rem ball going down and right
 if a=4 then ballx=ballx+1 : bally=bally+1

 if bally=10 && a=1 then a=3
 if bally=10 && a=2 then a=4

 if ballx<20 && a=3 then a=4
 if ballx<20 && a=1 then a=2

 if ballx>120 && a=2 then a=1
 if ballx>120 && a=4 then a=3

 if collision(ball,player0) && a=3 then a=2
 if collision(ball,player0) && a=4 then a=1

Link to comment
Share on other sites

Okay! That cleared a lot of things up for me, I got ball movement and hit detection up and running perfectly. But I have yet another question about scoring with 2 players.

 

I want 2 separate scores, one on the left side and one on the right side both starting at 0, and if respective player scores a point their score goes up. The only way I figured out how to do it is with sprite flicker and a counter, but that would take a little to much memory for my taste. Is there a better aproach to this?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...