7 x 10 dot matrix ball

lake

New Member
hey i am doing a dot matrix pong game i have programed the padddles to work well but i am strugling with doing the ball i was just wondering how should i start and if you could point me in the right direction thanks p.s i dont want a solution toit so it all st being writen as i want to learn how o do it my self i do know some programming but not the advanced codes so explain please many thanks again

Edited by - lakey009 on 21/06/2007 16:12:44
 

hippy

Technical Support
Staff member
That's not a simple request to make.

You need to know where the ball is in the playing area and which direction it is travelling in. You'll then need to calculate from that information where it will be next, and handle if it leaves the playing area or hits a paddle. That may mean indicating a win or loose of the game or setting the ball moving in a different direction.

I'd suggest imagining the playing area as a 7x10 matrix and the ball can be represented by two co-ordinates ( left-right, top-bottom ). The direction can be N, NE, E, SE, S, SW, W or NW.

You'll need a routine which can determine the next co-ordinates the ball will be at given its current co-ordinates and direction.

You'll need routines to determine if it has left the playing area or has hit a paddle. If it has hit a paddle you'll need a routine which changes direction, then you need to determine what its new co-ordinates will be.

In pseudo-code ...<code><pre><font size=2 face='Courier'>Do
Gosub MoveTheBall
If ball position IS off the playing area Then
Gosub HandleBallOffPlayingArea
Else
If ball hits paddle Then
ChangeBallsDirection
Gosub MoveTheBall
End If
Gosub DisplayBall
End If
Loop </font></pre></code>

Edited by - hippy on 21/06/2007 16:39:33
 

lake

New Member
right to start then we will say that there is no paddles and it is 5x5 square to keep it easy for now so all we need to do is get the ball moving around side to side and bouncing off the walls and after i know how to do that i should be able to do the rest myself do you have any websites that can help me with that programming i can program but im not sure what it is called i use the type that the pixace datasheets use or can you start me off many thanks

 

Rickharris

Senior Member
Start simple - Learn how to get the ball/spot/dot moving left to right and bouncing when it reaches the edge.

Moving:
the ball position in the 5 x 5 matrix is expressed as a cell number - ie a position in one of 5 horizontal cells. Start a loop at 1 and count to 5 print the ball position in the cell the loop points to each time through. this will move the ball left to right.

Check if reached the end.
If you know the start and size you can just execute the loop when the loop ends you are at the other side. Then repeat loop counting down to go back.

If you start somewhere other than the edge you need to do a check to see if your position number has reached 5 to tell if you are at the other side after every increment in the loop.

try working this out with graph paper and pencil so you can see the mechanism in action.
<code><pre><font size=2 face='Courier'>
For b0=1 to 5
print ball position at B0
wait 1
next B0
</font></pre></code>

or

<code><pre><font size=2 face='Courier'>
Main:
For B0=1 to 5
If B0=5 then changedirection
print ball position at B0
next B0

change direction:
for B0=5 to 1 step-1
If B0=1 then main
print ball position at B0
next B0
</font></pre></code>

Edited by - rickharris on 21/06/2007 17:45:13
 

lake

New Member
yes thanks alot that hsa helped loads so b0 will be at let say the left side and it moves along 1-5 so left to right along the outputs in the dot matrix i undersand that easy so how would i go about comparing the ball to a object or an other LED that is turned on would you do it using the inputs or the b0 by using the 1 and 5 values and and w0 1-5 an verticle values and compare them to the values that the object is thanks

Edited by - lakey009 on 21/06/2007 17:58:50
 

hippy

Technical Support
Staff member
If you use two variables to define the ball's left-right and up-down position, you can simply compare that with the position of the paddles or any other object using IF statements.

If you were using a 5x5 area, I'd define the left-right values as 0 to 6; 0 off the left of the board, 1 to 5 position on the board, 6 off the right of the board, and similar for up-down; 0 off bottom, 6 off top.

You can then add or subtract 1 so the ball moves left or right, up or down, or diagonally. You can choose which to do based upon the value held in another ball direction variable.

