I am making a space invaders type game(my first game) and I am struggling with the shooting. Here is my code:
p = vec2(400)
state = SPLASH
function setup()
displayMode(FULLSCREEN)
speed = 1
direction = 100
x=WIDTH/5
y=1
end
function draw()
background(42, 36, 36, 255)
sprite("Space Art:Green Ship",p.x,HEIGHT/3)
--enemy
pushMatrix()
translate(WIDTH/1,HEIGHT/1)
rotate(180)
sprite("Space Art:Red Ship",x,y,direction)
if state == SPLASH then
y=y+1
end
x=x+speed
end
function touched(t)
if CurrentTouch.x >949 then
p.x = p.x +4
end
if CurrentTouch.x <70 then
p.x = p.x -4
end
end
Please modify it for the green ship to shoot. Also, I need it to shoot when the ship is moving. Tap the left/right side of the screen to move. Any help would be highly appreciated.
What do you want the green ship to shoot? Lasers? Flames? Bullets?
You probably need this in the draw function to make your ship reverse direction when it gets to the edge of the screen
if x<0 or x>WIDTH then speed=-speed end
To allow for different screen sizes I would change the touch function to make it dependent on width of screen
function touched(t)
if CurrentTouch.x >WIDTH-70 then --<-- CHANGED
There’s a little trick to flipping a sprite upside down, you can replace all this code
pushMatrix()
translate(WIDTH/1,HEIGHT/1)
rotate(180)
sprite("Space Art:Red Ship",x,y,direction) --<--What is direction for?
with this
--first, in setup, read the image into memory
--the way you are doing it in draw, it is being read from disk 60 times a second
enemy=readImage("Space Art:Red Ship")
--then in the draw function
--this draws the image from memory
--and putting a negative height flips the y axis upside down
--(if you want to flip the x axis left to right, make the width negative)
sprite(enemy, x, y, enemy.width, -enemy.height)
PS if you use pushMatrix() to store current screen settings, don’t forget to put them back at the end with popMatrix().
@CalebAnder The starter games are meant for someone to add code to and make a more interesting game. They’re also meant for someone to play with/look at to help them understand how to do different things by changing the code and seeing what happens. You have to know how things work before you can properly use them or else you’re going to have a hard time programming. That comes from reading and experimenting. Shooting bullets is no different than moving your enemy ship across the screen. One other thing, you shouldn’t use CurrentTouch with the function touched(). CurrentTouch is just a simple version of the function touched(). The function touched() gives you more control over touching the screen. I’m sure in several of those starter games is everything you need to know about coding an invaders type game. You just need to understand how to do it.
@dave1707 ok, will do, but I know I shouldn’t use CurrentTouch but I am so confused with the BEGAN, MOVING AND ENDED commands. I have looked at so many examples but I just don’t get it. Please help. Thanks
@CalebAnder Here’s an example of using the touched() function. You can add whatever code you want in each of the if statements to do different things.
function setup()
tx,ty=0,0
str="touch the screen, move your finger, lift your finger"
end
function draw()
background(0)
fill(255)
text(str,WIDTH/2,HEIGHT/2)
text("x= "..tx.." y= "..ty,WIDTH/2,HEIGHT/2-100)
end
function touched(t)
tx=t.x
ty=t.y
if t.state==BEGAN then
str="your finger touched the screen"
elseif t.state==MOVING then
str="your finger is moving on the screen"
elseif t.state==ENDED then
str="your finger was lifted from the screen"
tx,ty=0,0
end
end