Score count and and collision detection
Author |
Message |
Original
|
Posted: Fri May 08, 2009 1:46 pm Post subject: Score count and and collision detection |
|
|
No one answered my previous question, so I'll try to rephrease it this time. I am new to turing, and i am trying to make a game that has donuts falling on the background. When Homer's face hits the donuts, the score goes up by one. I need to learn how to make a score count, and how to make the collision detection between homer and the falling donuts. Here is the code i have written so far. One more thing, when you run this, the donuts constantly flash. Can some help me fix this problem? Your help is appreciated. Here is the code.
Thanks
var x, y : int
x := 100
var movex : int
var movec : int
var homer : int := Pic.FileNew ("homer.bmp")
y := 100
var chars : array char of boolean
process donutfall
loop
randint (movec, 0, 255)
randint (movex, 0, maxx)
for decreasing i : maxy .. 0 by 20
Draw.FillOval (movex, i, 20, 20, movec)
delay (20)
cls
View.UpdateArea (0, 0, maxx, maxy)
if i <= 20 then
delay (1000)
end if
end for
end loop
end donutfall
procedure homermove
loop
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
y := y + 6
Pic.Draw (homer, x, y, picCopy)
end if
if chars (KEY_RIGHT_ARROW) then
x := x + 6
Pic.Draw (homer, x, y, picCopy)
end if
if chars (KEY_LEFT_ARROW) then
x := x - 6
Pic.Draw (homer, x, y, picCopy)
end if
if chars (KEY_DOWN_ARROW) then
y := y - 6
Pic.Draw (homer, x, y, picCopy)
end if
Pic.Draw (homer, x, y, picCopy)
delay (15)
cls
View.UpdateArea (0, 0, maxx, maxy)
end loop
end homermove
fork donutfall
homermove |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TheGuardian001
|
Posted: Fri May 08, 2009 2:18 pm Post subject: Re: Score count and and collision detection |
|
|
In the future, please use syntax tags when posting.
code: | [syntax="Turing]code here[/syntax] |
First off, Never ever use processes in Turing. It may work out the way you want it to occasionally, but in general they are a very poor way to accomplish things (in Turing, of course. other languages make them work, if they have them). Instead, use procedures/functions that only run through once each time you call them
so instead of
Turing: |
proc homermove
loop
%etc
end loop
end homermove
homermove
|
you should use
Turing: |
proc homermove
%etc
end homermove
loop
homermove
end loop
|
That having been said, you will want to look into offscreen or double buffering [url="http://compsci.ca/v3/viewtopic.php?t=12533"]here[/url]
This will eliminate the flickering in your program.
Collision detection is basically just checking if the boxes surrounding an object collide. If you know where the donut picture is and how big it is, you can find out what pixels it occupies. the same goes for Homer's head. Of course, this will not be pixel perfect collision detection, but it will work.
More on collisions can be found [url="http://compsci.ca/v3/viewtopic.php?t=75"]here[/url]
Finally, score is the easiest part of this. all you need to do is have an integer that keeps track of the current score and increase it every time you detect a collision. |
|
|
|
|
|
Dusk Eagle
|
Posted: Fri May 08, 2009 2:20 pm Post subject: Re: Score count and and collision detection |
|
|
Please use syntax tags when posting your code.They work like this:
1. Type [syntax[u]="turing"]
2. Put your code in place.
3. Type [/syntax]
Simple!
A score count is simple: code: |
var score : int := 0
if /*collision*/ then
score += 1%'score += 1' is just a fancy way of saying score := score + 1'
end if
|
As for collision detection, you must check whether the any of the donut is within any of a rectangle around Homer Simpson. (You should make your picture border as small as possible, as it is very difficult to detect anything but the size of your picture, and if your picture area extends for 30 pixels above the top of Homer Simpson's head, it will look bad.)
So, here's a bit of pseudo-code to get you started:
code: |
if donut's_left_edge is <= homer's_right_edge and donut's_right_edge >= homer's_left_edge
if donut's_bottom_edge <= homer's_top_edge and donut's_top_edge >= homer's_bottom_edge
collision = true
|
Edit: TheGuardian001 beat me to it, but you can learn from both our posts. |
|
|
|
|
|
TheGuardian001
|
Posted: Fri May 08, 2009 2:23 pm Post subject: Re: Score count and and collision detection |
|
|
And my Url tags fail ... and now I can't edit them...
Well, in any event, this is another tutorial on collision detection.
http://compsci.ca/v3/viewtopic.php?t=13661 |
|
|
|
|
|
|
|