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

Username:   Password: 
 RegisterRegister   
 URGENT! Airplane Game Collision Detect!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
manyo2010




PostPosted: Tue Jun 11, 2013 11:28 pm   Post subject: URGENT! Airplane Game Collision Detect!

What is it you are trying to achieve?
I'm trying to get the enemy airplane to detect the bullet and then exit.


What is the problem you are having?
The enemy airplane doesn't react to the bullet hitting it.


Describe what you have tried to solve this problem
The planeexit command works. But there is an issue where the program can't realize when to set planeexit to true. Maybe because of multi threading.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

[syntax="turing"]

<setscreen ("graphics:1024;768")

%delcare variables
var background_image : int
var plane1, plane2 : int
var cloud : int
var y : int
var x : int
var airplane : int
var key : string (1)
var move : int
var move1 : int
var xbullet : int
var ybullet : int
var xbullet1 : int
var ybullet1 : int
var xcloud : int
var ycloud : int
var xcloud1 : int
var ycloud1 : int
var xcloud2 : int
var ycloud2 : int
var xplane1, xplane2, xplane3, xplane4 : int
var yplane1, yplane2, yplane3, yplane4 : int
var gameover : boolean
var planeexit1, planeexit2, planeexit3, planeexit4 : boolean
%intialise variables
xplane4 := 750
yplane4 := 900
xplane2 := 250
yplane2 := 1000
xplane1 := 750
yplane1 := 900
y := 0
x := 250
move := 0
move1 := 0
ycloud := 900
xcloud := 0
xcloud1 := 250
xcloud2 := 750
ycloud2 := 900
ycloud1 := 900
gameover := false
planeexit1 := false
planeexit2 := false
planeexit3 := false
planeexit4 := true
%load image from the same folder
plane2 := Pic.FileNew ("plane2.gif")
plane1 := Pic.FileNew ("plane1.gif")
airplane := Pic.FileNew ("Ship00.gif")
background_image := Pic.FileNew ("background.jpg")
cloud := Pic.FileNew ("cloud.jpg")
Pic.SetTransparentColor (airplane, white)
Pic.SetTransparentColor (cloud, white)
Pic.SetTransparentColor (plane1, white)
Pic.SetTransparentColor (plane2, white)


process enemy2
xplane3 := xplane1 - 150
yplane3 := yplane1
loop
View.Set ("offscreenonly")
if yplane3 > 0 then
loop
if gameover = true then
exit
end if
for sway : 1 .. 300
if planeexit3 = true then
exit
end if
if gameover = true then
exit
end if
yplane3 := yplane3 - 2
xplane3 := xplane3 - 2
Pic.Draw (plane1, xplane3, yplane3, picMerge)
drawfilloval (xplane3 + 35, yplane3 + 48, 10, 10, black)
View.Update
if planeexit3 = true then
exit
end if
delay (10)
end for
for sway : 1 .. 200
if planeexit3 = true then
exit
end if
if gameover = true then
exit
end if
yplane3 := yplane3 - 2
xplane3 := xplane3 + 2
Pic.Draw (plane1, xplane3, yplane3, picMerge)
drawfilloval (xplane3 + 35, yplane3 + 48, 10, 10, black)
View.Update
if whatdotcolor (xplane3 + 35, yplane3 + 48) = green then
planeexit3 := true
end if
delay (10)
yplane3 := yplane3 - 2
Pic.Draw (plane1, xplane3, yplane3, picMerge)
drawfilloval (xplane3 + 35, yplane3 + 48, 10, 10, black)
View.Update
if planeexit3 = true then
exit
end if
delay (10)
end for
if planeexit3 = true then
exit
end if
exit when yplane3 < -250
end loop
end if
if planeexit3 = true then
exit
end if
yplane3 := 900
end loop
end enemy2

fork enemy2
put " "

