# really confused; question on a game I made

Printable View

• 01-15-2013, 12:40 AM
MW130
really confused; question on a game I made
hey guys; I got really ahead of my level, and was looking through a swing book in Java, and within it the Guy made a game.. I copied his code ( to learn) but he never included a main method, or where to execute it... Is this code supposed to be a weblet? How should I go about executing it...? Thanks so much... heres the code:

Code:

```import java.awt.Shape; public class BaseVectorShape { private Shape shape; private boolean alive; private double x,y; private double velX, velY; private double moveAngle, faceAngle; public Shape getShape() { return shape; } public boolean isAlive() { return alive; } public double getX() {return x;} public double getY() {return y;} public double getMoveAngle() {return moveAngle;} public double getFaceAngle() {return faceAngle;} public void setShape(Shape shape){this.shape = shape;} public void setAlive(boolean alive) {this.alive = alive;} public void setX(double x) {this.x =x;} public void incX(double i) {this.x += i;} public void setY(double y) {this.y = y;} public void incY(double i) {this.y=y+1;} public void setVelX(double i) {this.y = y;} public void incVelX(double i){this.y+=i;} public void setVelY(double i) { this.y = y;} public void setFaceAngle(double angle) {this.faceAngle = angle;} public void incFaceAngle(double i) { this.faceAngle += i;} public void setMoveAngle(double angle) {this.moveAngle = angle;} public void incMoveAngle(double i) {this.moveAngle += i;} BaseVectorShape(){ setShape(null); setAlive(false); setX(0.0); setY(0.0); setVelX(0.0); setVelY(0.0); setMoveAngle(0.0); setFaceAngle(0.0);   } } import java.awt.Polygon; import java.awt.Rectangle; public class Ship extends BaseVectorShape { private int[] shipx = { -6, -3, 0, 3, 6, 0}; private int[] shipy = { 6, 7, 7, 7, 6, -7}; public Rectangle getBounds() { Rectangle r; r = new Rectangle((int)getX() - 6, (int) getY() -6, 12, 12); return r; } Ship() { setShape(new Polygon(shipx, shipy, shipx.length)); setAlive(true);     } } import java.awt.*; import java.awt.Rectangle; public class Bullet extends BaseVectorShape { public Rectangle getBounds() { Rectangle r; r = new Rectangle((int)getX(), (int) getY(), 1, 1); return r; } Bullet() { setShape(new Rectangle(0, 0, 1, 1)); setAlive(false); } } import java.awt.Polygon; import java.awt.Rectangle; public class Asteroid extends BaseVectorShape { private int[] astx = {-20, -13, 0 , 20 , 22, 20, 12, 2 , -10, -22, -16}; private int[] asty = {20, 23, 17, 20, 16, -20, -22, -14, -17, -20, -5}; protected double rotVel; public double getRotationVelocity() { return rotVel;} public void setRotationVelocity(double v) { rotVel = v;} public Rectangle getBounds() { Rectangle r; r = new Rectangle((int)getX() - 20, (int) getY() - 20, 40, 60); return r; } Asteroid() { setShape(newPolygon(astx, asty, astx.length)); setAlive(true); setRotationVelocity(0.0);   } } import java.applet.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.*; import java.util.*; public class Asteroid extends Applet implements Runnable, KeyListener { Thread gameloop; BufferedImage backbuffer; Graphics g2d; boolean showBounds = false; int ASTEROIDS = 20; Asteroids[] ast = new Asteroid[ASTEROIDS]; int BULLETS = 10; Bullet[] bullet = new Bullet[BULLETS]; int currentBullet = 0; Ship ship = new Ship(); AffineTransform identity = new AffineTransform(); Random rand = new Random(); public void init() { backbuffer = new BufferedImage(640, 480, BufferedImage.TYPE_INT__RGB); g2d = backbuffer.creatGraphics(); ship.setX(320); ship.setY(240); for (int n= 0; n<BULLETS; n++) { ast[n] = new Asteroid(); ast[n].setRotationVelocity(rand.nextInt(3)+1); ast[n].setX((double)rand.nextInt(600) + 20); ast[n].setY((double)rand.nextInt(440)+20); ast[n].setMoveAngle(rand.nextInt(360)); double ang = ast[n].getMoveAngle() - 90; ast[n].setVelX(calcAngleMoveX(ang)); ast[n].setVelY(calcAngleMoveY(ang)); } addKeyListener(this); } public void drawShip() { g2d.setTransform(identity); g2d.translate(ship.getX(), ship.getY()); g2d.rotate(Math.toRadians(ship.getFaceAngle())); g2d.setColor(Color.ORANGE); g2d.fill(ship.getShape()); } public void drawBullets() { for(int n = 0; n<BULLETS; n++) { if (bullet[n].isAlive()) { g2d.setTransform(identity); g2d.translate(bullet[n].getX(), bullet[n].getY()); g2d.setColor(Color.MAGENTA); g2d.draw(bullet[n].getShape());     }   } } public void drawAsteroids() { for (int n= 0; n< ASTEROIDS; n++) { if (ast[n].isAlive()) { g2d.setTransform(identity); g2d.translate(ast[n].getX(), ast[n].getY()); g2d.rotate(Math.toRadians(ast[n].getMoveAngle())); g2d.setColor(Color.DARK_GRAY); g2d.fill(ast[n].getShape());   }   } } public void paint(Graphics g) { g.drawImage(backbuffer, 0, 0, this); } public void start() { gameloop = new Thread(this); gameloop.start(); } public void run() { Thread t = Thread.currentThread(); while( t == gameloop) { try { gameUpdate(); Thread.sleep(20); } catch(InterruptedException e) { e.printStackTrace(); } e.printStackTrace(); } repaint();   } public void stop() { gameloop = null; } private void gameUpdate() { updateShip(); updateBullets(); updateAsteroids(); checkCollisions(); } public void updateShip() { ship.incX(ship.getVelX()); if (ship.getX() < -10) ship.setX(getSize().width + 10); else if (ship.getX() > getSize().width + 10) ship.setX(-10); ship.incY(ship.getVelY()); if (ship.getY() < -10) ship.setY(getSize().height + 10); else if (ship.getY() > getSize().height + 10)     ship.setY(-10); } public void updateBullets() { for (int n = 0; n< BULLETS ; n++) { if (bullet[n].isAlive()) { bullet[n].incX(bullet[n].getVelX()); if (bullet[n].getX() < 0 || bullet[n].getX() > getSize().width) { bullet[n].setAlive(false); } bullet[n].incY(bullet[n].getVelyY()); if(bullet[n].getY() < 0 || bullet[n].getY() > getSize().height) { bullet[n].setAlive(false);           }       }     }   } public void updateAsteroids() { for (int n = 0; n < ASTEROIDS; n++) { if(ast[n].isAlive()){ ast[n].incX(ast[n].getVelX()); if(ast[n].getX() < -20) ast[n].setX(getSize().width + 20); else if (ast[n].getX() > getSize().width + 20) ast[n].setX(-20); ast[n].incY(ast[n].getVelY()); if(ast[n].getY() < -20) ast[n].setY(getSize().height + 20); else if(ast[n].getY() > getSize().height + 20) ast[n].setY(-20); ast[n].incMoveAngle(ast[n].getRotationVelocity()); if(ast[n].getMoveAngle() < 0) ast[n].setMoveAngle(360 - ast[n].getRotationVelocity()); else if (ast[n].getMoveAngle() > 360) ast[n].setMoveAngle(ast[n].setMoveAngle(ast[n].getRotationVelocity()));   }   } } public void checkCollisions() { for (int m=0; M<ASTEROIDS;m++) { if(ast[m].isAlive()) { for(int n = 0; n< BULLETS; n++) { if (bullet[n].isAlive()) { if (ast[m].getBounds().contains( bullet[n].getX(), bullet[n].getY())) { bullet[n].setAlive(false); continue;   }  } } if(ast[m].getBounds().intersects(ship.getBounds())) { ast[m].setAlive(false); ship.setX(320); ship.setY(240); ship.setFaceAngle(0); ship.setVelX(0); ship.setVelY(0); continue;      }   }  } } //key listener events public void keyReleased(KeyEvent k) {} public void keyTyped(KeyEvent k) {} public void keyPressed(KeyEvent k) {} int keyCode = k.getKeyCode(); switch (keyCode) { case KeyEvent.VK_LEFT: ship.incFaceAngle(-5); if(ship.getFaceAngle() < 0) ship.setFaceAngle(360-5); break; case KeyEvent.VK_RIGHT: ship.incFaceAngle(5); if(ship.getFaceAngle() > 360) ship.setFaceAngle(5); break; case KeyEvent.VK_UP: ship.setMoveAngle(ship.getFaceAngle() - 90); ship.incVelX(calcAngleMoveX(ship.getMoveAngle()) * 0.1); ship.incVelY(calcAngleMoveY(ship.getMoveAngle()) * 0.1); break; case KeyEvent.VK_CONTROL: case KeyEvent.VK_ENTER: case KeyEvent.VK_SPACE: currentBullet++; if(currentBullet > BULLETS -1) currentBullet = 0; bullet[currentBullet].setAlive(true); bullet[currentBullet].setX(ship.getX()); bullet[currentBullet].setY(ship.setY()); bullet[currentBullet].setMoveAngle(ship.getFaceAngle()-90); double angle = bullet[currentBullet].getMoveAngle(); double svx = ship.getVelX(); double svy = ship.getVelY(); bullet[currentBullet].setVelX( svx + calcAngleMoveX(angle) *2); bullet[currentBullet].setVelY( svy + calcAngleMoveY(angle) *2); break;   } } public double calcAngleMoveX(double angle) { return (double) (Math.cos(angle * Math.PI / 180)); } public double calcAngleMoveY(double angle) { return (double) (Math.sin(angle * Math.PI / 180)); } }```
• 01-15-2013, 01:14 AM
Fubarable
Re: really confused; question on a game I made
Quote:

Originally Posted by MW130
hey guys; I got really ahead of my level, and was looking through a swing book in Java, and within it the Guy made a game...

There is no Swing code in the code you've posted.

Quote:

I copied his code ( to learn) but he never included a main method, or where to execute it... Is this code supposed to be a weblet? How should I go about executing it...?
It's an Applet since one class extends Applet. Check out the tutorials on this.

Quote:

Thanks so much... heres the code:
Next time you post a gazillian lines of code, please have the courtesy to format it nicely. No one wants to read mostly left-justified code.
• 01-15-2013, 01:15 AM
Norm
Re: really confused; question on a game I made
Quote:

How should I go about executing it.
You could add a main() method to the class and use the java command with the name of the class with the main() method to start its execution.

BTW The code needs proper formatting. All the statements should NOT start in the first column. That makes it very hard to read and understand. See:
Code Conventions for the Java Programming Language: Contents
• 01-15-2013, 02:10 AM
MW130
Re: really confused; question on a game I made
Should I take off the extends applet?
• 01-15-2013, 02:11 AM
MW130
Re: really confused; question on a game I made
Quote:

Originally Posted by Norm
You could add a main() method to the class and use the java command with the name of the class with the main() method to start its execution.

BTW The code needs proper formatting. All the statements should NOT start in the first column. That makes it very hard to read and understand. See:
Code Conventions for the Java Programming Language: Contents