To tell if the ball is off the are or has hit something else, you then need to compare the balls co-ordinate variables with the value of what you are checking for. As with the ball, you need to also track where other objects are.

Finally you would urn those tracked objects into suitable images to display on your LED's. You aren't really comparing the LED's but the internal data which says which LED's are on.

If the centre of your left paddle up-down ( also 0-6 is on the far left ( left-right=1 ) and is three LEDE's wide ( one above and one below its centre LED ), you can tell the ball has hit the left paddle using something like ...<code><pre><font size=2 face='Courier'>If ball_LeftRight = 1 Then
If ball_UpDown &lt;= paddle_UpDown+1 And ball_Upown &gt;= paddleUpDown-1 Then
Gosub BallHas HitPaddle
End If
End If </font></pre></code>
 

lake

New Member
yes thanks alot i should beable to work somthing out from this thank you lots and lots for you help ^^ both of your helps

Edited by - lakey009 on 21/06/2007 18:28:15
 

lake

New Member
i ve just got the ball to work really well but now i am going to move on and check the balls postion to where it is on the paddle i am going to do this by checking to see if the ball is on the right row to bounce off the paddle then by checking the verticle value of the ball and compare it to the var center value of the paddle which i did good and now i need to compare it to see if it is going to hit the top led or the bottom of the 3 led paddle i was hoping that i could do something like


if w1= w0 then gosub changdirection
if w1= w0 + 1 then gosub topbonce
if w1= w0 - 1 then gosub bottombounce

but i get an error on the second and last lines is there anoter way to get around this. w1 is the value of the up and down and w0 is the center of the paddle many thanks
 

lake

New Member
i no i can use greater than and less than but i need it to be the diffence of 1 or -1 max and a less than would do everthing below which would mean it not working maybe could do a math sum like w1-w0 =1 then.. not sure anybody got any ideas

Edited by - lakey009 on 22/06/2007 21:40:52
 

hippy

Technical Support
Staff member
<i>if w1= w0 + 1 then </i>

Unfortunately with the PICAXE there's no way to do maths on either side of the comparison. You have to use a temporary variable -

wTemp = w0 + 1
If w1 = wTemp Then ...
 

boriz

Senior Member
Like this?

<code><pre><font size=2 face='Courier'>
pos=w0-w1+1
on pos gosub bottombounce,middlebounce,topbounce
</font></pre></code>

Not sure why you need to use word variables when bytes should do.


Edited by - boriz on 23/06/2007 04:54:33
 

Mike7081

New Member
Hi lakey009

I built and programmed a 5x7 pong game. It was surprisingly fun to play even on a small display.

To move the ball use 4 variables: x and y for the position of the ball and UpDown and LeftRight. The value of UpDown can only be 1 or -1. If the ball is moving up the value of UpDown will be 1. If moving down then -1. The value of LeftRight can only be 1, 0, or -1. If the ball is moving straight up or down the value of LeftRight will be 0. If moving right then 1 or -1 if moving left. Start the game with UpDown = 1 or 0 and LeftRight = 0.

If the ball hits one of the sidewalls or a corner of a paddle then LeftRight = -LeftRight to change direction. If the ball hits a paddle then UpDown = -UpDown. If the paddle is moving when the ball hits it add 1 to LeftRight if the paddle is moving right and LeftRight is not 1 or subtract 1 from LeftRight if the paddle is moving left and LeftRight is not -1.

To update the position of the ball x = x + LeftRight and y = y + UpDown.
 

Jeremy Leach

Senior Member
Yeah, that on..gosub looks right to me. Pos doesn't have to be a word variable. I'm not sure how you're using the others.

The way maths expressions work is that the calculation of the right hand side is done using word arithmetic, then the result is loaded into the variable on the left. If the result is going to be greater than a byte result you need to use a Word variable, if not a byte variable is fine.


Edited by - jeremy leach on 23/06/2007 08:00:34
 

lake

New Member
axeeffect yes i see that is a good idea so you mean to have like you moving ball
b0=0 to 9
and have the direction it counts in by using the updown value like
for B0=6 to 0 stepb9
b9 being -1 and when it hits the side of the paddle just start the leftright to equal 1 or -1
am i along the right tracks

thanks
 

hippy

Technical Support
Staff member
I think you need to forget about FOR loops moving the ball. Simply add or subtract one to the left-right / up-down position whenever it needs to step.

The FOR loops will pre-define where and how the ball tracks prior to its moving and to change direction means having to alter all the parameters of the FOR loop, and doing that within the FOR loop is going to cause quite a lot of confusion and make the code much more complex than it needs to be and will use more code space. It's almost guaranteed you will end up with the FOR-NEXT loop exiting under some condition or other when it shouldn't, and it's going to get harder and harder for people to understand how your code works if you need help in debugging any problems.

Consider this as a 'free-state' system, where the next state the game will be depends upon what the current state is and what forces are being applied ( current direction, collisions etc ). Using FOR-NEXT would be ideal for a fixed-state system where progression of states is pre-defined to start with. Trying to bash a fixed-state system into being a free-state system is a lot of work compared to starting with a free-state system.

The solution to paddle collision from Boriz looks good to me, and quite efficient. It can also be adapted to paddles over 3 pixels in length.
 

lake

New Member
yes thanks ive just been playing around with everones sugestion and that was was seems to be working im just trying to fit it into the whole program thanks
 

lake

New Member
its finshed wow bout time as well lol apart from one thing and that is adding a value on every so many milliseconds and slowly the pause getting smaller if any one can help my little problem the code is

ball:
high portc b0
high b1
let pins=%00000000
let pinsc=%00000000
let b0=b0+b2
let b1=b1+b4
if b1=6 then gosub negb4
if b1=0 then gosub posb4
if b0=7 then checkrightpad
if b0=1 then checkleftpad
gosub paddle


b2 needs to change as it is the rate the ball changes from led to led so it cannot just have a pause in the code as it will stop the dot matrix effect
 

hippy

Technical Support
Staff member
One really helpful technique is to give your variables meaningful names suing SYMBOL commands -<code><pre><font size=2 face='Courier'> SYMBOL ballLeftRight = b0
SYMBOL ballUpDown = b1 </font></pre></code> That way people can tell what your code is meant to be doing when they do not know what variables are being used and you haven't described what they are.

You also really need to give a more detailed description of what you are doing and how the code fits in with the overall architecture of your design.

While you do, I have no idea why you have those 'high portc', 'pins=' and 'pinsc=' commands in what looks to be a ball movement routine. It's very hard to suggest what you might need to do to achieve what you want, because we don't know how it works so cannot say how to alter things.

Your b2 can only be zero, one or greater and if you only want to move the ball every N times through the loop you'll need to use a counter so b2 is only added on when needed. You will still have to check for collisions because the paddles could be moving even though the ball is not.

One solution would be to split the ball and paddle movements out from the routines which actually draw the display. You would run the drawing routine every loop for maximum refresh rate and minimum flicker, and only call the ball and paddle move routines only every now and again.

You can change the speed at which the ball moves by altering b2, or alternatively you can make b2 a fixed value of 1 and alter the rate at which the ball move routine is called.
 

boriz

Senior Member
A common problem in game programming is how to time everything correctly so that all the moving elements move at the correct speed and there is rapid response to user input. Try something like this:

<code><pre><font size=2 face='Courier'>
max_user_time=10
max_ball_time=20
user_time=max_user_time
ball_time=max_ball_time
do
&#8216;USER TIMER SECTION
dec user_time
if user_time=0 then
user_time=max_user_time
gosub do_user_update
endif
&#8216;BALL TIMER SECTION
dec ball_time
if ball_time=0 then
ball_time=max_ball_time
gosub do_ball_update
endif
.
.
&lt;draw all objects&gt;
.
.
Loop
</font></pre></code>

Here the user controlled objects are updated twice as often as the ball object. Manipulate the relative timing to suit your purpose. And if you need any other differently timed objects (sound for example) just add another section and time variables.


Edited by - boriz on 23/06/2007 22:52:10
 
Top