process enemy1
loop
View.Set ("offscreenonly")
if yplane2 > 0 then
loop
if gameover = true then
exit
end if
if planeexit2 = true then
exit
end if
for sway : 1 .. 200
if gameover = true then
exit
end if
if planeexit2 = true then
exit
end if
yplane2 := yplane2 - 1
xplane2 := xplane2 + 2
Pic.Draw (plane2, xplane2, yplane2, picMerge)
drawfilloval (xplane2 + 35, yplane2 + 48, 10, 10, black)
delay (10)
end for
for sway : 1 .. 300
if planeexit2 = true then
exit
end if
if gameover = true then
exit
end if
yplane2 := yplane2 - 1
xplane2 := xplane2 - 2
Pic.Draw (plane2, xplane2, yplane2, picMerge)
drawfilloval (xplane2 + 35, yplane2 + 48, 10, 10, black)
delay (10)
yplane2 := yplane2 - 1
Pic.Draw (plane2, xplane2, yplane2, picMerge)
drawfilloval (xplane2 + 35, yplane2 + 48, 10, 10, black)
delay (10)
if planeexit2 = true then
exit
end if
end for
if planeexit2 = true then
exit
end if
exit when yplane2 < -150
end loop
end if
if planeexit2 = true then
exit
end if
yplane2 := 800
end loop
end enemy1

fork enemy1
put " "


process enemy5
loop
View.Set ("offscreenonly")
if planeexit4 = true then
exit
end if
if yplane4 > 0 then
loop
if planeexit4 = true then
exit
end if
if gameover = true then
exit
end if
for sway : 1 .. 200
if gameover = true then
exit
end if
if planeexit4 = true then
exit
end if
yplane4 := yplane4 - 1
xplane4 := xplane4 + 2
Pic.Draw (plane2, xplane4, yplane4, picMerge)
drawfilloval (xplane4 + 35, yplane4 + 48, 10, 10, black)
delay (10)
end for
for sway : 1 .. 300
if gameover = true then
exit
end if
if planeexit4 = true then
exit
end if
yplane4 := yplane4 - 1
xplane4 := xplane4 - 2
Pic.Draw (plane2, xplane4, yplane4, picMerge)
drawfilloval (xplane4 + 35, yplane4 + 48, 10, 10, black)
if whatdotcolor (xplane3 + 35, yplane3 + 48) = green then
exit
end if
delay (10)
yplane2 := yplane2 - 1
Pic.Draw (plane2, xplane2, yplane2, picMerge)
drawfilloval (xplane2 + 35, yplane2 + 48, 10, 10, black)
end for
exit when yplane4 < -150
end loop
end if
if planeexit4 = true then
exit
end if
yplane4 := 900
end loop
end enemy5

fork enemy5
put " "


process enemy
loop
View.Set ("offscreenonly")
if planeexit1 = true then
exit
end if
if yplane1 > 0 then
loop
if gameover = true then
exit
end if
if planeexit1 = true then
exit
end if
for sway : 1 .. 200
if gameover = true then
exit
end if
if planeexit1 = true then
exit
end if
yplane1 := yplane1 - 2
xplane1 := xplane1 - 2
Pic.Draw (plane1, xplane1, yplane1, picMerge)
drawfilloval (xplane1 + 35, yplane1 + 48, 10, 10, black)
delay (10)
end for
for sway : 1 .. 200
if gameover = true then
exit
end if
if planeexit1 = true then
exit
end if
yplane1 := yplane1 - 2
xplane1 := xplane1 + 2
Pic.Draw (plane1, xplane1, yplane1, picMerge)
drawfilloval (xplane1 + 35, yplane1 + 48, 10, 10, black)
delay (10)
yplane1 := yplane1 - 2
Pic.Draw (plane1, xplane1, yplane1, picMerge)
drawfilloval (xplane1 + 35, yplane1 + 48, 10, 10, black)
delay (10)
end for
exit when yplane1 < -250
end loop
end if
if planeexit1 = true then
exit
end if
yplane1 := 900
end loop
end enemy

fork enemy
put " "

