Orbital Game Help
Author |
Message |
Luminosity
|
Posted: Sun Apr 17, 2011 5:54 pm Post subject: Orbital Game Help |
|
|
What is it you are trying to achieve?
Trying to make a game like Orbital on the Ipod Touch.
What is the problem you are having?
So far the project is going well, the program runs fine for about 3-5 mins but then will crash and give me an Illegal Picture ID error. I am not using any picture files for the project yet, but I only used the Pic.New function to be able to rotate an object. I think the problem may be caused by the Pic.Free function, but I do not know how to fix this.
Describe what you have tried to solve this problem
The only way I got it to work again is by closing the entire Turing program, but it will still crash again after the 3-5 mins.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
My game code so far, the shooting is not implemented yet.
Turing: |
setscreen ("graphics:400;600, nobuttonbar, offscreenonly")
const cx := maxx div 2
const cy := maxy div 2
var angle, slope : real := 0
var chars : array char of boolean
var mouseX, mouseY, mouseButton, shotX, shotY : int := 0
% Changeable variables
var shotSize := 10
var cannonLength := 80
var cannonWidth := 10
% Checks the mouse's boundaries
procedure mouseBounds
% If the mouse's x position is outside of the window then the mouseX value is still inside
if mouseX < 0 then
mouseX := 0
elsif mouseX > maxx then
mouseX := maxx
end if
% If the mouse's y position is outside of the window then the mouseY value is still inside
if mouseY < 0 then
mouseY := 0
elsif mouseY > maxy then
mouseY := maxy
end if
end mouseBounds
% Limits the angle (and slope value) of the cannon
procedure limitAngleSlope (minAngle, maxAngle : int)
% If the angle is greater than the max angle it becomes the max angle and the slope value is equal to the maxangle
if angle > maxAngle then
angle := maxAngle
slope := tand (angle )
% If the angle is less than the min angle it becomes the max angle and the slope value is equal to the minangle
elsif angle < minAngle and mouseX >= 200 then
angle := minAngle
slope := tand (angle )
elsif angle < minAngle and mouseX < 200 then
angle := maxAngle
slope := tand (angle )
end if
end limitAngleSlope
% Creates a picture of a cannon
var picCannon, rotateCannon : int
drawfillbox (0, 0, maxx, maxy, black)
drawfillbox (cannonLength - cannonWidth, 0, cannonLength + cannonWidth, cannonLength, grey)
picCannon := Pic.New (0, 0, cannonLength * 2, cannonLength )
% Clears the screen
cls
% Main Loop
loop
Input.KeyDown (chars )
View.Update
% Draws background
drawfillbox (0, 0, maxx, maxy, black)
% Finds the coordinates of the mouse
Mouse.Where (mouseX, mouseY, mouseButton )
% Checks the mouse boundaries
mouseBounds
% If the mouse's x position is not equal to the center of the screen
if mouseX not= cx then
% Then calculate the slope and the angle
slope := mouseY / (mouseX - cx )
angle := arctand (slope )
else
% Else the angle is 90 (and slope is undefined)
angle := 90
end if
% Changes the negative angles to the correct angle
if angle < 0 then
angle := angle + 180
end if
% Limits the angle (and slope value) of the cannon if it is less than 10 degrees or greater than 170 degrees
limitAngleSlope (10, 170)
% Rotates the barrel of the cannon according to the angle
rotateCannon := Pic.Rotate (picCannon, round (angle ) - 90, cannonLength, 0)
% Draws the barrel of the cannon according to the rotation
Pic.Draw (rotateCannon, cx - cannonLength, 0, picMerge)
Pic.Free (rotateCannon )
% Draws the cannon circle
drawfilloval (cx, 0, cannonLength div 2, cannonLength div 2, grey)
drawoval (cx, 0, cannonLength div 2 - 5, cannonLength div 2 - 5, white)
drawoval (cx, 0, cannonLength div 2, cannonLength div 2, white)
% Draws the red line
drawline (0, 30, maxx, 30, red)
% Calculates the coordinates of the shot
shotX := round (cannonLength * cosd (angle ))
shotY := round (cannonLength * sind (angle ))
% If the left button is pressed then draw the shot
if mouseButton = 1 then
% Draws the shot
drawoval (shotX + cx, shotY, shotSize, shotSize, white)
drawoval (shotX + cx, shotY, shotSize - 5, shotSize - 5, white)
end if
% Quits when the user presses q
if chars ('q') then
Pic.Free (picCannon )
delay (200)
exit
end if
end loop
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Sun Apr 17, 2011 7:33 pm Post subject: RE:Orbital Game Help |
|
|
You allocate a new picture every time you loop (with Pic.Rotate, which makes a new picture). You only deallocate that picture with Pic.Free if the player is pressing Q.
You should rotate the image before you start play, and store the rotated versions of the image in an array of ints. Alternately, you can make sure that a Pic.New / Pic.FileNew / Pic.Rotate is ALWAYS matched with a Pic.Free. |
|
|
|
|
|
Raknarg
|
Posted: Sun Apr 17, 2011 8:05 pm Post subject: RE:Orbital Game Help |
|
|
Or you could simplify this and learn Trig |
|
|
|
|
|
Luminosity
|
Posted: Sun Apr 17, 2011 9:33 pm Post subject: Re: Orbital Game Help |
|
|
@DemonWasp thanks for the suggestions, but I tried both and it didn't work out. The arrays didn't help, it still has the same problem. By matching up the Pic.Free, the program still doesn't work and it gives me an error saying Pic was freed.
@Raknarg could you tell me how to simplify this? thanks |
|
|
|
|
|
Tony
|
Posted: Sun Apr 17, 2011 9:44 pm Post subject: RE:Orbital Game Help |
|
|
Arrays help greatly, but that assumes that you'll be using less than 1000 pictures.
Quote:
By matching up the Pic.Free, the program still doesn't work and it gives me an error saying Pic was freed.
This means that new and free were not matched up correctly (as you are letting go of a picture, and then trying to use it after). |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Raknarg
|
Posted: Tue Apr 19, 2011 6:53 pm Post subject: RE:Orbital Game Help |
|
|
If you really wanted to use trig, you would use this:
Turing: |
View.Set ("offscreenonly")
var x, y, dx, dy : array - 5 .. 5 of int
var mx, my, b, t : int
for i : - 5 .. 5
x (i ) := 200
y (i ) := 200
dx (i ) := 200
dy (i ) := 200
end for
loop
Mouse.Where (mx, my, b )
for i : - 5 .. 5
Draw.Line (dx (i ), dy (i ), x (i ), y (i ), 7)
end for
if b = 1 then
if mx > 200 then
t := round (arctand ((my - 200) / (mx - 200)))
elsif mx < 200 then
t := round (arctand ((my - 200) / (mx - 200))) - 180
end if
for i : - 5 .. 5
dx (i ) := 200 + round (i * cosd (t - 90))
dy (i ) := 200 + round (i * sind (t - 90))
x (i ) := dx (i ) + round (100 * cosd (t ))
y (i ) := dy (i ) + round (100 * sind (t ))
end for
end if
View.UpdateArea (0, 0, maxx, maxy)
delay (20)
cls
end loop
|
However, this is kindof bulky and not quite as effective. If you want a better effect, just use Draw.ThickLine |
|
|
|
|
|
|
|