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

Username:   Password: 
 RegisterRegister   
 My pong game...some things I need help with
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Pro Programmer!




PostPosted: Sat Mar 18, 2006 10:09 am   Post subject: My pong game...some things I need help with

Okay I got the collision all down. Its old school pong. No extra touch added.
code:

var chars : array char of boolean
var up1 : int := 50
var up2 : int := 50
var down1 : int := 1
var down2 : int := 1
var ballx, bally : int
var speed, speed1 : int := 1
var size : int := 5
ballx := Rand.Int (10, 100) %sets the x of the ball
bally := Rand.Int (10, 100) %sets the y of the ball
loop
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then % makes pong bar 2 move up if up arrow pressed
        up1 := up1 + 1
        down1 := down1 + 1
    end if
    if chars (KEY_DOWN_ARROW) then % makes pong bar 2 move down if down arrow pressed
        up1 := up1 - 1
        down1 := down1 - 1
    end if
    if chars ('s') then % makes pong bar 1 move if 's' is pressed
        up2 := up2 - 1
        down2 := down2 - 1
    end if
    if chars ('w') then % makes pong bar 1 move is 'w' is pressed
        up2 := up2 + 1
        down2 := down2 + 1
    end if
     if bally <= 0 then % checks to see if ball goes off bottom of screen
        speed := 1 % if it is this changes the direction
    end if
    if bally >= maxy then %checks to see if ball goes off top of screen
        speed := - 1 % if it does this changes the direction
    end if
    colourback (black) % makes background black
    drawfillbox (20, up2, 30, down2, white) % draws pong bar 1
    drawfillbox (600, up1, 610, down1, white) % draw pong bar 2
    delay (3)
    cls
    Draw.FillOval (ballx, bally, size, size, white) % draws ball
    ballx += speed1 % this makes ball move
    bally += speed
    if ballx = 600 and bally < up1 and bally > down1 then %checks to see if ball hits pong bar 2
    speed1 := -1 % if it does this changes the direction
    elsif ballx = 20 and bally < up2 and bally > down2 then % checks to see if ball hits pong bar 1
    speed1 := 1 % if it does this changes direction
    end if
    end loop



I have a few problems. My paddles can and will go off the screen. How do I make it so that it doesn't and can go only as far as maxx and 0. Now I want to start off my ball speed slower than I have it but after a certain amount of time I want it to get faster. I know how I would make it faster and all but how would I go about doing it after a certain amount of time...say every 15 seconds? Thats all my questions for now and thanx for the help!
Sponsor
Sponsor
Sponsor
sponsor
Pro Programmer!




PostPosted: Sat Mar 18, 2006 10:12 am   Post subject: (No subject)

Forgot to add one more thing. My ball always starts off in the same place how do I make it so that it starts a bit more random? I haven't finished and I will make a menu and all and make scores and winners and everything. I just got the hard part finished first!
Cervantes




PostPosted: Sat Mar 18, 2006 10:34 am   Post subject: (No subject)

Welcome.

Glad to see you used [code ][/code ] tags. Although, they aren't that important if your code isn't indented properly. Wink


  • To prevent the paddle from going off the screen:
    code:

    if paddle.x > maxx then
        paddle.x := maxx
    elsif paddle.x < 0 then
        paddle.x := 0
    end if

    Note that you will have to incorporate the width of the paddle into that.

    Alternatively, you could do something like this:
    code:

    paddle.x := max (0, paddle.x)
    paddle.x := min (paddle.x, maxx)

    Which can then be compressed to:
    code:

    paddle.x := min (max (0, paddle.x), maxx)


  • Use Time.Elapsed to determine when 15 seconds has passed.
    code:

    var current_time := 0
    loop
        if Time.Elapsed - current_time > 15000 then
            %speed up ball
            current_time := Time.Elapsed
        end if
    end loop

  • To get a "random" integer, use the Rand.Int function.
Pro Programmer!




PostPosted: Sat Mar 18, 2006 12:56 pm   Post subject: (No subject)

Thanx alot!
Pro Programmer!




PostPosted: Sat Mar 18, 2006 2:27 pm   Post subject: (No subject)

Wow ok I did all that and everything and I realized something...I have NO idea how the AI is going to work. I'm going to want difficulty levels ofcourse.
Cervantes




PostPosted: Sat Mar 18, 2006 2:49 pm   Post subject: (No subject)

The simplest AI for pong would be like this:

    If the ball is above the paddle, move the paddle up
    If the ball is below the paddle, move the paddle down


For levels, you can speed the paddle of the movement up, though beyond a certain point it becomes impossible to beat the computer.

A more advanced AI would be to try to predict where the ball will go (use some math), then move the paddle to that spot.
Pro Programmer!




PostPosted: Sun Mar 19, 2006 2:06 pm   Post subject: (No subject)

Ok I have 1 more question now (sorry lol). I did the AI like you said ceraventes but now its impossible to beat. It ALWAYS gets to it. It never missed...heres my new code

code:

