Computer Science Canada How do I add multiple rows of enemies in Space Invaders using an Array or loop? |
Author: | Memebot [ Wed Apr 26, 2017 8:27 pm ] |
Post subject: | How do I add multiple rows of enemies in Space Invaders using an Array or loop? |
[size=14][b]What is it you are trying to achieve?[/b][/size] Make More than 1 Row of enemy Appear [size=14][b]Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)[/b][/size] This is the code which we set up in class, we haven't got to the bombs/bullets clashing with the enemies yet, I just wish to figure out the lines of code to make more than 1 row appear %Set up the screen with how you want it to look setscreen ("graphics:max,max") %Width & Height const WIDTH : int := 1170 const HEIGHT : int := 830 setscreen ("graphics:" + intstr (WIDTH) + ";" + intstr (HEIGHT) + ",position:bottom:center,nobuttonbar,title:Space Invaders") %read the picture files into a variable ID var backgroundID : int := Pic.FileNew ("background.jpg") var shipID : int := Pic.FileNew ("ship.bmp") var bulletID : int := Pic.FileNew ("bombD.gif") var alienID : int var alienID1 : int := Pic.FileNew ("bicho.gif") var alienID2 : int := Pic.FileNew ("bicho0.gif") var alienID3 : int := Pic.FileNew ("bicho1.gif") %Allow up to 15 bullets at one time var bulletX : array 1 .. 15 of int := init (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) var bulletY : array 1 .. 15 of int := init (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) var currentTime, bulletTime : int sysclock (bulletTime) % initialize the starting point for the row of aliens var alienX : array 1 .. 16 of int := init (310, 350, 390, 430, 470, 510, 550, 590, 630, 670, 710, 750, 790, 830, 870, 910) var alienY : int := 850 var alienDir : int := 1 var pictureCtr, picture : int := 1 %display the background picture Pic.Draw (backgroundID, 0, 0, picCopy) %Merge the "ship" onto the background at a specified x,y location var y : int := 10 var x : int := floor (WIDTH / 2) Pic.Draw (shipID, x, y, picMerge) %Shoot procedure procedure shoot () %find a free spot to begin another bullet for i : 1 .. 15 if bulletX (i) = 0 then bulletX (i) := x + 43 bulletY (i) := y + 60 sysclock (bulletTime) exit end if end for end shoot %Play game procedure playGame () %capture the arrow key inputs and reposition the ship on the background var chars : array char of boolean loop Input.KeyDown (chars) if chars (KEY_RIGHT_ARROW) then x := x + 5 if x >= WIDTH then x := 0 end if end if if chars (KEY_LEFT_ARROW) then x := x - 5 if x <= 0 then x := WIDTH end if end if if chars (KEY_UP_ARROW) then y := y + 5 if y >= HEIGHT then y := 0 end if end if if chars (KEY_DOWN_ARROW) then y := y - 5 if y <= 0 then y := 0 end if end if if chars (' ') then sysclock (currentTime) if currentTime - bulletTime >= 200 then shoot () end if end if if pictureCtr >= 6 then % time to switch pictures if picture < 2 then picture := picture + 1 else picture := 0 end if pictureCtr := 1 else pictureCtr := pictureCtr + 1 end if % Load the chosen picture of the alien case picture of label 1 : alienID := alienID1 label 2 : alienID := alienID2 label 3 : alienID := alienID3 label 4 : alienID := alienID2 label : alienID := alienID3 end case %determine if aliens are at an edge %if so, switch directions & move down if alienX (16) >= WIDTH - 15 or alienX (1) < 5 then alienDir := alienDir * (-1) alienY := alienY - 30 end if %draw background, then ship,then aliens, then bullets Pic.Draw (backgroundID, 0, 0, picMerge) Pic.Draw (shipID, x, y, picMerge) for i : 1 .. 16 alienX (i) := alienX (i) + alienDir if alienX (i) not= 0 then Pic.Draw (alienID, alienX (i), alienY, picMerge) end if end for for i : 1 .. 15 if bulletY (i) not= 0 then if bulletY (i) + 3 < maxy then bulletY (i) := bulletY (i) + 5 Pic.Draw (bulletID, bulletX (i), bulletY (i), picMerge) else bulletX (i) := 0 bulletY (i) := 0 end if end if end for View.Update end loop end playGame %Play the game procedure playGame () [/syntax] [size=14][b]Please specify what version of Turing you are using[/b][/size] I believe we are using 4.1 in class. |
Author: | Insectoid [ Fri Apr 28, 2017 2:20 pm ] | ||||
Post subject: | RE:How do I add multiple rows of enemies in Space Invaders using an Array or loop? | ||||
You''re already using an array to store your enemies so you're halfway there. You already know that if you need a bunch of a thing, you keep them in an array. What do we want now? A bunch of rows. A row is a thing though, right? So if we want a bunch of rows, it makes sense to store them in an array! Since an array can be of any type, and an array is itself a type, we can make an array of arrays!
This array contains 5 cells, each of which is itself an array of length 5. You access elements in the array pretty much as you'd expect:
This will return the 3rd element of the 2nd element of foo. Now you need to write some code that works on arrays of arrays. I'll leave that up to you, but I'll give you a hint: Arrays of arrays play really nice with loops inside loops. |