Posted: 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.
Sponsor Sponsor
Nathan4102
Posted: Sun Apr 07, 2013 12:10 pm Post subject: RE:Pong Help
then you aren't redrawing everything in your loop :p
funnyface43
Posted: 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.
Nathan4102
Posted: Sun Apr 07, 2013 12:22 pm Post subject: RE:Pong Help
Show me your code, I'll see what the problem is.
funnyface43
Posted: 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
%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)
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)
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
Nathan4102
Posted: 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.
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
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
%Background
drawfillbox (0, 0, maxx, maxy, black)
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
funnyface43
Posted: Sun Apr 07, 2013 12:54 pm Post subject: RE:Pong Help
Thanks, it works a lot better and I will take your advice.