process cloudwes2
loop
View.Set ("offscreenonly")
if ycloud2 > 0 then
loop
if gameover = true then
exit
end if
ycloud2 := ycloud2 - 3
Pic.Draw (cloud, xcloud2, ycloud2, picMerge)
delay (10)
exit when ycloud2 <= -700
end loop
end if
ycloud2 := 900
end loop
end cloudwes2

fork cloudwes2

put " "



process cloudwes1
loop
View.Set ("offscreenonly")
if ycloud1 > 0 then
loop
if gameover = true then
exit
end if
ycloud1 := ycloud1 - 7
Pic.Draw (cloud, xcloud1, ycloud1, picMerge)
delay (10)
exit when ycloud1 <= -500
end loop
end if
ycloud1 := 900
end loop
end cloudwes1

fork cloudwes1
put " "

process cloudwes
loop
View.Set ("offscreenonly")
if ycloud > 0 then
loop
if gameover = true then
exit
end if
ycloud := ycloud - 5
Pic.Draw (cloud, xcloud, ycloud, picMerge)
delay (10)
exit when ycloud <= -100
end loop
end if
ycloud := 900
end loop
end cloudwes

fork cloudwes
put " "
View.Update

process test1
loop
View.Set ("offscreenonly")
if gameover = true then
exit
end if
getch (key)
xbullet1 := x
ybullet1 := y
if key = " " then
loop
if gameover = true then
exit
end if
drawfilloval (xbullet1 + 75, ybullet1 + 45 + move1, 5, 5, green)
move1 := move1 + 10
exit when move1 >= 500
delay (10)
end loop
end if
if xbullet > xplane4 and xbullet < xplane4 + 66 and ybullet + move > yplane4 and ybullet + move1 < yplane4 + 96 then
planeexit4 := true
elsif xbullet > xplane3 and xbullet < xplane3 + 66 and ybullet + move > yplane3 and ybullet + move1 < yplane3 + 96 then
planeexit3 := true
elsif xbullet > xplane2 and xbullet < xplane2 + 66 and ybullet + move > yplane2 and ybullet + move1 < yplane2 + 96 then
planeexit2 := true
elsif xbullet > xplane1 and xbullet < xplane1 + 66 and ybullet + move > yplane1 and ybullet + move1 < yplane1 + 96 then
planeexit1 := true
end if
move1 := 0
end loop
end test1

fork test1
put " "

process test
loop
View.Set ("offscreenonly")
getch (key)
xbullet := x
ybullet := y
if key = " " then
loop
if gameover = true then
exit
end if
drawfilloval (xbullet, ybullet + 45 + move, 5, 5, green)
move := move + 10
exit when move >= 500
delay (10)
end loop
end if
if xbullet > xplane4 and xbullet < xplane4 + 66 and ybullet + move > yplane4 and ybullet + move < yplane4 + 96 then
planeexit4 := true
elsif xbullet > xplane3 and xbullet < xplane3 + 66 and ybullet + move > yplane3 and ybullet + move < yplane3 + 96 then
planeexit3 := true
elsif xbullet > xplane2 and xbullet < xplane2 + 66 and ybullet + move > yplane2 and ybullet + move < yplane2 + 96 then
planeexit2 := true
elsif xbullet > xplane1 and xbullet < xplane1 + 66 and ybullet + move > yplane1 and ybullet + move < yplane1 + 96 then
planeexit1 := true
end if
move := 0
end loop
end test

