We are about to switch to a new forum software. Until then we have removed the registration on this forum.
it's following a path that looks like this.
that why I said tower defense space invaders they dont follow simple paths. I tried using a loop on the sides going up. After done copying the track I'l remove the picture then have the enemy do the track without the player seeing where the enemy will go.
Probably. But I wouldn't touch that code with a ten-foot pole.
Instead, let's back up a bit and think about things a little. How are you enemies actually moving? Like the classic space invaders? Like this?
OK, I'm making a tower defense/ space invaders game and an enemy moves the same way a bunch of times in a row, but when I tried dropping it into a loop the enemy zoomed off the screen. So now I got a bunch of lines of code making the enemy do like a staircase moving. I think there is a way to make it less code, but idk. Here the movement code:
int x,y,t,sp;
int spd = 1;
if (t == 0){
sp = y;
t = 1;
}
if (t == 1){
y -= spd;
if (y <= sp-100){
t = 2;
sp = x;
}
}
if (t == 2){
x -= spd;
if (x < sp-290){
t = 3;
sp = y;
}
}// t == 1
if (t == 3){
y -= spd;
if (y <= sp-80){
t = 4;
sp = x;
}
}
if (t == 4){
x += spd;
if (x > sp+50){
t = 5;
sp = y;
}
}// t == 1
if (t == 5){
y -= spd;
if (y <= sp-80){
t = 6;
sp = x;
}
}
if (t == 6){
x += spd;
if (x > sp+50){
t = 7;
sp = y;
}
}// t == 1
if (t == 7){
y -= spd;
if (y <= sp-80){
t = 8;
sp = x;
}
}
if (t == 8){
x += spd;
if (x > sp+50){
t = 9;
sp = y;
}
}// t == 1
if (t == 9){
y -= spd;
if (y <= sp-80){
t = 10;
sp = x;
}
}
if (t == 10){
x += spd;
if (x > sp+50){
t = 11;
sp = y;
}
}// t == 1
if (t == 11){
y -= spd;
if (y <= sp-50){
t = 12;
sp = x;
}
}
if (t == 12){
x += spd;
if (x > sp+70){
t = 13;
sp = y;
}
}// t == 1
if (t == 13){
y += spd;
if (y >= sp+350){
t = 14;
sp = x;
}
}
if (t == 14){
x += spd;
if (x > sp+85){
t = 15;
sp = y;
}
}// t == 1
can anyone help me make this code shorter.
Hey, I'm making a space invaders game & I made a bunch of different kind of bullets the player can use, but when a bullet collides with 2 enemies it gives me a bullet remove error.
for (int i = Bullet.size()-1; i >= 0; i--)
{ bullet bu = Bullet.get(i);
bu.show();
bu.move();
for (int j = Enemy.size()-1; j >= 0; j--)
{ enemy en = Enemy.get(j);
float d = dist(bu.x,bu.y,en.x,en.y);
if (d < bu.r + en.r){
Bullet.remove(i);
en.life -= 1; } } }
anyone know a way to fix it so my bullet ca hit 2 enemies without an error. I think the trouble is that both enemies it collided with are trying to remove it at the same time.
My solution will not work easily with your code. The problem is that you need
1) record the gun's current x position when the bullet is fired then
2) use this x position (not the gun's position) as the bullet moves up the screen.
The problem with your code is that the gun fires AND the bullet moves on the same event, the mousePressed event.
The real problem is that you don't record the {x,y] position of the bullets rather use some maths to calculate the y position each frame.
To create a space invaders type game you need to be able to store data about multiple bullets and multiple aliens. You can't do that unless you use arrays or some other Java collection.
Imagine the real game, you press and hold the fire button and move the gun to the right. You will get a stream of bullets like this.
You need to manage a collection of bullets with each bullet having it's own [x,y] position.
I suggest that you look at the tutorials on arrays. I also suggest using the PVector class to store positions.
Trying to make one from scratch with limited knowledge on coding. I have been able to get it to move left to right, but I can't seem to get the bullets to stop moving along with the turret or to disappear once it hits the top of the canvas.
float gunX;
float bulletY;
float SPEED = 2.0;
float STALL = 0.0;
int N=0;
final int BLACK = #000000;
final int WHITE = #FFFFFF;
final float gunBodySize = 50;
final float barrelParameterOne=25;
final float barrelParameterTwo=75;
final float bulletDIAM = 5;
final float SPACING = 20;
boolean hitLeftEdge, hitRightEdge,hitTop;
void setup(){
size(500,500);
gunX=width/2-barrelParameterOne;
}
void draw(){
background(150);
moveBullet();
drawBullet();
moveGun();
drawGun();
}
void moveGun(){
hitLeftEdge();
hitRightEdge();
if (key == CODED) {
if (hitLeftEdge){
gunX=0;
}
else if (keyPressed && keyCode == LEFT) {
gunX+=-SPEED;
}
if (hitRightEdge){
gunX=width-gunBodySize;
}
else if (keyPressed && keyCode == RIGHT) {
gunX+=SPEED;
}
}
}
void drawGun(){
fill(WHITE);
rect(gunX,height, gunBodySize,-gunBodySize);
triangle(gunX,height-gunBodySize,gunX+barrelParameterOne,height-barrelParameterTwo,gunX+gunBodySize,height-gunBodySize);
}
void hitLeftEdge(){
if (gunX<0){
hitLeftEdge = true;
}
else {
hitLeftEdge = false;
}
}
void hitRightEdge(){
if (gunX>width-gunBodySize){
hitRightEdge = true;
}
else {
hitRightEdge = false;
}
}
void drawBullet(){
fill(BLACK);
if (mousePressed){
int i = 1;
while (i<=5){
bulletY=height-(gunBodySize+SPEED*N)+SPACING*i;
ellipse(gunX+barrelParameterOne,bulletY,bulletDIAM,bulletDIAM);
i++;
}
}
}
void moveBullet(){
checkBullet();
if(mousePressed && !hitTop){
N++;
}
else if(mousePressed && hitTop){
N=0;
}
}
void checkBullet(){
if (bulletY<=0){
hitTop = true;
}
else {
hitTop = false;
}
}
I meant once only ever during the sketch lifetime. I didn't see anything in the question about animation, I was just responding to the part about needing good fps performance. I was assuming that this was for static assets -- tiles etc.
If the whole sketch just involves drawing to a single buffer (a mini-screen) then it is fine to just render the buffer with image(buffer, x, y, w, h)
each frame. My sketch also shows that as an alternative in draw, commented out (although it sounds like OP had worked that out or something like it already).
However: if you are draw-time resizing with 5, 10 or 20 different static resources, each requiring their own resize every frame, or if you are drawing the same static resource many times, which is quite common in games (e.g. many ghosts, many bricks, many space invaders) THEN it is really better for performance to resize the base asset only once during the lifetime of the sketch and use (img, x, y)
. A loop with image(buffer, x, y, w, h)
in it -- e.g. over an array of enemies -- is a terrible idea.
On the other hand, if in order to draw continuous animation (of a moving ghost, or of a changing screen) you are updating the buffer every frame then drawing to a resize-buffer THEN drawing to the screen, every frame -- then that extra copy won't increase performance. Performance will get worse.
...in that case, the best thing for performance would be not drawing or resizing resources at all (if possible). It is better to store those assets (bricks, invaders, etc.) in sprite sheets and/or load pre-upscaled graphics (if the game has a fixed window size).
Español en el foro, en el pasado:
2015
2014
2013
Español en el foro, recientemente:
2017
2016
Hello, i have some trouble making my code work, let me explain :
First we have to draw rectangles everytime there is a 1 on this array :
int[][] alienSprite = {
{1, 0, 1, 1, 1, 0, 1},
{0, 1, 0, 1, 0, 1, 0},
{1, 1, 1, 1, 1, 1, 1},
{0, 1, 0, 1, 0, 1, 0},
{1, 0, 0, 0, 0, 0, 1}
};
The next thing is to store the position where the invaders will spawn :
int[] posAlienX = new int[8];
void setup() {
size (800, 600);
for (int i = 0; i <posAlienX.length; i++){
posAlienX[i] = x;
x = x +100;
}
}
Till now it was very basic stuff, i created 2 arrays, one for the rectangles "pattern", one for the invaders positions...
void alien(){
int spriteY = 20;
int n = 0;
for (int s = 0; s < posAlienX.length; s++){
int spriteX = posAlienX[s];
while (n < 7) {
for (int i = 0; i < alienSprite.length; i++ ){
if (alienSprite[i][n] == 1){
fill(125);
rect(spriteX,spriteY,5,5);
spriteY = spriteY + 5;
}
else if (alienSprite[i][n] == 0){
spriteY = spriteY + 5;
}
}
spriteX = spriteX +5;
spriteY = 20;
n++;
}
}
}
when i enter the loop i want SpriteX to be the position of the cell, but it doesn't work.
Unfortunately it draw only one invader (i need 8). I don't understand because i have a loop to change my SpriteX position with the AlienX array. Again i'm new to Java and programming, so thanks for your help and your patience.
it give me this :
ok, so I thought I'd make my newer version of space invaders like space invaders the regular one & have them all move down together, but they keep just moving indivual.
here the code in enemy I think that all you need idk;
class Enemy extends Circle {
float life = 1;
float spd = 1;
float type = 1; // here I give each class a type so I can have several different enemies
Enemy(float x, float y, float r) {
super(x, y, r);
}
void show() {
fill(128, 0, 128);
ellipse(x, y, r*2, r*2);
}
void move() {
x += spd;
if (x+r > width && spd > 0) { // here it checks if collide with wall
for (Enemy e : enemy) { // here I want it to loop through all the enemy
if (e.type == 1 && spd > 0) { //any with type = 1 need to move down
spd *= -1;
y += 30;
}
}
} else if (x-r < 0 && spd < 0) { /// I havent done this one yet it for the other side
spd *= -1;
y += 30;
}
}
void grow() {
for (int i = enemy.size()-1; i >= 0; i--) {
Enemy e = enemy.get(i);
if (e.life <= 0) {
enemy.remove(i);
control.dead -= 1;
}
}
}
}
Hey, so I'm still working on my space invaders game & got tons of buttons. Each button has a long line of code to check if the mouse is on it & every time I make I new button. I gotta change the long line of code here and there. I was wondering if there a way to shorten the line. I got a button class & an array holding the buttons.
here the long line of code;
if (mouseButton == LEFT && mouseX < button[3].x+button[3].w/2 && mouseX > button[3].x-button[3].w/2 && mouseY < button[3].y+button[3].h/2 && mouseY > button[3].y-button[3].h/2){ /// this the line I'm wondering if there a way to shorten
if (room == 0){
exit();
}
}
if (mouseButton == LEFT && mouseX < button[4].x+button[4].w/2 && mouseX > button[4].x-button[4].w/2 && mouseY < button[4].y+button[4].h/2 && mouseY > button[4].y-button[4].h/2){ /// this the line I'm wondering if there a way to shorten
if (room == 2 || room == 3){
room = 0;
}
}
if (mouseButton == LEFT && mouseX < button[5].x+button[5].w/2 && mouseX > button[5].x-button[5].w/2 && mouseY < button[5].y+button[5].h/2 && mouseY > button[5].y-button[5].h/2){/// this the line I'm wondering if there a way to shorten
if (room == 4){
doubleShot = true;
}
}
OK, I'm making a space invaders game & ran into a problem. I been slamming into a wall for 15 minutes then decided I'll ask others. So I got buttons, Button[] button, leading to different rooms, float room, it works fine until I return to my menu room then the buttons stop working. I click on them & nothing. There are 3 buttons in the menu. 5 buttons in all, the other 2 buttons are in other rooms. I hope I gave all the information you need. Processing Java. When the problem happened I was coming back from looking at my high score board with a menu button.
float wave, alive, dead, boss,room,high;
int checks;
ArrayList<Enemy> enemy;
ArrayList<Bullet> bullet;
boolean restart = false;
Score[] score = new Score[5];
Button[] button = new Button[5];
Ship ship;
HighScore highscore;
LVL lvl;
Reset reset;
Movement movement;
void setup(){
size(500,600);
reset = new Reset();
enemy = new ArrayList<Enemy>();
lvl = new LVL();
for (int i = 0 ; i < 5 ; i++) {
score[i] = new Score();
}
highscore = new HighScore();
button[0] = new Button(width/2,100,200,50);
button[1] = new Button(width/2,160,200,50);
button[2] = new Button(width/2,220,200,50);
button[3] = new Button(width/2,100,200,50);
button[4] = new Button(width-125,30,200,50);
ship = new Ship(width/2,height-15,10);
bullet = new ArrayList<Bullet>();
movement = new Movement();
}
void draw(){
background(0);
if (restart == true){
reset.update();
button[3].show();
button[3].update();
button[1].show();
button[1].update();
text("Replay", button[3].x-50,button[3].y+10);
text("High Score", button[1].x-75,button[1].y+10);
}
if (room == 0){
for (int i = 0; i < 3; i++){
button[i].show();
button[i].update();
text("Start Game", button[0].x-90,button[0].y+10);
text("High Score", button[1].x-75,button[1].y+10);
text("Movement", button[2].x-75,button[2].y+10);
}
}
if (room == 1){
lvl.show();
lvl.make();
lvl.update();
}
if (room == 2){
highscore.show();
for (int i = 0; i<5; i++){
textSize(32);
fill(255);
text(i+1 + " High Score: " + score[i].total, 10, 150+(50*i));
}
}
if (room == 3){
movement.show();
}
}
void mousePressed(){
if (mouseButton == LEFT && button[0].collide == true){
room = 1;
}
if (mouseButton == LEFT && button[1].collide == true){
room = 2;
if (restart == true){
highscore.update();
highscore.on = false;
restart = false;
}
}
if (mouseButton == LEFT && button[2].collide == true){
room = 3;
}
if (mouseButton == LEFT && button[4].collide == true){
room = 0;
}
}
class Button{
float x,y,w,h;
boolean collide = false;
Button(float x, float y, float w, float h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
void show(){
fill(255,255,0);
rectMode(CENTER);
rect(x,y,w,h);
fill(255,0,0);
textSize(32);
}
void update(){
if (mouseX < x+w/2 && mouseX > x-w/2 && mouseY < y+h/2 && mouseY > y-h/2){
collide = true;
}
}
}
When the bunkers in space invaders get hit, only a few pixels are removed. Do I need to use the pixel array to do that.. or can I do it with the set() function (with loadPixels and updatePixels)... or could I somehow do it with my creation below?
I'm using p5.js.
Alright I did Space Invaders
I have seen few posts related to space invaders before: https://forum.processing.org/two/search?Search=invaders
However, I am not sure if it would be easy or difficult. I will say it is doable.
Kf
Would space invaders be easy?
if this is for the barriers of space invaders and they're only getting hit from the top by aliens you may get away with a 1D array rather than a bitmap
Hello,
some ideas:
don't use draw
as a class name
you even have hw hw = new hw();
, where the object has the same name as the class. Bad idea.
advice: objects small letter (e.g. smallLetter), classes start with a Capital (Bullet)
this if (gamestarted == true) {
same as if (gamestarted) {
I would place all classes at the end in one go and not have one class definition between setup() and draw()
This //circle drawing void
is not correct. Void is just the return type of the function. Write //circle drawing function
instead.
Your handling of the bullet ArrayList
You have this
void movebullets() {
for (Bullet ammo : bullets) {
move();
}
}
inside the class. This is wrong. The class represents only one bullet and what it can do (display, collide). So everything concerning all bullets should be in playdatgame()
or in the function movebullets()
. Also you fail to remove bullets correctly; remove() makes the error ConcurrentModificationException btw. To remove you need to loop backward over the list , see reference on ArrayList
please.
With this: ammo.display();
you display the last bullet but not all in the list. To display all, for loop over them. ammo_x and ammo_y not needed. It's in the class as x,y.
All in all, your understanding of using an ArrayList of objects of a class seems not to be complete yet.
Best, Chrisir ;-)
Full new version:
ClassDraw d = new ClassDraw();
Hardware hw = new Hardware();
//every bitmap used in this code
int[][] bitmap = {
{0, -1},
{-1, -1},
};
int[][] smile = {
{0, -1, -1, -1, 0, 0, 0, 0},
{-1, -1, 0, 0, 0, 0, 0, 0},
{-1, 0, 0, 0, -1, -1, -1, -1},
{-1, 0, 0, 0, 0, 0, 0, 0},
{-1, 0, 0, 0, 0, 0, 0, 0},
{-1, 0, 0, 0, -1, -1, -1, -1},
{-1, -1, 0, 0, 0, 0, 0, 0},
{0, -1, -1, -1, 0, 0, 0, 0},
};
/**
This is the ship
0 0 0 # # 0 0 0
0 0 0 # # 0 0 0
0 0 # # # # 0 0
0 0 # # # # 0 0
0 # # # # # # 0
0 # # # # # # 0
# # # # # # # #
0 # # # # # # 0
*/
int[][] ship = {
{0, 0, 0, -1, -1, 0, 0, 0},
{0, 0, 0, -1, -1, 0, 0, 0},
{0, 0, -1, -1, -1, -1, 0, 0},
{0, 0, -1, -1, -1, -1, 0, 0},
{0, -1, -1, -1, -1, -1, -1, 0},
{0, -1, -1, -1, -1, -1, -1, 0},
{-1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, 0, 0, 0, 0, -1, -1},
};
int[][] enemy = {
{-1, -1, 0, 0, 0, 0, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1},
{0, -1, -1, -1, -1, -1, -1, 0},
{0, -1, -1, -1, -1, -1, -1, 0},
{0, 0, -1, -1, -1, -1, 0, 0},
{0, 0, -1, -1, -1, -1, 0, 0},
{0, 0, 0, -1, -1, 0, 0, 0},
{0, 0, 0, -1, -1, 0, 0, 0},
};
//public game's instructions
boolean gamestarted = true;
//draw class public variables
color bgcolor = 0;
//avatar variables
int a_x = 0;
boolean shootammo = false;
//int ammo_x = 0;
//int ammo_y = 0;
//enemy variables
int e_x = 0;
int e_dir = 1;
ArrayList <Bullet> bullets;
//------------------------------------------------------
void setup() {
//set resolution
size(640, 480);
bullets = new ArrayList();
//set fps to 200
frameRate(200);
}
void draw() {
/**
tests for the commands in class draw
d.circle(320,240, 20, color(0,255,0));
d.pixel(320, 240, color(255,0,0));
d.bitmap(ship,320,240,8,8);
*/
//run the space invaders test code every cycle
playdatgame();
}
// -------------------------------------------------------------
//space invaders prototype function
void playdatgame() {
if (gamestarted) {
d.bg(0);
a_x = hw.getMouseX();
movebullets();
if (hw.pressed()) {
if (!shootammo) { // the ! means not
//shootammo = true;
Bullet newBullet = new Bullet(a_x+4, 450);
bullets.add(newBullet);
//ammo.move();
// ammo.display();
//ammo_x = a_x+4;
//ammo_y = 450;
}
}
//if (shootammo) {
// //d.pixel(ammo_x, ammo_y, -1);
// ammo_y--;
// if (ammo_y<0) {
// shootammo = false;
// }
// //println(e_x);
// if (ammo_y<18) {
// if ((ammo_x >= e_x) && (ammo_x <= e_x+8)) {
// println("HIT");
// exit();
// }
// }
//}
d.bitmap(ship, a_x, 450, 8, 8);
d.bitmap(enemy, e_x, 10, 8, 8);
if (e_dir == -1) {
e_x--;
}
if (e_dir == 1) {
e_x++;
}
if (e_x>width-8) {
e_dir = -1;
}
if (e_x<0) {
e_dir = 1;
}
}
}
void movebullets() {
for (Bullet currentBullet : bullets) {
currentBullet.display();
currentBullet.move();
}
for (int i = bullets.size() - 1; i >= 0; i--) {
Bullet currentBullet = bullets.get(i);
if (currentBullet.isDead) {
bullets.remove(i);
}
}
}
// ============================================
class Bullet {
// represents one bullet
int x;
int y;
boolean isDead=false;
Bullet(int tx, int ty) {
x = tx;
y = ty;
}
void display() {
d.pixel(x, y, -1);
// println(x, y);
}
void move() {
y -= 1;
if (y<-2) {
// bullets.remove(0);
isDead=true;
}
//println(e_x);
if (y<18) {
if ((x >= e_x) && (x <= e_x+8)) {
println("HIT");
exit();
}
}
}
}
//=============================================
//hardware control class
public class Hardware {
int getMouseX() {
return mouseX;
}
boolean pressed() {
return mousePressed;
}
}
// =====================================
//drawing control class
public class ClassDraw {
//pixel drawing void
void pixel(int px, int py,
color pcolor) {
stroke(pcolor);
point(px, py);
}
//circle drawing void
void circle(int cx, int cy, int crad, color ccolor) {
fill(bgcolor);
stroke(ccolor);
ellipse(cx, cy, crad, crad);
}
//background color control void (wipes everything to bg color)
void bg(color bgc) {
background(bgc);
bgcolor = bgc;
}
//bitmap control void (uses 2d arrays to get the image)
void bitmap(int[][] array, int x, int y, int w, int h) {
for (int xc=0; xc<w; xc++) {
for (int yc=0; yc<h; yc++) {
//print(array[xc][yc]);
stroke(color(array[yc][xc]));
point(xc+x, yc+y);
}
}
}
}//class
//
Hey! I started coding a game for my project and I thought I could write it to Processing IDE to test it out. Just one problem: I can't get the bullet system right. I tried using ArrayList(), but it keeps crashing. Also, this is one of my first Java (well, kind of) codes. I usually go with C++ but the graphics are kind of hard to accomplish. Hope someone could help.
(is there any way to make the code smaller? like linking it as .txt or even collapsing it? I tried the C button but I quess it ain't for that :P)
draw d = new draw();
hw hw = new hw();
//every bitmap used in this code
int[][] bitmap = {
{0, -1},
{-1, -1},
};
int[][] smile = {
{0, -1, -1, -1, 0, 0, 0, 0},
{-1, -1, 0, 0, 0, 0, 0, 0},
{-1, 0, 0, 0, -1, -1, -1, -1},
{-1, 0, 0, 0, 0, 0, 0, 0},
{-1, 0, 0, 0, 0, 0, 0, 0},
{-1, 0, 0, 0, -1, -1, -1, -1},
{-1, -1, 0, 0, 0, 0, 0, 0},
{0, -1, -1, -1, 0, 0, 0, 0},
};
/**
This is the ship
0 0 0 # # 0 0 0
0 0 0 # # 0 0 0
0 0 # # # # 0 0
0 0 # # # # 0 0
0 # # # # # # 0
0 # # # # # # 0
# # # # # # # #
0 # # # # # # 0
*/
int[][] ship = {
{0, 0, 0, -1, -1, 0, 0, 0},
{0, 0, 0, -1, -1, 0, 0, 0},
{0, 0, -1, -1, -1, -1, 0, 0},
{0, 0, -1, -1, -1, -1, 0, 0},
{0, -1, -1, -1, -1, -1, -1, 0},
{0, -1, -1, -1, -1, -1, -1, 0},
{-1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, 0, 0, 0, 0, -1, -1},
};
int[][] enemy = {
{-1, -1, 0, 0, 0, 0, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1},
{0, -1, -1, -1, -1, -1, -1, 0},
{0, -1, -1, -1, -1, -1, -1, 0},
{0, 0, -1, -1, -1, -1, 0, 0},
{0, 0, -1, -1, -1, -1, 0, 0},
{0, 0, 0, -1, -1, 0, 0, 0},
{0, 0, 0, -1, -1, 0, 0, 0},
};
//public game's instructions
boolean gamestarted = true;
//draw class public variables
color bgcolor = 0;
//avatar variables
int a_x = 0;
boolean shootammo = false;
int ammo_x = 0;
int ammo_y = 0;
//enemy variables
int e_x = 0;
int e_dir = 1;
ArrayList <Bullet> bullets;
void setup() {
bullets = new ArrayList();
//set resolution
size(640, 480);
//set fps to 200
frameRate(200);
}
class Bullet{
int x;
int y;
Bullet(int tx, int ty) {
x = tx;
y = ty;
}
void display() {
d.pixel(x, y, -1);
println(x,y);
}
void move() {
y -= 1;
if(y<1) {
bullets.remove(0);
}
//println(e_x);
if(y<18) {
if((x >= e_x) && (x <= e_x+8)) {
println("HIT");
exit();
}
}
}
void movebullets() {
for(Bullet ammo : bullets) {
move();
}
}
}
void draw() {
/**
tests for the commands in class draw
d.circle(320,240, 20, color(0,255,0));
d.pixel(320, 240, color(255,0,0));
d.bitmap(ship,320,240,8,8);
*/
//run the space invaders test code every cycle
playdatgame();
}
//space invaders prototype void
void playdatgame() {
if (gamestarted == true) {
d.bg(0);
a_x = hw.getMouseX();
if(hw.pressed()) {
if(shootammo == false) {
//shootammo = true;
Bullet ammo = new Bullet(a_x+4,450);
bullets.add(ammo);
ammo.movebullets();
//ammo.move();
ammo.display();
//ammo_x = a_x+4;
//ammo_y = 450;
}
}
if(shootammo == true) {
//d.pixel(ammo_x, ammo_y, -1);
ammo_y--;
if(ammo_y<0) {
shootammo = false;
}
//println(e_x);
if(ammo_y<18) {
if((ammo_x >= e_x) && (ammo_x <= e_x+8)) {
println("HIT");
exit();
}
}
}
d.bitmap(ship,a_x,450,8,8);
d.bitmap(enemy,e_x,10,8,8);
if(e_dir == -1) {
e_x--;
}
if(e_dir == 1){
e_x++;
}
if(e_x>width-8) {
e_dir = -1;
}
if(e_x<0) {
e_dir = 1;
}
}
}
//hardware control class
public class hw {
int getMouseX() {
return mouseX;
}
boolean pressed() {
return mousePressed;
}
}
//drawing control class
public class draw {
//pixel drawing void
void pixel(int px, int py, color pcolor) {
stroke(pcolor);
point(px, py);
}
//circle drawing void
void circle(int cx, int cy, int crad, color ccolor) {
fill(bgcolor);
stroke(ccolor);
ellipse(cx, cy, crad, crad);
}
//background color control void (wipes everything to bg color)
void bg(color bgc) {
background(bgc);
bgcolor = bgc;
}
//bitmap control void (uses 2d arrays to get the image)
void bitmap(int[][] array, int x, int y, int w, int h) {
for (int xc=0; xc<w; xc++) {
for (int yc=0; yc<h; yc++) {
//print(array[xc][yc]);
stroke(color(array[yc][xc]));
point(xc+x, yc+y);
}
}
}
}