Computer Science Canada SIMPLE Space Invaders Game HELP?? |
Author: | Smarties102 [ Thu Jun 16, 2011 6:07 pm ] |
Post subject: | SIMPLE Space Invaders Game HELP?? |
What is it you are trying to achieve? For grade 10 computer science I need to create a game of space invaders. What is the problem you are having? I don't know how to code to return to the main menu after the aliens touch the ground because the variable is stored as an array of int. Describe what you have tried to solve this problem I've tried if statements and using like when yEnemies <0 then exit (along the lines of that) Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) % SpaceInvaders.t % Started on June 8,2011 % Starts a game of Space Invaders % Enables the graphics and sets the screen size to 1000 x 700 setscreen ("graphics:1010;695,nobuttonbar,nocursor,position:centre,truemiddle,offscreenonly") % Declares variables var titleFont : int := Font.New ("Courier New:89:bold") var buttonFont : int := Font.New ("Consolas:45") var otherTitleFont : int := Font.New ("Segoe UI:70:bold") var mainTextFont : int := Font.New ("Segoe UI:20") var enemiesPics : array 1 .. 5 of int var player : int := Pic.FileNew ("player.bmp") var playerTitle : int := Pic.Scale (player, 300, 200) playerTitle := Pic.Rotate (playerTitle, 90, -1, -1) var enemyTitle : int var x, y, button, level, counter, numEnemies, enemyType, xPlayer, xShot, yShot, direction, numDead : int := 0 var xEnemies, xEnemiesOriginal, yEnemies : array 1 .. 60 of int var enemies : array 1 .. 60 of boolean var shot, titleShot, titleEnemy, directionChange : boolean := false var keys : array char of boolean % Initializes pics enemiesPics (1) := Pic.FileNew ("enemy1.bmp") enemiesPics (2) := Pic.FileNew ("enemy2.bmp") enemiesPics (3) := Pic.FileNew ("enemy3.bmp") enemiesPics (4) := Pic.FileNew ("enemy4.bmp") enemiesPics (5) := Pic.FileNew ("mothership.bmp") enemyTitle := Pic.Scale (enemiesPics (2), 200, 200) % Ensures the fonts and pictures exist assert titleFont > 0 assert buttonFont > 0 assert otherTitleFont > 0 assert mainTextFont > 0 assert player > 0 % Scales the pics for i : 1 .. 5 assert enemiesPics (i) > 0 enemiesPics (i) := Pic.Scale (enemiesPics (i), 50, 50) end for % Checks to see if the mouse is hovering over a button procedure ButtonCheck loop Mouse.Where (x, y, button) Font.Draw ("back", 435, 130, buttonFont, brightwhite) if x > 383 and x < 616 and y > 100 and y < 200 then Font.Draw ("back", 435, 130, buttonFont, brightblue) View.Update else Font.Draw ("back", 435, 130, buttonFont, blue) View.Update end if exit when x > 383 and x < 616 and y > 100 and y < 200 and button = 1 end loop end ButtonCheck % Sets the basic layout for a level procedure SetLevel drawfill (0, 0, black, brightgreen) counter := 1 % Draws the enemies only if they have not been destroyed for rows : 1 .. level + 1 exit when rows = 9 for xPosition : 1 .. 641 by 70 if enemies (xPosition div 70 + 1) = true then Pic.Draw (enemiesPics (rows), xPosition, 700 - (rows * 70), picMerge) xEnemies (counter) := xPosition xEnemiesOriginal (counter) := xPosition yEnemies (counter) := 700 - (rows * 70) end if counter += 1 end for end for % Draws the player Pic.Draw (player, xPlayer, 0, picMerge) View.Update end SetLevel % Used to move enemies procedure UpdateLevel drawfill (0, 0, black, brightgreen) for i : 1 .. numEnemies % Moves the enemies sideways if direction mod 2 = 0 then xEnemies (i) += 1 else xEnemies (i) -= 1 end if % Moves the enemies down 70 pixels if the right/leftmost enemy hits the right/left side of the screen if direction mod 2 = 0 then for decreasing j : numEnemies .. 1 if xEnemies (j) >= maxx - 50 and enemies (j) = true then for k : 1 .. numEnemies yEnemies (k) -= 70 exit when yEnemies (j) <1 end for if directionChange = true then direction += 1 directionChange := false end if end if end for directionChange := true elsif direction mod 2 = 1 then for j : 1 .. numEnemies if xEnemies (j) <= 0 and enemies (j) = true then for k : 1 .. numEnemies yEnemies (k) -= 70 exit when yEnemies (k) <1 end for if directionChange = true then direction += 1 directionChange := false end if end if end for directionChange := true end if % Redraws the enemies if they are still alive if enemies (i) = true then if i <= 10 then Pic.Draw (enemiesPics (1), xEnemies (i), yEnemies (i), picMerge) elsif i <= 20 then Pic.Draw (enemiesPics (2), xEnemies (i), yEnemies (i), picMerge) elsif i <= 30 then Pic.Draw (enemiesPics (3), xEnemies (i), yEnemies (i), picMerge) else Pic.Draw (enemiesPics (4), xEnemies (i), yEnemies (i), picMerge) end if end if end for % Redraws the player Pic.Draw (player, xPlayer, 0, picMerge) View.Update end UpdateLevel % Creates the title screen procedure Title drawfill (0, 0, black, brightgreen) Font.Draw ("SPACE INVADERS", 3, 600, titleFont, white) % Draws the buttons drawfillbox (50, 100, 283, 200, brightwhite) drawbox (50, 100, 283, 200, white) drawfillbox (383, 100, 616, 200, brightwhite) drawbox (383, 100, 616, 200, white) drawfillbox (716, 100, 949, 200, brightwhite) drawbox (716, 100, 949, 200, white) % Draws the button text Font.Draw ("start", 84, 130, buttonFont, blue) Font.Draw ("help", 435, 130, buttonFont, blue) Font.Draw ("quit", 765, 130, buttonFont, blue) View.Update end Title % Contains the main game procedure PlayGame loop Music.PlayFileLoop ("Deadmau5 - 8 bit HD.mp3") % Initializes variables level += 1 exit when level > 4 direction := 0 directionChange := true numEnemies := (level + 1) * 10 for i : 1 .. numEnemies enemies (i) := true end for numDead := 0 cls % Draws the player drawfill (0, 0, black, brightgreen) xPlayer := maxx div 2 - 75 Pic.Draw (player, xPlayer, 0, picMerge) % Draws the level start message Font.Draw ("Level " + intstr (level) + " Start", 225, maxy div 2, otherTitleFont, white) Font.Draw ("Press any key to start", 360, maxy div 2 - 30, mainTextFont, white) View.Update Input.Pause % Sets up the level cls SetLevel % Gets user input for the controls loop Input.KeyDown (keys) if keys (KEY_LEFT_ARROW) then xPlayer -= 5 % Ensures that the player does not go off the screen if xPlayer <= 0 then xPlayer := 0 end if % Updates the player drawfillbox (xPlayer, 0, xPlayer + 150, 100, black) Pic.Draw (player, xPlayer, 0, picMerge) View.Update end if if keys (KEY_RIGHT_ARROW) then xPlayer += 5 % Ensures that the player does not go off the screen if xPlayer >= maxx - 150 then xPlayer := maxx - 150 end if % Updates the player drawfillbox (xPlayer, 0, xPlayer + 150, 100, black) Pic.Draw (player, xPlayer, 0, picMerge) View.Update end if % Shoots if keys (' ') and shot = false then shot := true xShot := xPlayer + 75 yShot := 101 drawfilloval (xShot, yShot, 5, 5, white) View.Update end if % If the shot is on screen, then moves the shot if yShot > 100 and yShot < maxy + 5 and shot = true then yShot += 12 drawfilloval (xShot, yShot, 5, 5, white) View.Update else shot := false end if % Checks if there is a collision if shot = true then for i : 1 .. numEnemies if Math.Distance (xShot, yShot, xEnemies (i) + 25, yEnemies (i) + 25) <= 30 and enemies (i) = true then enemies (i) := false numDead += 1 shot := false UpdateLevel end if end for end if exit when numDead = numEnemies % Moves the enemies UpdateLevel end loop end loop end PlayGame % Contains the instructions procedure Instructions enemyType := 0 drawfill (0, 0, black, brightgreen) Font.Draw ("Instructions", 250, 620, otherTitleFont, white) % Draws the text and sample pictures Font.Draw ("The objective of the game is to destroy all of the enemies.", 10, 550, mainTextFont, white) for xPosition : 50 .. 650 by 150 enemyType += 1 Pic.Draw (enemiesPics (enemyType), xPosition, 425, picMerge) end for Font.Draw ("You are the fighter at the bottom. Use the left and right arrow keys to move.", 10, 400, mainTextFont, white) Font.Draw ("Press the spacebar to shoot. You can only have one shot on screen at", 10, 330, mainTextFont, white) Font.Draw ("a time. You will move slower when a shot is on screen to recharge.", 10, 300, mainTextFont, white) Pic.Draw (player, 840, 280, picMerge) Font.Draw ("The objective of the game is to shoot the aliens before they reach", 10, 230, mainTextFont, white) Font.Draw ("the bottom of the screen.", 10, 200, mainTextFont, white) % Draws the "back" button drawfillbox (383, 100, 616, 200, brightwhite) drawbox (383, 100, 616, 200, white) % Checks if the button is pressed or moused over delay (200) loop Mouse.Where (x, y, button) Font.Draw ("back", 435, 130, buttonFont, brightwhite) if x > 383 and x < 616 and y > 100 and y < 200 then Font.Draw ("back", 435, 130, buttonFont, brightblue) View.Update else Font.Draw ("back", 435, 130, buttonFont, blue) View.Update end if exit when x > 383 and x < 616 and y > 100 and y < 200 and button = 1 end loop cls end Instructions % Starts the appropriate procedure loop Title % Initializes variables used for the title animation xShot := 699 titleShot := true titleEnemy := true delay (200) loop % Draws a simple title animation Pic.Draw (playerTitle, 700, maxy div 2 - 100, picMerge) if titleEnemy = true then Pic.Draw (enemyTitle, 100, maxy div 2 - 60, picMerge) end if if xShot < 700 and titleShot = true then drawfilloval (xShot + 2, maxy div 2 + 50, 10, 10, black) drawfilloval (xShot, maxy div 2 + 50, 10, 10, white) xShot -= 2 end if if titleEnemy = true then if Math.Distance (xShot, maxy div 2 + 50, 200, maxy div 2 + 50) <= 110 then titleShot := false titleEnemy := false drawfillbox (100, maxy div 2 - 60, 300, maxy div 2 + 140, black) drawfilloval (xShot + 2, maxy div 2 + 50, 10, 10, black) end if end if % "Shoots" on screen if the spacebar is pressed Input.KeyDown (keys) if keys (' ') and titleShot = false then titleShot := true xShot := 699 drawfilloval (xShot + 2, maxy div 2 + 50, 10, 10, black) drawfilloval (xShot, maxy div 2 + 50, 10, 10, white) end if if xShot < -12 and titleShot = true then titleShot := false end if % Checks if the buttons are pressed or moused over Mouse.Where (x, y, button) if x > 50 and x < 283 and y > 100 and y < 200 and button = 1 then PlayGame exit elsif x > 383 and x < 616 and y > 100 and y < 200 and button = 1 then Instructions exit elsif x > 716 and x < 949 and y > 100 and y < 200 and button = 1 then exit elsif x > 50 and x < 283 and y > 100 and y < 200 then Font.Draw ("start", 84, 130, buttonFont, brightwhite) Font.Draw ("start", 84, 130, buttonFont, brightblue) View.Update elsif x > 383 and x < 616 and y > 100 and y < 200 then Font.Draw ("help", 435, 130, buttonFont, brightwhite) Font.Draw ("help", 435, 130, buttonFont, brightblue) View.Update elsif x > 716 and x < 949 and y > 100 and y < 200 then Font.Draw ("quit", 765, 130, buttonFont, brightwhite) Font.Draw ("quit", 765, 130, buttonFont, brightblue) View.Update else Font.Draw ("start", 84, 130, buttonFont, brightwhite) Font.Draw ("start", 84, 130, buttonFont, blue) Font.Draw ("help", 435, 130, buttonFont, brightwhite) Font.Draw ("help", 435, 130, buttonFont, blue) Font.Draw ("quit", 765, 130, buttonFont, brightwhite) Font.Draw ("quit", 765, 130, buttonFont, blue) View.Update end if end loop exit when x > 716 and x < 949 and y > 100 and y < 200 end loop |