fork test
loop
View.Set ("offscreenonly")
if gameover = true then
exit
end if
delay (10)
if parallelget = 104 then
y := y + 2
elsif parallelget = 168 then
x := x + 2
elsif parallelget = 56 then
x := x - 2
elsif parallelget = 8 then
y := y - 2
elsif parallelget = 120 then
y := y + 2
x := x - 2
elsif parallelget = 232 then
y := y + 2
x := x + 2
elsif parallelget = 24 then
y := y - 2
x := x - 2
elsif parallelget = 136 then
y := y - 2
x := x + 2
end if
%draw image
Pic.Draw (background_image, 0, 0, 0)
Pic.Draw (airplane, x, y, picMerge)
if whatdotcolor (x + 47, y + 96) = black then
gameover := true
end if
if whatdotcolor (x + 49, y + 96) = black then
gameover := true
end if
if whatdotcolor (x + 48, y + 96) = black then
gameover := true
end if
if whatdotcolor (x + 96, y + 48) = black then
gameover := true
end if
if whatdotcolor (x, y + 48) = black then
gameover := true
end if
if whatdotcolor (x + 48, y) = black then
gameover := true
end if
if gameover = true then
Pic.Draw (background_image, 0, 0, 0)
put "Game Over."
end if
end loop

process restart
loop
if planeexit4 = true then
delay (15000)
planeexit4 := false
put "hello"
elsif planeexit3 = true then
delay (15000)
planeexit3 := false
elsif planeexit2 = true then
delay (15000)
planeexit2 := false
elsif planeexit1 = true then
delay (15000)
planeexit1 := false
end if
end loop
end restart

fork restart
put " " >


Please specify what version of Turing you are using
4.1.1



Final Plane Game TURING.zip
 Description:
If you don't have parallelget then just remove that command.

Download
 Filename:  Final Plane Game TURING.zip
 Filesize:  281.01 KB
 Downloaded:  161 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Jun 12, 2013 12:21 am   Post subject: RE:URGENT! Airplane Game Collision Detect!

You have 4 different exit flags
Quote:

var planeexit1, planeexit2, planeexit3, planeexit4 : boolean

and each is checked 6 or 7 times (quite actually, planeexit2 and planeexit3 are checked 7 times each, while planeexit1 and planeexit4 are checked "only" 6 times) if some loop should end.

Can you reason about the state of the game's world, for all 26 of those conditions, while shuffling the control flow around via processes? That seems difficult. You should at least log the states to help you make sense of what's going on. E.g.

code:

if planeexit3 = true then
put "planeexit3 is true for the 5th time, now exiting"
exit
end if


Although since you are also drawing stuff to the screen, you might want to "put" that into a log file instead.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
manyo2010




PostPosted: Wed Jun 12, 2013 8:14 am   Post subject: Re: RE:URGENT! Airplane Game Collision Detect!

Tony @ Wed Jun 12, 2013 12:21 am wrote:
You have 4 different exit flags
Quote:

var planeexit1, planeexit2, planeexit3, planeexit4 : boolean

and each is checked 6 or 7 times (quite actually, planeexit2 and planeexit3 are checked 7 times each, while planeexit1 and planeexit4 are checked "only" 6 times) if some loop should end.

Can you reason about the state of the game's world, for all 26 of those conditions, while shuffling the control flow around via processes? That seems difficult. You should at least log the states to help you make sense of what's going on. E.g.

code:

if planeexit3 = true then
put "planeexit3 is true for the 5th time, now exiting"
exit
end if


Although since you are also drawing stuff to the screen, you might want to "put" that into a log file instead.


I actually set the standard values for the planeexit commands to true in the beginning, and all of the planes did not show up. So I don't think its a problem of the planes not working on the command. I have it checking the planeexit command so often because I want to be sure that the plane actually exits. I did what you told me to do, and added a put command whenever the bullet is supposed to hit the airplane, and only one airplane showed that put command. The rest did not for some reason.
Tony




PostPosted: Wed Jun 12, 2013 12:32 pm   Post subject: Re: RE:URGENT! Airplane Game Collision Detect!

manyo2010 @ Wed Jun 12, 2013 8:14 am wrote:
I did what you told me to do, and added a put command whenever the bullet is supposed to hit the airplane, and only one airplane showed that put command. The rest did not for some reason.

That's very good, in a way that you are getting much closer to the root of the problem. Now, what's different in the code between the plane that works and one that doesn't?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
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  [ 4 Posts ]
Jump to:   


Style:  
Search: