"Dodge Ball" Game- Collision Checking Issues
Author |
Message |
boomgreen87
|
Posted: Tue Oct 28, 2014 7:58 am Post subject: "Dodge Ball" Game- Collision Checking Issues |
|
|
What is it you are trying to achieve?
I'm taking a programming class for the first time and I've been in the class for about 2 months. I have a lot of free time in class so I am trying to create a "dodge ball" game.
What is the problem you are having?
I can't get the game to properly check when a ball hits the player
Describe what you have tried to solve this problem
Various 'if' statements. An example is in the current code.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
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 "" : 30, "Welcome to DODGEBALL"
put "" : 29, "Press any key to play!"
put ""
put "" : 34, "How to play:"
put "" : 18, "Use the arrow keys to move. Avoid the balls!"
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
drawoval (maxx + 50, maxy - 50 - ballposition, ballradius, ballradius, red)
%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"
drawbox (playerxpos, playerypos, playerxpos + 50, playerypos + 50, black)
%Draws dodgeballs
drawoval (ballspeed, maxy - 50 - ballposition, ballradius, ballradius, red)
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
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: Tue Oct 28, 2014 8:31 am Post subject: RE:"Dodge Ball" Game- Collision Checking Issues |
|
|
The skill you're trying to learn is called Collision Detection.
For a beginner, you should pick between checking for collisions between circles, or between rectangles.
Doing collision detection between a circle and a rectangle (properly) is intermediate level.
Since you want "dodge balls" to be a circle, I'd advise turning your player into a circle as well.
You can learn about Circle collision detection in the Turing Walkthrough.
http://compsci.ca/v3/viewtopic.php?t=13661
Circle collision detection is basically calculating if the distance between the centers of both circles (using the Pythagorean theorem) is smaller or equal to the sum of both radius's. |
|
|
|
|
|
boomgreen87
|
Posted: Wed Oct 29, 2014 7:01 am Post subject: Re: "Dodge Ball" Game- Collision Checking Issues |
|
|
Okay! Thanks a lot! |
|
|
|
|
|
|
|