var chars : array char of boolean
var up1 : int := 50
var up2 : int := 50
var down1 : int := 1
var down2 : int := 1
var ballx, bally : int
var speed, speed1 : int := 1
var size : int := 5
var RunningTime := 0
var team1, team2 : int := 0
var player : string
ballx := Rand.Int (10, 100) %sets the x of the ball
bally := Rand.Int (10, 500) %sets the y of the ball
put "1 player or 2 players? 1p for 1 player and 2p for 2 player"
get player
loop
    put "Team 1 = ", team1, " points"
    locate (1, 64)
    put "Team 2 = ", team2, " points"
    Draw.FillOval (ballx, bally, size, size, white)
    drawfillbox (600, up1, 610, down1, white)
    drawfillbox (20, up2, 30, down2, white)
    Input.KeyDown (chars)
    if player = "2p" then
        if chars (KEY_UP_ARROW) then % makes pong bar 2 move up if up arrow pressed
            up1 := up1 + 1
            down1 := down1 + 1
        elsif chars (KEY_DOWN_ARROW) then % makes pong bar 2 move down if down arrow pressed
            up1 := up1 - 1
            down1 := down1 - 1
        end if
    end if
    if chars ('s') then     % makes pong bar 1 move if 's' is pressed
        up2 := up2 - 1
        down2 := down2 - 1
    end if
    if chars ('w') then     % makes pong bar 1 move is 'w' is pressed
        up2 := up2 + 1
        down2 := down2 + 1
    end if
    if bally <= 0 then     % checks to see if ball goes off bottom of screen
        speed := 1     % if it is this changes the direction
    end if
    if bally >= maxy then     %checks to see if ball goes off top of screen
        speed := -1     % if it does this changes the direction
    end if
    if player = "1p" then
        if bally < up1 then
            down1 := down1 - 1
            up1 := up1 - 1
        elsif bally > down1 then
            down1 := down1 + 1
            up1 := up1 + 1
        end if
    end if
    if down1 <= 0 then     %This if statement prevents the paddles from going off the screen
        down1 := 0
        up1 := 49
    elsif up1 >= maxy then
        down1 := maxy - 49
        up1 := maxy
    end if
    if down2 <= 0 then
        down2 := 0
        up2 := 49
    elsif up2 >= maxy then
        down2 := maxy - 49
        up2 := maxy
    end if
    if ballx = 630 then     % This adds one point everytime the ball reaches a point and then resets the coordinates
        cls             % of the paddle and the ball
        colour (white)
        put "Team 1 scores!"
        team1 := team1 + 1
        delay (2000)
        up1 := 50
        up2 := 50
        down1 := 1
        down2 := 1
        ballx := Rand.Int (10, 100)
        bally := Rand.Int (10, 500)
    elsif ballx = 0 then
        cls
        colour (white)
        put "Team 2 scores!"
        team2 := team2 + 1
        delay (2000)
        up1 := 50
        up2 := 50
        down1 := 1
        down2 := 1
        ballx := Rand.Int (500, 600)
        bally := Rand.Int (10, 500)
    end if
    if team1 = 5 then
        cls
        colour (white)
        locate (23, 40)
        put "Team 1 wins!"
        delay (2000)
        exit
    elsif team2 = 5 then
        cls
        colour (white)
        locate (23, 40)
        put "Team 2 wins!"
        delay (2000)
        exit
    end if
    colourback (black)     % makes background black
    ballx += speed1     % this makes ball move
    bally += speed
    if ballx = 600 and bally <= up1 and bally >= down1 then     %checks to see if ball hits pong bar 2
        speed1 := -1     % if it does this changes the direction
    elsif ballx = 30 and bally <= up2 and bally >= down2 then     % checks to see if ball hits pong bar 1
        speed1 := 1     % if it does this changes direction
    end if
    delay (3)
    cls
end loop

Cervantes




PostPosted: Sun Mar 19, 2006 2:35 pm   Post subject: (No subject)

Try to impliment a little bit of randomness or poor decision or hesitation into the AI.

Pick a random number (Rand.Int), from 1 to 100, say. If it is a 1, move the paddle the wrong way. Therefore, 1% of the time the computer will move the wrong way.
Sponsor
Sponsor
Sponsor
sponsor
Bored




PostPosted: Sun Mar 19, 2006 3:12 pm   Post subject: (No subject)

The problem with the formentioned aproach is that the ai can look sorta shaky if you want it to be bad enough to beat. A better approach might be to have the ai paddle move slightly slower then the ball. I'd suggest using a real value for the speed and position so that you can fine tune the speed more accuratly and if your using a ball speed of say 1 the paddle could then move at like .75 and thus be slower.
Pro Programmer!




PostPosted: Mon Mar 20, 2006 1:36 am   Post subject: (No subject)

Yea I tried that bored. That way however, the paddle misses the ball 80% of the time. I will do what you said ceraventes and thanks for the enormous amount of help! I will make it more than 1% though. Anyways when I finish I will post it on applications!
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 1  [ 10 Posts ]
Jump to:   


Style:  
Search: