Hello I am creating the famouse Space Invaders game for an exam and I wanted to create the horizontally and vertically movement like in the real game and so I wanted to ask if somebody could help me doing it please ?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Row here. * * @author (your name) * @version (a version number or a date) */ public class Row extends Actor { /** * Act - do whatever the Row wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Row() { // Add your action code here. } public boolean moveRow(int dir) { boolean changeDir = false; for (Object obj : getIntersectingObjects(Alien.class)) { Alien alien = (Alien) obj; changeDir = alien.moveAlien(dir) || changeDir; } return changeDir; } public void dropRow() { for (Object obj : getIntersectingObjects(Alien.class)) { Actor alien = (Alien) obj; alien.setLocation(alien.getX(),alien.getY()+10); } setLocation(getX(), getY()+10); }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Alien here. * * @author (your name) * @version (a version number or a date) */ public class Alien extends Actor { private int timer = 2+Greenfoot.getRandomNumber(500); public Alien() { setRotation(0); } /** * Act - do whatever the Alien wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Actor bullet = getOneIntersectingObject(Bullet.class); //variable qui retourne la classe bullet if(bullet!= null) { World world = getWorld(); MyWorld myWorld = (MyWorld)world; Counter counter = myWorld.getCounter(); counter.addScore(); getWorld().removeObject(this); } if (--timer==0){ shoot(); timer = 2+Greenfoot.getRandomNumber(500); } } public void shoot() { getWorld().addObject(new AlienBullet(), getX(), getY()); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Row here. * * @author (your name) * @version (a version number or a date) */ public class Row extends Actor { /** * Act - do whatever the Row wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Row() { // Add your action code here. } public boolean moveRow(int dir) { boolean changeDir = false; for (Object obj : getIntersectingObjects(Alien.class)) { Alien alien = (Alien) obj; changeDir = alien.moveAlien(dir) || changeDir; } return changeDir; } public void dropRow() { for (Object obj : getIntersectingObjects(Alien.class)) { Actor alien = (Alien) obj; alien.setLocation(alien.getX(),alien.getY()+10); } setLocation(getX(), getY()+10); } }