Hi norm, but what would I put inside of the main method as to execute it..? Thanks
• 01-15-2013, 02:43 AM
Fubarable
Re: really confused; question on a game I made
Please go through the tutorials and a text book on Java soon. You are increasing the frustration level of this forum and wasting your time and ours asking many basic questions that will all be answered if you just learn the basics, as has been recommended to you again and again.
• 01-15-2013, 02:59 AM
MW130
Re: really confused; question on a game I made
Quote:

Originally Posted by Fubarable
Please go through the tutorials and a text book on Java soon. You are increasing the frustration level of this forum and wasting your time and ours asking many basic questions that will all be answered if you just learn the basics, as it has been recommended to you again and again.

But I did check the tutorials you gave me. I went through them , and now I'm trying to do swing again... How else would I learn if not by actually coding...? ):
• 01-15-2013, 03:01 AM
Norm
Re: really confused; question on a game I made
Quote:

looking through a swing book in Java, and within it the Guy made a game.. I copied his code
Go back and read the book to see what the code does and how to use it.

Don't skip over learning the basics. Do as Fubarable recommended and start learning the basics.
• 01-15-2013, 03:14 AM
MW130
Re: really confused; question on a game I made
Quote:

Originally Posted by Norm
Go back and read the book to see what the code does and how to use it.

Don't skip over learning the basics. Do as Fubarable recommended and start learning the basics.

I do, but he even says specifically in the book; "You are not expected to understand all of this; the code is to give you a glimpse as to what we will be covering in further chapters." I was only really sad because I literally spent my whole day writing the code and analyzing it, only to find that I can't even use it, and all the effort was for no return. I just want to try out the game that I spent my whole day trying to make. I know what you guys mean when you say it, and I can only imagine how frustrating it is to teach people who are extremely new to the language when you guys know so much, but I'm only asking that you help me figure out how to execute this code so that I can go to rest knowing that my effort yielded results. I hope you understand. Regards, Michael
• 01-15-2013, 03:16 AM
Norm
Re: really confused; question on a game I made
Start with something simpler and build your knowledge first before attempting to copy a 300 line program and get it to work.
• 01-15-2013, 03:25 AM
MW130
Re: really confused; question on a game I made
Quote:

Originally Posted by Norm
Start with something simpler and build your knowledge first before attempting to copy a 300 line program and get it to work.

I know, but as I said, this code was just to give us a taste of what we will be learning in the book. He explicitly says that we aren't expected to understand it all. Nor is it my code; he does it, and I will downgrade right after; I just want to try this code and see it work and feel the joy of actually having done something and seeing something on the screen move because of my actions :P. Could you please help me this once, and then I will be out of your hair? I just dont know what to execute inside of the main method >.< If you'd like, I can reformat the code?
• 01-15-2013, 03:34 AM
Norm
Re: really confused; question on a game I made
There is no guarantee that the code will work. It could be a big waste of time trying to fix something full of problems.

If the text book doesn't explain how to execute the code, you'll have to wait until someone comes along that wants the challenge of working on this unformatted code.
• 01-15-2013, 03:41 AM
MW130
Re: really confused; question on a game I made
Okay.