Help! trying to create 'space invaders'
hi. im a high school student with a little knowledge of java. im am currently working on a class assignment where each student has to create their own program. i decided to create the classic game of 'Space Invaders'. But i have a problem; i can't figure out how i will get the characters to move.
does anyone if there is a method that will allow the user to move a character? for example, in C++ there is a conio.h library that has a goto(x,y) function. is there one for java as well? thanks alot!
This is another way
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Si extends JFrame
Timer ta;
myView view = new myView();
public Si()
setBounds(3,10,765,350);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent ev)
{ dispose();
System.exit(0);
getContentPane().add("Center",view);
setVisible(true);
ta = new Timer(45, new ActionListener()
{ public void actionPerformed(ActionEvent e)
view.move();
ta.start();
public class myView extends JComponent
Point invs[] = new Point[15];
Color invc[] = new Color[15];
Color color[] = new Color[]{
Color.blue, Color.cyan, Color.green, Color.magenta,
Color.orange, Color.pink, Color.red, Color.yellow};
public myView()
for (int p=0; p < invs.length; p++)
int x = (int)(Math.random()*600)+33;
int y = (int)(Math.random()*200)+33;
invs[p] = new Point(x,y);
int c = (int)(Math.random()*8);
invc[p] = color[c];
public void move()
for (int p=0; p < invs.length; p++)
repaint(invs[p].x-1,invs[p].y-1,20,18);
int x = (int)(Math.random()*3)-1;
int y = (int)(Math.random()*3)-1;
invs[p].translate(x,y);
repaint(invs[p].x-1,invs[p].y-1,20,18);
public void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
for (int p=0; p < invs.length; p++)
g.setColor(invc[p]);
g.fillOval(invs[p].x,invs[p].y,17,13);
public static void main (String[] args)
new Si();
} Noah
Similar Messages
-
Help trying to create a button in safari please.
trying to create a button that runs an applescript. I have been discussing it in 101 but cross posting it here because it's not getting much interest over there.
Any help would be nice. I have read Become an xCoder and it doesn't seem to answer my questions.
http://discussions.apple.com/thread.jspa?threadID=1514799&tstart=0Is there no way to create an appcontroller that works like an applescript? Or get an applescript to be recognized so a new NSButton added to the Bookmark bar be activated?
i.e.
*tell application "System Events"*
* tell application "Safari" to activate*
* keystroke "n" using {command down}*
* delay 0.1*
* keystroke "l" using {command down, shift down}*
*end tell*
Or,
_Something Like:_
/ AppController /
*#import <Cocoa/Cocoa.h>*
*@interface AppController : NSObject*
*IBOutlet id NSButton;*
*-(IBAction) autoFill:,delay 0.1,checkSpelling:(id)sender*
@end
I really don't know if that AppController looks anything like it should or if it's even close. -
Help trying to create multiple rectangles
Hi, I'm trying to create a five-in-a-row game, and I need lots of rectangles, forming a kind of a net. I've figured out a bit, but the thing is that every new rectangle seems to erase all the former ones, so in the end there's just the last one. I use this code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class luffare extends JApplet {
ruta rutor[] = new ruta[400];
public void init() {
for(int a=0;a<20;a++){
for(int b=0;b<20;b++){
rutor[a*20+b] = new ruta(a*10, b*10);
//rutor[a*20+b].setBackground(Color.white);
getContentPane().add(rutor[a*20+b]);
class ruta extends JPanel implements MouseListener, MouseMotionListener{
Rectangle rect;
public ruta(int xPos, int yPos){
rect = new Rectangle(xPos, yPos, 10, 10);
addMouseMotionListener(this);
addMouseListener(this);
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.draw(rect);
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
I'm really stuck here, can anybody help?
regards,
M?ns WideHey, I'm no Swing expert, actually I'm very much a beginner in Swing! I suggest you go through the Swing tutorials
{color:#0000ff}http://java.sun.com/docs/books/tutorial/uiswing/index.html{color}
and learn about the multitude of features that can be useful to you.
After that, if you have any problems related to Swing, please post in the Swing forum where you will get advice from real experts.
This works, using setBounds(...) with null Layout --I've added the position number to each Ruta (size increased to 20 X 20 for that) so you can see where they go.
file Luffare.javapackage manswide;
import java.awt.*;
import javax.swing.*;
public class Luffare extends JApplet
Ruta rutor[] = new Ruta[400];
public void init ()
setLayout (null);
for(int a = 0; a < 20; a++)
for(int b = 0; b < 20; b++)
rutor[a * 20 + b] = new Ruta (a * 20 + b);
getContentPane ().add (rutor[a * 20 + b]);
rutor[a * 20 + b].setBounds (a * 20, b * 20, 20, 20);
}file Ruta.javapackage manswide;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
class Ruta extends JPanel implements MouseListener, MouseMotionListener
Rectangle rect;
int id;
public Ruta (int newId)
id = newId;
setBorder (LineBorder.createBlackLineBorder ());
addMouseMotionListener (this);
addMouseListener (this);
public void paintComponent (Graphics g)
Graphics2D g2 = (Graphics2D)g;
g2.setFont (new Font("Arial", 1, 9));
g2.drawString (Integer.toString (id), 2, 10);
public void mouseReleased (MouseEvent e) {}
public void mousePressed (MouseEvent e) {}
public void mouseDragged (MouseEvent e) {}
public void mouseMoved (MouseEvent e) {}
public void mouseClicked (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
}It's good practise to place each java class in its own file.
db
-- To post code, use the code button or type the code tags -- [code]CODE[/code] is displayed as CODE -
Need help trying to create a ActiveX registered event
I am trying to create a register event for an ActiveX component. The problem I am having is creating the callback vi. When I right click on the VI ref and create the callback vi it is created with the user parameter as a variant. I would like this to be a control refnum. If I open the callback vi and replace the user parameter variant with a control refnum the wire from the callback vi to the VI ref is broken. How do I change the user parameter?
Joe is correct on this one. The callback where designed to take variants as their inputs, so that they are more flexible. In addition Variants are used through out ActiveX programming.
If you have a control reference that you want to pass into the VI, all you need to use is the "To Variant" function in the main VI to turn the reference into a variant. You can then inside the Callback VI you can use the Variant to Data VI to change the variant back to a reference; you simply where a constant of the reference type to the "Type" terminal.
Evan
National Instruments -
Help trying to create a 2 page indesign doc with 1 landscape & 1 portrait page?
I'm creating a double sided A5 flyer, how do I create a 2 page indesign document with one page portrait & the other landscape?
Thank you in advance for anyone who can help with this ;-)You can use the Page tool to change one, but if it's going to print I think it would be better to make them both either ladscape or portrait, then use Rotate Spread View in the Pages panel to make one appear inthe other orientation on screen while work.
-
I'm trying to create space on my ipad by moving photos to icloud
I Spent hours yesterday backing up everything to icloud. Today I'm learning about how to use icloud. I am trying to view my photos on icloud but I need an icloud photo app. Now here's my problem. I can't download the app because I have no space. How do I know if my photos have gone into icloud? I really don't want to lose my pictures. If they have gone to icloud would it automatically free up space on my ipad?
Sorry this message is a bit garbled, but I don't really know how to explain my probs.To the best of my knowledge, POP does not support the use of folders on the mail server. Those are only availble in IMAP or Microsoft Exchange accounts. Since the Mail app in iOS doesn't support creation of folders locally, there's no way to do this for your POP account that I know of.
Regards. -
Help -- Trying to create JAR file but not working
Hi everyone
I have created a rather noddy Java program that consists of 4 JComboBox's and 1 JButton on a JFrame.
When the button is clicked a text file is generate with the options that have been selected.
There are severial files that the program uses for this:
gcc.java
OutToFile.java
gccConfirm.java
MyNewWindow.java
main.java
I am using Jcreator and so must use the command line to create the Jar file.
I have never done a jar file before so I did an example with a hello world program and got it working now problem. I did the same for this program and the JAR file is generated without error. I double click on the JAR file to run the application, but nothing happens. I can see the application running in memory but nothing loads on the screen. However if I run it from the command line using Java -jar gcc.jar then everything is ok.
can anyone help me here?
thanks in advanceIf you run you .jar from the command line (a shell in unix speak) there
already is a console where your java program can send it's System.out
stuff. Right click on your .jar and select 'properties' (or similar). Note
that your .jar is run by 'javaw'. This executable is similar to the 'java'
executable except that it doesn't open a console for you. Change the
'javaw' command to 'java' and voila, there's your console again.
kind regards,
Jos -
Please help - Trying to create a realistic liquid morphing
Hello
I’m fairly new to Flash and vector art but have a
project to create a Flash based flowchart that is made up of liquid
blobs that morph to show the various paths to the chart.
I’ve had a go making the blobs in flash but when l go
to animate them, it all looks a bit rubbish - see:
http://www.mintmedia.co.uk/example2.html
Could anyone suggest a better way of attempting to do this
animation....
Thanks in advance
RichIs there no way to create an appcontroller that works like an applescript? Or get an applescript to be recognized so a new NSButton added to the Bookmark bar be activated?
i.e.
*tell application "System Events"*
* tell application "Safari" to activate*
* keystroke "n" using {command down}*
* delay 0.1*
* keystroke "l" using {command down, shift down}*
*end tell*
Or,
_Something Like:_
/ AppController /
*#import <Cocoa/Cocoa.h>*
*@interface AppController : NSObject*
*IBOutlet id NSButton;*
*-(IBAction) autoFill:,delay 0.1,checkSpelling:(id)sender*
@end
I really don't know if that AppController looks anything like it should or if it's even close. -
Help-need to create space!
this may seem like a stupid question but....
i need to free up disk space and have moved almost everything possible from my powerbookG4 but i still only have about 6.26GB out of 74.41 GB of space! 40.15GB are being taken up by an alias of my hard drive which is inside my network-servers folder, can i delete this copy? or can i delete other copies of my HD which are on my computer and i don't know the reason for them being there?
thanksSpecialist Entertainment:
Copied the Macintosh Hard Drive of my computer into the External Hard Drive.
Can you tell us exactly what you copied, and how you did it?
Then I copied all of the files in my home folder into the External Hard Drive.
Exactly what do you mean by "all of the files in my home folder"?
I then made a copy of the pictures, music and videos into the External Hard Drive.
Again, how did you do this?
I then reseted my computer and when I loaded it to my surprise the memory available was 1.48gb.
What do you mean by "I then reseted my computer"?
Can you tell us exactly what you see on your external HDD?
My guess is that you have duplicated some of the things that you have copied. Had you posted your question prior to attemting this project, we will have directed you to a much more efficient and effective way of doing it. Too late for that. Answering the questions above will give us an idea of what is going on, and we will be better able to direct you what to do next.
cornelius -
Need Help with Java Space Invaders Game!
Hi, im new to these forums so forgive if i dont follow the right way on laying things out. Im trying to create space invaders game but im stuck when im trying to create multiple aliens/ invaders. I have a class called Aliens (which is the invaders), and i can create an instance of an Alien. But when I try to create multiple instances they are not showing up on screen. I am not getting any error message though (which im hoping is a good sign, lol).
GamePanel is where all the action is and Alien is the alien (invaders) class.
Here is the code:
import java.awt.; import javax.swing.; import java.awt.Image; import java.awt.event.KeyListener; import java.awt.event.KeyEvent;
public class GamePanel extends JComponent implements KeyListener { SpaceInvaders game; static Player player1; Aliens alien1; static public Image spaceship2R; static public Image spaceship2L;
public GamePanel(SpaceInvaders game)
this.game = game;
player1 = new Player(this);
player1.start();
///// trying to create multiple instances of aliens here
for(int i=0; i<4; i++)
alien1 = new Aliens(this);
alien1.setX(i*100);
alien1.start();
this.setFocusable(true);
this.addKeyListener(this);
public void paintComponent(Graphics g)
Image pic1 = player1.getImage();
Image pic2 = alien1.getImage();
super.paintComponent(g);
g.drawImage(pic1, player1.getX(), player1.getY(), 50, 50,this);
g.drawImage(pic2, alien1.getX(), alien1.getY(), 50, 50,this);
}//end class
import java.awt.Image; import java.awt.*;
class Aliens extends Thread { //variables GamePanel parent; private boolean isRunning; private int xPos = 0, yPos = 0; Thread thread; static char direction = 'r'; Aliens alien; static public Image alien1; static public Image alien2;
public Aliens(GamePanel parent)
this.parent = parent;
isRunning = true;
public void run()
while(isRunning)
Toolkit kit = Toolkit.getDefaultToolkit();
alien1 = kit.getImage("alien1.gif");
alien2 = kit.getImage("alien2.gif");
try
thread.sleep(100);
catch(InterruptedException e)
System.err.println(e);
updateAliens();
parent.repaint();
public static Image getImage()
Image alienImage = alien1;
switch(direction){
case('l'):
alienImage = alien1;
break;
case('r'):
alienImage = alien1;
break;
return alienImage;
public void updateAliens()
if(direction=='r')
xPos+=20;
if(xPos >= SpaceInvaders.WIDTH-50||xPos < 0)
yPos+=50;
xPos-=20;
direction='l';
else
xPos-=20;
if(xPos >= SpaceInvaders.WIDTH-50||xPos < 0)
yPos+=50;
xPos+=20;
direction='r';
public void setDirection(char c){ direction = c; }
public void setX(int x){ xPos = x; }
public void setY(int y){ yPos = y; }
public int getX(){ return xPos; }
public int getY(){ return yPos; }
}//end classEdited by: deathwings on Oct 19, 2009 9:47 AM
Edited by: deathwings on Oct 19, 2009 9:53 AMMaybe the array I have created is not being used in the paint method, or Im working with 2 different objects as you put it? Sorry, Im just learning and I appreciate your time and help. Here is my GamePanel Class and my Aliens class:
import java.awt.*;
import javax.swing.*;
import java.awt.Image;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
public class GamePanel extends JComponent implements KeyListener
SpaceInvaders game;
static Player player1;
static Bullets bullet;
//static public Image spaceship2R;
//static public Image spaceship2L;
private BufferStrategy strategy;
static int alienNum =0;
static Aliens[] aliens;
public GamePanel(SpaceInvaders game)
this.game = game;
player1 = new Player(this);
player1.start();
Aliens[] aliens = new Aliens[10];
for(int i=0; i<4; i++)
aliens[i] = new Aliens(this);
aliens.setX(i*100);
aliens[i].start();
alienNum++;
initialiseGame();
this.setFocusable(true);
this.addKeyListener(this);
public void paintComponent(Graphics g)
Image pic1 = player1.getImage();
Image pic2 = aliens[0].getImage();
Image bulletPic = bullet.getImage();
super.paintComponent(g);
g.drawImage(pic1, player1.getX(), player1.getY(), 50, 50,this);
for ( int i = 0; i < alienNum; i++ )
g.drawImage(pic1, aliens[0].getX(), aliens[0].getY(), 50, 50,this);
//if (bullet.fired==true){
// g.drawImage(bulletPic, bullet.getX(), bullet.getY(), 20, 20,this);
public void keyPressed(KeyEvent k)
switch (k.getKeyCode()) {
case (KeyEvent.VK_KP_DOWN):
case (KeyEvent.VK_DOWN):
player1.setDirection('d');
break;
case (KeyEvent.VK_KP_UP):
case (KeyEvent.VK_UP):
player1.setDirection('u');
break;
case (KeyEvent.VK_KP_RIGHT):
case (KeyEvent.VK_RIGHT):
player1.setDirection('r');
break;
case (KeyEvent.VK_KP_LEFT):
case (KeyEvent.VK_LEFT):
player1.setDirection('l');
break;
case (KeyEvent.VK_SPACE):
fireBullet();
break;
public void keyTyped(KeyEvent k) {}//empty
public void keyReleased(KeyEvent k)
player1.setDirection('n');
public void initialiseGame()
public static void checkCollision()
Rectangle playerRec = new Rectangle(player1.getBoundingBox());
for(int i=0; i<alienNum; i++)
//if(playerRec.intersects(aliens[i].getBoundingBox()))
//collisionDetected();
public static void collisionDetected()
System.out.println("COLLISION");
public void fireBullet()
Bullets bullet = new Bullets(player1.getX(),player1.getY());
bullet.fired=true;
}//end GamePanelimport java.awt.Image;
import java.awt.*;
class Aliens extends Thread
//variables
GamePanel parent;
private boolean isRunning;
private int xPos = 0, yPos = 0;
Thread thread;
static char direction = 'r';
Aliens alien;
static public Image alien1;
static public Image alien2;
public Aliens(GamePanel parent)
this.parent = parent;
isRunning = true;
public void run()
while(isRunning)
Toolkit kit = Toolkit.getDefaultToolkit();
alien1 = kit.getImage("alien1.gif");
//alien2 = kit.getImage("alien2.gif");
try
thread.sleep(100);
catch(InterruptedException e)
System.err.println(e);
updateAliens();
parent.repaint();
public static Image getImage()
Image alienImage = alien1;
switch(direction){
case('l'):
alienImage = alien1;
break;
case('r'):
alienImage = alien1;
break;
return alienImage;
public void updateAliens()
if(direction=='r')
xPos+=20;
if(xPos >= SpaceInvaders.WIDTH-50||xPos < 0)
yPos+=50;
xPos-=20;
direction='l';
else
xPos-=20;
if(xPos >= SpaceInvaders.WIDTH-50||xPos < 0)
yPos+=50;
xPos+=20;
direction='r';
public Rectangle getBoundingBox()
return new Rectangle((int)xPos, (int)yPos, 50, 50);
public void setDirection(char c){ direction = c; }
public void setX(int x){ xPos = x; }
public void setY(int y){ yPos = y; }
public int getX(){ return xPos; }
public int getY(){ return yPos; }
}//end class -
How do i remove an Object (aliens/ bullets) from my Space Invaders game??
Creating Space Invaders game and im getting confused on removing objects from my game. How do you actually remove an object (i.e bullets and aliens).
I know this isnt the right way to do it, but for the bullets i had an array of the Objects bullets[] and set the x and y coordinates off screen so u cant see then wen they are painted you cant see them, then wen the player fires them, the x and y coordinates change so they come from the players ship till it hits an alien or goes off screen, then i reset the coordinates so they are pasted off screen again. Im relatively new to this so i would appreciate any help. Im going to have to do the same with aliens wen a collision is detected. Is there a way of actually removing an object?
Here is sum code incase you need to see it:
public void checkCollision()
Rectangle playerRec = new Rectangle(player1.getBoundingBox());
for(int i=0; i<alienRow; i++)
for(int j=0; j<alienCol; j++){
if(playerRec.intersects(aliens[i][j].getBoundingBox()))
collisionDetected();
for(int k=0; k<bulletNum; k++){
Rectangle bulletRec = new Rectangle(bullets[k].getBoundingBox());
if(bulletRec.intersects(aliens[i][j].getBoundingBox()))
removeBullet(bullets[k]);
collisionDetected();
public static void collisionDetected()
System.out.println("COLLISION");
private void removeBullet(Bullets bullet){
bullet.fired=false;
bullet.setX(-10);
bullet.setY(-10);
}Edited by: deathwings on Nov 25, 2009 8:20 AMdeathwings wrote:
I was thinking bout that arraylist angle before, but it makes sense now so i think that will work ok. Thx kevin.Not a problem.
Taking the List idea one step further, you could have a parent Object/Interface that all of your game objects extend/implement. Let's call it GameObject.
A GameObject would have a draw(Graphics g) function that you call from your paintComponent( ) method, and for example an act( ) function as well.
You'd have a single List of GameObjects that you add everything in your game into. Now painting is simply a matter of looping over a single List. And in your game logic loop, you could do a similar thing with the act( ) function: your bullet's act( ) would simply move it forward and check for collisions and going off screen. An alien's act( ) function would move it around and maybe shoot a weapon every so often.
Just an idea. -
Create space in iMovie app on ipad
I'm trying to create space on an ipad running iOS 7.0.4. I have a number of iMovie projects on it. I first shared two big projects with iTunes on my iMac, but then I discovered that I could not import the .iMovieMobile files that were created into my iMovie v10. Then I tried to "Save Video" in iMovie on the ipad, initially choosing a large format file. It got part way through then told me that there was not enough space to complete. So I tried a slightly smaller format file, and the same thing happened. Finally I tried the smallest format file and the same thing happened. Then I realized that these partially completed files appeared to be persisting on the ipad somewhere, because my free space went from about 8 GB to virtually nothing. I need to create space on the ipad by deleting the partial files fut have not figured out how to do this. I restarted the ipad, tried synching it (got error message), and tried hooking up to a different iMac. Does anyone know how to get rid of these? Thanks for any help.
on the iphone it is not possible to create albums you have to create them from you computer and sync it with itunes
this video will exactly show you how
http://www.youtube.com/watch?v=Xlzh856Guxg -
Trying to create a shell script to cut/paste files in finder. Help needed.
I'm trying to create an automator shell script to cut/paste. It'll function exactly like copy/paste. i.e. I'll just copy file/files with command+c like always, but then I'll create an automator which uses the "mv" terminal app to move the files which works exactly like cut paste.
I need some help since I don't know the syntax for creating shell scripts.
What I did so far is to do it in automator with Apple Script which goes like the following:
on run {input, parameters}
tell application "Finder"
set theWindow to window 1
set thePath to quoted form of (POSIX path of (target of theWindow as string))
end tell
tell application "Terminal"
do script with command "mv \"" & input & "\"" & thePath in window 1
end tell
return input
end run
This gets the copied file path from clipboard before, as input, and then recognizes the active finder window as thePath so then executes the mv command for the input file to the thePath window.
It doesn't work as expected since it connects both file/window paths into a single path instead of leaving a space between them so the mv command can't recognize two separate paths.
What's the correct syntax for that line
do script with command "mv \"" & input & "\"" & thePath in window 1
to leave a space between input and thePath under the mv command?
Also this requires the terminal app to be open in the background.
After I get this to work I want to do the exact same thing using shell script within automator, so I won't need Terminal to be open all the time.
And the next step will be to cut/paste multiple files/folders but that should be easy to do once I get the hang of it.Try using:
on run {input, parameters}
tell application "Finder"
set theWindow to window 1
set thePath to quoted form of (POSIX path of (target of theWindow as string))
end tell
do shell script "mv \"" & input & "\" " & thePath
return input
end run
(45977) -
Help with KeyDown (space invaders game)
I'm making a space invaders type game for school using Greenfoot (new Java IDE). Whenever I press space, a bullet will fire. However, multiple bullets are being created when I press space once, and when I hold space, a long stream of bullets are being made. I want one bullet to be created when I press space. Tried using KeyPress, but apparently Greenfoot does not recognize java.awt? Thanks in advance.
public class Ship extends Vehicle {
* Constructor for objects of class Shuttle
public Bullet B;
boolean canFire = false;
public Ship() {
GreenfootImage ship = new GreenfootImage("spaceship.gif");
this.setImage(ship);
//Create a bullet
public void fire(){
B = new Bullet();
getWorld().addObject(B,getX(),getY()-60);
public boolean collision(){
Actor enemy = getOneIntersectingObject(null);
return (enemy!=null) ? true : false;
public void act(){
// move with keys
if (this.canMove())
move();
if(Greenfoot.isKeyDown("space")){
fire();
else if(collision()){
getWorld().removeObject(this);
}Make a Class Variable to use as a flag indicating that a key was pressed... check to see if the flag is set, and if it is not, then fire a bullet and then set the flag. Reset the flag when they keyReleased action fires.
This way only one bullet can be fired for each time a key is pressed, provided your act() is being polled, but if a keyPressed fires act(), but not a keyRelease too, then it will not work. If it does not work, then you'll need to go to a regular KeyListener.
//Your code make these changes
public class Ship extends Vehicle {
* Constructor for objects of class Shuttle
public Bullet B;
boolean canFire = false;
public Ship() {
GreenfootImage ship = new GreenfootImage("spaceship.gif");
this.setImage(ship);
//Create a bullet
public void fire(){
B = new Bullet();
getWorld().addObject(B,getX(),getY()-60);
public boolean collision(){
Actor enemy = getOneIntersectingObject(null);
return (enemy!=null) ? true : false;
public void act(){
// move with keys
if (this.canMove())
move();
if(!GreenFoot.isKeyDown("space")) canFire = true; //add this
if(Greenfoot.isKeyDown("space") and canFire){
fire();
canFire = false; //add this
else if(collision()){
getWorld().removeObject(this);
} -
Space invaders! I need some help...!
Hello,
i'm trying to program a very simple version of space invaders. I'm drawing my moving objects (spaceship, rockets, aliens) with drawstring but I'm getting these compiling error: illegal start of expression.
Also I want to move the spaceship with translate is this OK to do so!?
Can somebody help me? Thanks for your help!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics2D;
//in deze klasse teken ik het ruimteschip en kan het links/rechts bewegen
public class Ruimteschip
//ruimteschip
Ruimteschip roderikschip = new Ruimteschip();
public void paint(Graphics ruimteschip)
{ //Ruimteschip weergeven
Graphics2D ruimteschip2D = (Graphics2D)ruimteschip;
String ruimteschip = "ruimteschip";
ruimteschip2D.drawString("ruimteschip", x, y,);
//beweegmethod
public void beweegRechts()
{ //teken opnieuw 1 coordinaat rechts
roderikschip.translate(1, 0);
ruimteschip2D.drawString("ruimteschip", x, y);
//beweegmethod
public void beweegLinks()
{ //teken opnieuw 1 coordinaat links
roderikschip.translate(-1, 0);
ruimteschip2D.drawString("ruimteschip", x, y);
}Hi and thanks for your replies.
I know drawString isn't the most beautiful way, but I'm making this game for an assignment, and I have to use drawString.
Still i'm getting "illegal start of expression" in my paint method...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics2D;
//in deze klasse teken ik het ruimteschip en kan het links/rechts bewegen
public class Ruimteschip
//ruimteschip
Ruimteschip ruimteschip = new Ruimteschip();
public void paint(Graphics ruimteschip)
{ //Ruimteschip weergeven
Graphics2D ruimteschip2D = (Graphics2D)ruimteschip;
String s = "ruimteschip";
*ruimteschip2D.drawString(s, x, y,);*
//beweegmethod
public void beweegRechts()
{ //teken opnieuw 1 coordinaat rechts
s.translate(1, 0);
ruimteschip.drawString(s, x, y);
//beweegmethod
public void beweegLinks()
{ //teken opnieuw 1 coordinaat links
s.translate(-1, 0);
ruimteschip.drawString(s, x, y);
} Edited by: Roddy on May 1, 2008 8:28 AM
Maybe you are looking for
-
Camera raw error in photoshop elements 11
I have PE11 with a Canon EOS Rebel T5 camera. When I try to open the raw image (CR2) in photo editor it get an error: "cannot open file because it appears to be from a camera model that is not supported by the installed version of camera raw" I have
-
How to change a SAP script output without changing Print Program
Hi, I want to change output of standard PO report printing object. I have to change the ZSAPSCRIPT but I have to keep the print program intact. please give me ideas.
-
Error in german language for sap cube,
getting below error when selected only single column form the sap cube. Error Details Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. URL: http://IPnumber:
-
I'm fairly to a little knowledgeable about video and converting and such ... mostly due to my experience putting video on my iPod classic. But lately I've been messing around with a new program that converts DVD directly to video (instead of using a
-
I am truly sorry if the following is going to be a "oh no not this again" issue. I am having an issue with 3 of my disks having "primary label corrupt: using backup" issues. Here is what I have tried and what has happened thusfar: first I tried forma