Computer Science Canada

HELP! Editing code for Dodgeball game.

Author:  penny2199 [ Mon Jan 05, 2015 9:55 am ]
Post subject:  HELP! Editing code for Dodgeball game.

What is it you are trying to achieve?
<-get collision detection to work
-make player a circle>


What is the problem you are having?
<-player becomes a stretchable box/circle
-dodgeballs hit the player, and game doesn't end>


Turing:


%Dodgeball Game Project

View.Set ("graphics,nobuttonbar")
var playerpos, points, playerxpos, playerypos : int
var ballspeed, ballposition, ballradius : int
var gameover : int
var chars : array char of boolean
var anykey : string (1)

points := 0
playerxpos := 585
playerypos := 175
gameover := 0

%Home Screen
locate (11, 1)
put "" : 28, "Welcome to Alien Showdown"
put "" : 29, "Press any key to play!"
put ""
put "" : 34, "How to play:"
put "" : 18, "Use the arrow keys to move. Avoid the flying orbs!"
loop
    if hasch then
        getch (anykey)
        exit
    else
    end if
end loop
cls

put "Story:"

put "Square Town is under Attack by the Aliens! Weirdly shaped beings are"
put "destroying your town! Avoid the orbs the Aliens shoot."
put ""
put "Can you survive?"
put "Enter any key to start."
loop
    if hasch then
        getch (anykey)
        exit
    else
    end if
end loop
cls

loop

    %Draws original dodgeballs and determines position and size
    randint (ballradius, 20, 40)
    randint (ballposition, 1, 315)
    ballspeed := 0
    drawfilloval (maxx + 50, maxy - 50 - ballposition, ballradius, ballradius, 45)

    %Begins loop
    loop
        cls

        %Character commands
        Input.KeyDown (chars)

        %Moves character down
        if chars (KEY_DOWN_ARROW) then
            if playerypos > 0 then
                playerypos -= 2
            end if
        end if

        %Moves character up
        if chars (KEY_UP_ARROW) then
            if playerypos < maxy - 66 then
                playerypos += 2
            end if
        end if

        %Moves character up
        if chars (KEY_LEFT_ARROW) then
            if playerxpos > 0 then
                playerxpos -= 2
            end if
        end if

        %Moves character up
        if chars (KEY_RIGHT_ARROW) then
            if playerxpos < maxx - 50 then
                playerxpos += 2
            end if
        end if

        %Draws "character"
        drawfillbox (playerxpos, playerypos, playerxpos + 50, playerypos + 50, 21)

        %Draws dodgeballs
        drawfilloval (ballspeed, maxy - 50 - ballposition, ballradius, ballradius, 45)
        ballspeed := ballspeed + 5
        delay (7)
        exit when ballspeed = 700

        %Determines if a dodgeball hits the player
        if ballspeed = playerxpos and ballspeed <= playerxpos + 50 and ballposition >= playerypos and ballposition <= playerypos + 50 then
            gameover += 1
            exit
        end if

    end loop

    %Adds up points
    points := points + 1

    %Exits to Game Over screen when hit by a dodgeball
    if gameover > 0
            then
        exit
    end if

end loop
cls

put "Score: ", points, " "




Author:  Insectoid [ Mon Jan 05, 2015 10:34 am ]
Post subject:  RE:HELP! Editing code for Dodgeball game.

Your collision detection code makes no sense.

code:
ballspeed = playerxpos and ballspeed <= playerxpos + 50


Take a look at that and tell me what it means. I suggest you read more into a collision tutorial because I don't think you fully understand it yet.

Furthermore, your variable names could use some work. 'ballspeed' is the x coordinate of the ball, so why not just use 'ballX' or something similar? It does not represent speed. ballposition is not the ball's position, it's the ball's y coordinate.


: