Please specify what version of Turing you are using
4.1.1
Sponsor Sponsor
Nathan4102
Posted: 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.
Edit: I'm pretty sure your assigning the balls position inside the loop aswell. Move those assignments outside and let me know if it worked. In the future, click "indent" at the top of turing when you come across a problem, it makes the code a lot easier to read.
Scratch that, it didn't fix the second problem. Give me a minute
: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
funnyface43
Posted: Sat Apr 06, 2013 6:23 pm Post subject: RE:Pong Help
Thanks, i will try putting it out of the loop
funnyface43
Posted: 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.
Nathan4102
Posted: 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?
CODE:
if ball_past_paddle AND ball_inbetween_top_and_bottom_of_paddle then
ball_change_direction
funnyface43
Posted: 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
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
Nathan4102
Posted: 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.
Posted: 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
%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
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
Sponsor Sponsor
Nathan4102
Posted: 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.
Raknarg
Posted: Sun Apr 07, 2013 10:20 am Post subject: RE:Pong Help
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.
funnyface43
Posted: Sun Apr 07, 2013 11:00 am Post subject: RE:Pong Help
So I have to erase everything, repeat everything but change the score?
funnyface43
Posted: Sun Apr 07, 2013 11:08 am Post subject: RE:Pong Help
or is it possible to just erase the score and replace it?
Nathan4102
Posted: 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.
funnyface43
Posted: 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?
Nathan4102
Posted: 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)