Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Pong Help
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
funnyface43




PostPosted: 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
Sponsor
sponsor
Nathan4102




PostPosted: Sun Apr 07, 2013 12:10 pm   Post subject: RE:Pong Help

then you aren't redrawing everything in your loop :p
funnyface43




PostPosted: 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




PostPosted: Sun Apr 07, 2013 12:22 pm   Post subject: RE:Pong Help

Show me your code, I'll see what the problem is.
funnyface43




PostPosted: 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
Nathan4102




PostPosted: 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 Wink

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

%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)


    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
funnyface43




PostPosted: Sun Apr 07, 2013 12:54 pm   Post subject: RE:Pong Help

Thanks, it works a lot better and I will take your advice.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 22 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: