Pong game help
Author |
Message |
nightfeng2
|
Posted: Fri Jan 07, 2011 11:37 pm Post subject: Pong game help |
|
|
What is it you are trying to achieve?
Have a smooth paddle for pong game
What is the problem you are having?
I made a pong game with 1 paddle and 1 ball, but then paddle's movement is so slow, when i press up, the paddle moves a pixel up, delays and then move up continuously with lag.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
setscreen ("graphics:640;480,nocursor")
var ckey : string (1)
var x1, y1, x2, y2 : int %variables for left paddle
var ballx, bally, subx, suby : int %for ball
subx := - 5
suby := 5
ballx := 320
bally := 240
%%%%%%%%%%%%%%%%%%left paddle
x1 := 10
y1 := 180
x2 := 30
y2 := 300
drawfillbox (x1, y1, x2, y2, 1)
%%%%%%%%%%%ball
process ball
loop
drawfilloval (ballx, bally, 5, 5, 4)
delay (25)
drawfilloval (ballx, bally, 5, 5, 0)
if ballx = 10 then
subx := 5
elsif bally = 475 then
suby := - 5
elsif ballx = 635 then
subx := - 5
elsif bally = 5 then
suby := 5
elsif bally <= 0 then
suby := 5
elsif bally >= y1 and bally <= y2 and ballx <= x2 + 8 then
subx := 5
end if
ballx := ballx + subx
bally := bally + suby
end loop
end ball
fork ball
%%%%%%%%%%%%%%%%%%%leftpaddle movement
process leftpad
loop
getch (ckey )
if ckey = chr (119) then
drawfillbox (x1, y1, x2, y2, 0)
y1 := y1 + 10
y2 := y2 + 10
drawfillbox (x1, y1, x2, y2, 1)
elsif ckey = chr (115) then
drawfillbox (x1, y1, x2, y2, 0)
y1 := y1 - 10
y2 := y2 - 10
drawfillbox (x1, y1, x2, y2, 1)
end if
end loop
end leftpad
fork leftpad
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: Sat Jan 08, 2011 12:26 am Post subject: RE:Pong game help |
|
|
I suspect that the lag is from the use of forks. If you avoid getting blocked on input with the use of either hasch or Input.KeyDown, then you can use the same loop for both the ball and the paddles, and remove forks. That would make things much easier in the future. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
nightfeng2
|
Posted: Sun Jan 09, 2011 4:24 pm Post subject: Re: Pong game help |
|
|
I have no idea how to get rid of the lag, if i put them in 1 loop, the ball only moves when the paddle moves.
heres another version:.
setscreen ("graphics:640;480,nocursor")
var ckey : string (1)
var x1, y1, x2, y2 : int %variables for left paddle
var x3, y3, x4, y4 : int %variables for right paddle
var ballx, bally, subx, suby : int %for ball
subx := -5
suby := 5
ballx := 320
bally := 240
%%%%%%%%%%%%%%%%%%left paddle
x1 := 10
y1 := 180
x2 := 30
y2 := 300
drawfillbox (x1, y1, x2, y2, 1)
%%%%%%%%%%%ball
process ball
loop
drawfilloval (ballx, bally, 5, 5, 4)
delay (25)
drawfilloval (ballx, bally, 5, 5, 0)
if ballx = 10 then
subx := 5
elsif bally = 475 then
suby := -5
elsif ballx = 635 then
subx := -5
elsif bally = 5 then
suby := 5
elsif bally <= 0 then
suby := 5
elsif bally >= y1 and bally <= y2 and ballx <= x2 + 8 then
subx := 5
end if
ballx := ballx + subx
bally := bally + suby
end loop
end ball
fork ball
%%%%%%%%%%%%%%%%%%%leftpaddle movement
loop
getch (ckey)
if ckey = chr (119) then
drawfillbox (x1, y1, x2, y2, 0)
y1 := y1 + 10
y2 := y2 + 10
drawfillbox (x1, y1, x2, y2, 1)
elsif ckey = chr (115) then
drawfillbox (x1, y1, x2, y2, 0)
y1 := y1 - 10
y2 := y2 - 10
drawfillbox (x1, y1, x2, y2, 1)
end if
end loop |
|
|
|
|
 |
nightfeng2
|
Posted: Sun Jan 09, 2011 4:30 pm Post subject: RE:Pong game help |
|
|
found a solution, i used input keydown instead of getch |
|
|
|
|
 |
|
|