Computer Science Canada Pong Help |
Author: | funnyface43 [ Sat Apr 06, 2013 4:44 pm ] | ||
Post subject: | Pong Help | ||
What is it you are trying to achieve? I am trying to get the ball to bounce off the paddle and fix the paddles because they stopped moving. What is the problem you are having? I can't get the paddles to move and get the ball to bounce off the paddles. Describe what you have tried to solve this problem I have tried moving some codes around and searching through other codes but i still can't get it. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Please specify what version of Turing you are using 4.1.1 |
Author: | Nathan4102 [ Sat Apr 06, 2013 4:59 pm ] |
Post subject: | RE:Pong Help |
Just quickly glancing at your code, the paddle isn't moving because your assigning its position INSIDE the loop. You need to move the assignments outside. Give me a second and Ill find your second issue. ![]() ![]() :facepalm: I thought you made code for the second problem, but it didn't work. :/ To get balls to bounce off your paddle, you need to check if the ball is both touching or behind the paddle x-wise, and in between the top and bottom of the paddle y-wise. This isn't hard since you have the exact position of all objects at all time. Good luck! Nathan |
Author: | funnyface43 [ Sat Apr 06, 2013 6:23 pm ] |
Post subject: | RE:Pong Help |
Thanks, i will try putting it out of the loop |
Author: | funnyface43 [ Sat Apr 06, 2013 6:30 pm ] |
Post subject: | RE:Pong Help |
it did work thanks, still trying to get the ball to bounce off the paddle though. if anyone can help i would really appreciate it. |
Author: | Nathan4102 [ Sat Apr 06, 2013 7:04 pm ] | ||
Post subject: | RE:Pong Help | ||
Did you read what I said? I'm not gonna give you the exact answer, you should be able to get it yourself. You know the exact x and y co-ordinates of the ball and both paddles, so wouldn't there be a way to check if the ball has passed the paddle?
|
Author: | funnyface43 [ Sat Apr 06, 2013 7:25 pm ] |
Post subject: | RE:Pong Help |
yes sorry, i did get it but i need to know if there is any improvements i can make to it and how to get a score int eh same font and size as the top part. Here is my new code: %Variables var intfont:int var y1,y2,y3,y4:int var x1,x2,x3,x4:int var x:int:=maxx div 2 var y:int:=maxy div 2-25 var r1,r2:int:=2 var dx,dy:int:=1 %Background drawfillbox(0,0,maxx,maxy,black) %Score drawbox(0,maxy-50,maxx,maxy,white) drawline(maxx div 2,maxy-50,maxx div 2,maxy,white) intfont:=Font.New("Segoe Print:18") Font.Draw("Player 1",5,maxy-25,intfont,white) intfont:=Font.New("Segoe Print:18") Font.Draw("Player 2",maxx-95,maxy-25,intfont,white) %Center Line for count:1..374 by 10 drawdot(maxx div 2,count,white) end for %Paddles and Ball y1:=150 y2:=200 y3:=150 y4:=200 x1:=5 x2:=10 x3:=maxx-5 x4:=maxx-10 var chars:array char of boolean loop View.Set ("offscreenonly") Input.KeyDown (chars) if chars ('w') then y1:=y1+5 y2:=y2+5 if y1 >=maxy-100 then y1:=maxy-100 y2:=maxy-51 end if end if if chars ('s') then y1:=y1-5 y2:=y2-5 if y1 <=0 then y1:=0 y2:=50 end if end if if chars (KEY_UP_ARROW) then y3:=y3+5 y4:=y4+5 if y3 >=maxy-100 then y3:=maxy-100 y4:=maxy-51 end if end if if chars (KEY_DOWN_ARROW) then y3:=y3-5 y4:=y4-5 if y3 <=0 then y3:=0 y4:=50 end if end if drawfillbox(x1,y1,x2,y2,white) drawfillbox(x3,y3,x4,y4,white) drawfilloval(x,y,r1,r2,white) delay(7) View.Update drawfillbox(5,y1,10,y2,black) drawfillbox(maxx-5,y3,maxx-10,y4,black) drawfilloval(x,y,r1,r2,black) x:=x+dx y:=y+dy if (x-r1)=0 or (x+r1)=maxx then x:=maxx div 2 y:=maxy div 2-25 delay(1000) dx:=(-1)*dx elsif (y-r1)=0 or (y+r1)=maxy-51 then dy:=(-1)*dy end if if (x+r1) <= x2 then if (y+r2) >= y1 then if (y+r2) <= y2 then dx:=1 end if end if end if if (x+r1) >= x4 then if (y+r2) >= y3 then if (y+r2) <= y4 then dx:=-1 end if end if end if %Center Line for count:1..374 by 10 drawdot(maxx div 2,count,white) end for end loop |
Author: | Nathan4102 [ Sat Apr 06, 2013 8:38 pm ] |
Post subject: | RE:Pong Help |
Store the scores as integers, or in an integer array, then use intstr() to print them to the screen. http://compsci.ca/holtsoft/doc/intstr.html |
Author: | funnyface43 [ Sun Apr 07, 2013 5:40 am ] |
Post subject: | RE:Pong Help |
Thanks, that was really helpful and I was able to get a score but only if it is inside of the loop. But when I do put it inside the loop it just shows the numbers and then keeps adding the numbers on top. How can I fix this? Code: %Variables var intfont:int var y1,y2,y3,y4:int var x1,x2,x3,x4:int var x:int:=maxx div 2 var y:int:=maxy div 2-25 var r1,r2:int:=2 var dx,dy:int:=1 var score1,score2:int:=0 var finalscore1,finalscore2:string %Background drawfillbox(0,0,maxx,maxy,black) %Score drawbox(0,maxy-50,maxx,maxy,white) drawline(maxx div 2,maxy-50,maxx div 2,maxy,white) intfont:=Font.New("Segoe Print:18") Font.Draw("Player 1",5,maxy-32,intfont,white) intfont:=Font.New("Segoe Print:18") Font.Draw("Player 2",maxx-95,maxy-32,intfont,white) finalscore1:=intstr(score2) finalscore2:=intstr(score1) intfont:=Font.New("Segoe Print:18") Font.Draw(finalscore1,250,maxy-32,intfont,white) intfont:=Font.New("Segoe Print:18") Font.Draw(finalscore2,maxx-250,maxy-32,intfont,white) %Center Line for count:1..374 by 10 drawdot(maxx div 2,count,white) end for %Paddles and Ball y1:=150 y2:=200 y3:=150 y4:=200 x1:=5 x2:=10 x3:=maxx-5 x4:=maxx-10 var chars:array char of boolean loop View.Set ("offscreenonly") Input.KeyDown (chars) if chars ('w') then y1:=y1+5 y2:=y2+5 if y1 >=maxy-100 then y1:=maxy-100 y2:=maxy-51 end if end if if chars ('s') then y1:=y1-5 y2:=y2-5 if y1 <=0 then y1:=0 y2:=50 end if end if if chars (KEY_UP_ARROW) then y3:=y3+5 y4:=y4+5 if y3 >=maxy-100 then y3:=maxy-100 y4:=maxy-51 end if end if if chars (KEY_DOWN_ARROW) then y3:=y3-5 y4:=y4-5 if y3 <=0 then y3:=0 y4:=50 end if end if drawfillbox(x1,y1,x2,y2,white) drawfillbox(x3,y3,x4,y4,white) drawfilloval(x,y,r1,r2,white) delay(7) View.Update drawfillbox(x1,y1,x2,y2,black) drawfillbox(x3,y3,x4,y4,black) drawfilloval(x,y,r1,r2,black) x:=x+dx y:=y+dy if (x-r1)=0 then x:=maxx div 2 y:=maxy div 2-25 score2:=score2+1 delay(1000) dx:=(-1)*dx elsif (y-r1)=0 or (y+r1)=maxy-51 then dy:=(-1)*dy end if if (x+r1)=maxx then x:=maxx div 2 y:=maxy div 2-25 score1:=score1+1 delay(1000) dx:=(-1)*dx end if if (x-r1) <= x2 then if (y+r2) >= y1 then if (y+r2) <= y2 then dx:=1 end if end if end if if (x+r1) >= x4 then if (y+r2) >= y3 then if (y+r2) <= y4 then dx:=-1 end if end if end if %Center Line for count:1..374 by 10 drawdot(maxx div 2,count,white) end for end loop |
Author: | Nathan4102 [ Sun Apr 07, 2013 10:15 am ] |
Post subject: | RE:Pong Help |
You need to erase and redraw the score every time a point is scored. |
Author: | Raknarg [ Sun Apr 07, 2013 10:20 am ] | ||
Post subject: | RE:Pong Help | ||
Your game should be structured like this:
You should restructure all your drawing so that your game progresses like that. This is the simplest way to keep animations running smoothly and predictably. Not to mention it's a lot easier to read it that way. |
Author: | funnyface43 [ Sun Apr 07, 2013 11:00 am ] |
Post subject: | RE:Pong Help |
So I have to erase everything, repeat everything but change the score? |
Author: | funnyface43 [ Sun Apr 07, 2013 11:08 am ] |
Post subject: | RE:Pong Help |
or is it possible to just erase the score and replace it? |
Author: | Nathan4102 [ Sun Apr 07, 2013 11:11 am ] |
Post subject: | RE:Pong Help |
Only if a point was scored, yes. You clear the whole screen every frame, and re-draw everything every frame. |
Author: | funnyface43 [ Sun Apr 07, 2013 11:19 am ] |
Post subject: | RE:Pong Help |
but isn't it all in a loop so everything will constantly be cleared? |
Author: | Nathan4102 [ Sun Apr 07, 2013 11:35 am ] |
Post subject: | RE:Pong Help |
Yup, thats the point! It would go: Draw everything to buffer View.Update(draw buffer to screen) cls (erase buffer) and you would keep looping that. |
Author: | funnyface43 [ Sun Apr 07, 2013 11:47 am ] |
Post subject: | RE:Pong Help |
I just tried that but it just showed a white screen with the paddles and ball flashing. |
Author: | Nathan4102 [ Sun Apr 07, 2013 12:10 pm ] |
Post subject: | RE:Pong Help |
then you aren't redrawing everything in your loop :p |
Author: | funnyface43 [ Sun Apr 07, 2013 12:21 pm ] |
Post subject: | RE:Pong Help |
I tried clearing the screen after the ball hits the wall but now the paddles won't move and the ball and paddles are flashing or glitching or something. |
Author: | Nathan4102 [ Sun Apr 07, 2013 12:22 pm ] |
Post subject: | RE:Pong Help |
Show me your code, I'll see what the problem is. |
Author: | funnyface43 [ Sun Apr 07, 2013 12:45 pm ] |
Post subject: | Re: Pong Help |
I sort of fixed it but now only the paddles won't move. code: %Variables var intfont:int var y1,y2,y3,y4:int var x1,x2,x3,x4:int var x:int:=maxx div 2 var y:int:=maxy div 2-25 var r1,r2:int:=2 var dx,dy:int:=1 var score1,score2:int:=0 var finalscore1,finalscore2:string %Background drawfillbox(0,0,maxx,maxy,black) %Score drawbox(0,maxy-50,maxx,maxy,white) drawline(maxx div 2,maxy-50,maxx div 2,maxy,white) intfont:=Font.New("Segoe Print:18") Font.Draw("Player 1",5,maxy-32,intfont,white) intfont:=Font.New("Segoe Print:18") Font.Draw("Player 2",maxx-95,maxy-32,intfont,white) finalscore1:=intstr(score1) finalscore2:=intstr(score2) intfont:=Font.New("Segoe Print:18") Font.Draw(finalscore1,250,maxy-32,intfont,white) intfont:=Font.New("Segoe Print:18") Font.Draw(finalscore2,maxx-250,maxy-32,intfont,white) %Paddles and Ball y1:=150 y2:=200 y3:=150 y4:=200 x1:=5 x2:=10 x3:=maxx-5 x4:=maxx-10 var chars:array char of boolean loop View.Set ("offscreenonly") Input.KeyDown (chars) if chars ('w') then y1:=y1+5 y2:=y2+5 if y1 >=maxy-100 then y1:=maxy-100 y2:=maxy-51 end if end if if chars ('s') then y1:=y1-5 y2:=y2-5 if y1 <=0 then y1:=0 y2:=50 end if end if if chars (KEY_UP_ARROW) then y3:=y3+5 y4:=y4+5 if y3 >=maxy-100 then y3:=maxy-100 y4:=maxy-51 end if end if if chars (KEY_DOWN_ARROW) then y3:=y3-5 y4:=y4-5 if y3 <=0 then y3:=0 y4:=50 end if end if drawfillbox(x1,y1,x2,y2,white) drawfillbox(x3,y3,x4,y4,white) drawfilloval(x,y,r1,r2,white) delay(7) View.Update drawfillbox(x1,y1,x2,y2,black) drawfillbox(x3,y3,x4,y4,black) drawfilloval(x,y,r1,r2,black) x:=x+dx y:=y+dy if (x-r1)=0 then x:=maxx div 2 y:=maxy div 2-25 score2:=score2+1 delay(1000) View.Update cls dx:=(-1)*dx elsif (y-r1)=0 or (y+r1)=maxy-51 then dy:=(-1)*dy end if if (x+r1)=maxx then x:=maxx div 2 y:=maxy div 2-25 score1:=score1+1 delay(1000) View.Update cls dx:=(-1)*dx end if if (x-r1) <= x2 then if (y+r2) >= y1 then if (y+r2) <= y2 then dx:=1 end if end if end if if (x+r1) >= x4 then if (y+r2) >= y3 then if (y+r2) <= y4 then dx:=-1 end if end if end if %Center Line for count:1..374 by 10 drawdot(maxx div 2,count,white) end for %Background drawfillbox(0,0,maxx,maxy,black) %Score drawbox(0,maxy-50,maxx,maxy,white) drawline(maxx div 2,maxy-50,maxx div 2,maxy,white) intfont:=Font.New("Segoe Print:18") Font.Draw("Player 1",5,maxy-32,intfont,white) intfont:=Font.New("Segoe Print:18") Font.Draw("Player 2",maxx-95,maxy-32,intfont,white) finalscore1:=intstr(score1) finalscore2:=intstr(score2) intfont:=Font.New("Segoe Print:18") Font.Draw(finalscore1,250,maxy-32,intfont,white) intfont:=Font.New("Segoe Print:18") Font.Draw(finalscore2,maxx-250,maxy-32,intfont,white) %Paddles and Ball y1:=150 y2:=200 y3:=150 y4:=200 x1:=5 x2:=10 x3:=maxx-5 x4:=maxx-10 View.Set ("offscreenonly") Input.KeyDown (chars) if chars ('w') then y1:=y1+5 y2:=y2+5 if y1 >=maxy-100 then y1:=maxy-100 y2:=maxy-51 end if end if if chars ('s') then y1:=y1-5 y2:=y2-5 if y1 <=0 then y1:=0 y2:=50 end if end if if chars (KEY_UP_ARROW) then y3:=y3+5 y4:=y4+5 if y3 >=maxy-100 then y3:=maxy-100 y4:=maxy-51 end if end if if chars (KEY_DOWN_ARROW) then y3:=y3-5 y4:=y4-5 if y3 <=0 then y3:=0 y4:=50 end if end if x:=x+dx y:=y+dy if (x-r1)=0 then x:=maxx div 2 y:=maxy div 2-25 score2:=score2+1 delay(1000) View.Update cls dx:=(-1)*dx elsif (y-r1)=0 or (y+r1)=maxy-51 then dy:=(-1)*dy end if if (x+r1)=maxx then x:=maxx div 2 y:=maxy div 2-25 score1:=score1+1 delay(1000) View.Update cls dx:=(-1)*dx end if if (x-r1) <= x2 then if (y+r2) >= y1 then if (y+r2) <= y2 then dx:=1 end if end if end if if (x+r1) >= x4 then if (y+r2) >= y3 then if (y+r2) <= y4 then dx:=-1 end if end if end if %Center Line for count:1..374 by 10 drawdot(maxx div 2,count,white) end for end loop |
Author: | Nathan4102 [ Sun Apr 07, 2013 12:51 pm ] | ||
Post subject: | RE:Pong Help | ||
You had all your initial assignments inside the loop again, those go just outside of the loop, as they only need to be run once. I'd reccomend cleaning your code up a bit, name your variables more appropriately, and use code tags! Those would all make helping you a lot easier ![]() Also, you should only be "View.Update"ing and "cls"ing once per loop, while you are doing it every single time something moves. Try deleting those cls and view.updates, and instead, update the entire screen and all the movements at once.
|
Author: | funnyface43 [ Sun Apr 07, 2013 12:54 pm ] |
Post subject: | RE:Pong Help |
Thanks, it works a lot better and I will take your advice. |