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 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
funnyface43




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


Turing:


%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
loop
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
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(5)
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
end loop




Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Nathan4102




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

Scratch that, it didn't fix the second problem. Give me a minute Wink

: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




PostPosted: Sat Apr 06, 2013 6:23 pm   Post subject: RE:Pong Help

Thanks, i will try putting it out of the loop
funnyface43




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




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




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




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




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




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




PostPosted: Sun Apr 07, 2013 10:20 am   Post subject: RE:Pong Help

Your game should be structured like this:

Turing:

loop
     take_in_inputs()
     calculations()
     draw_background()
     draw_stuff()
     View.Update
     cls
end loop


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




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




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




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




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




PostPosted: 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.
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 1 of 2  [ 22 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: