game not working
Author |
Message |
newvegasfan01
|
Posted: Sat Feb 12, 2011 10:45 am Post subject: game not working |
|
|
I created an asteroids game last summer and all of a sudden when I try to run it, it says that there is no main class so it won't run. Is there a reason why it says that any help is appreciated thanks |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Sat Feb 12, 2011 11:10 am Post subject: RE:game not working |
|
|
Are you missing a source file? |
|
|
|
|
|
newvegasfan01
|
Posted: Sat Feb 12, 2011 11:13 am Post subject: Re: game not working |
|
|
I don't think so because I haven't tried to run my game in a few months |
|
|
|
|
|
ProgrammingFun
|
Posted: Sat Feb 12, 2011 11:26 am Post subject: RE:game not working |
|
|
Maybe you should upload it so we can see what may be the problem... |
|
|
|
|
|
newvegasfan01
|
Posted: Sat Feb 12, 2011 11:29 am Post subject: Re: game not working |
|
|
here is my code
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import java.util.ArrayList;
import java.applet.AudioClip;
//import javax.sound.midi.Track;
public class astroids extends Applet implements KeyListener,ActionListener {
Image offscreen;
Graphics offg;
spacecraft player;
Timer gametimer;
boolean upkey,leftkey,downkey,rightkey;
boolean wkey,akey,skey,dkey;
ArrayList<astroid> asteroidlist;
ArrayList<bullets> bulletlist;
ArrayList<debris> debrislist;
AudioClip gunsound;
AudioClip explosion;
AudioClip music;
int score;
spacecraft player2;
int score2;
int level;
int bulletsfired;
int bulletsfired2;
String localhighscore;
file highscore;
boolean p2enabled;
boolean startscreen;
boolean pvpmode;
public void Init(){
asteroidlist=new ArrayList();
this.setSize(600,600);
this.addKeyListener(this);
gametimer=new Timer(20,this);
player= new spacecraft();
offscreen = createImage(600, 600);
offg = offscreen.getGraphics();
player.xpos= 250;
player.ypos= 250;
gunsound=getAudioClip(getCodeBase(),"laser80.wav");
explosion=getAudioClip(getCodeBase(),"explode1.wav");
music=getAudioClip(getCodeBase(),"freakynowclip2.wav");
music.stop();
music=getAudioClip(getCodeBase(),"WALK ALONE clip.wav");
music.play();
player2=new spacecraft();
player2.xpos= 240;
player2.ypos= 240;
level=1;
bulletlist=new ArrayList();
debrislist=new ArrayList();
highscore=new file();
localhighscore=highscore.read;
startscreen=true;
pvpmode = false;
asteroidlist.add (new astroid());
}
public int stringtoint(String s){
return Integer.parseInt(s);
}
public void Paint(Graphics g){
offg.setColor(Color.BLACK);
offg.fillRect(0,0,600,600);
offg.setColor(Color.red);
if (startscreen){
offg.drawString("Asteroids",(int)300,(int)100);
offg.drawString("Instructions",(int)300,(int)470);
offg.drawString("1 Player Press:1",(int)300,(int)450);
offg.drawString("2 Players Press:2",(int)300,(int)460);
offg.drawString("3 Toggle PVP " + pvpmode,(int)300,(int)480);
offg.drawString("",(int)300,(int)454);
}else{
for (int index=0; index<bulletlist.size(); index++)
{
bulletlist.get(index).draw (offg);
}
if (player.dead==false){
player.draw(offg);
}
if (player2.dead==false){
if (p2enabled == true){
player2.draw(offg);
}
}
if (player.dead==true){
offg.setFont(Font.getFont("Times New Roman"));
offg.drawString("press 'R' to respawn", 200,200);
}
if (player2.dead==true){
if(p2enabled){
offg.drawString("press 'F' to respawn", 200,250);
}
}
for(int index1=0; index1<asteroidlist.size(); index1++){
if (asteroidlist.get(index1).dead==false){
asteroidlist.get(index1).draw(offg);
}
}
for (int index3=0; index3<debrislist.size(); index3++){
if (debrislist.get(index3).dead==false){
debrislist.get(index3).draw(offg);
}
}
offg.drawString("player 1: "+score,(int) player.xpos - 30,(int) player.ypos + 45);
if(p2enabled){
offg.drawString("player 2: "+score2,(int) player2.xpos-30,(int)player2.ypos+45);
}
offg.drawString("High Score:"+localhighscore,(int)50,(int)40);
offg.drawString("Player 1: "+score,(int) 50,(int)50);
if (p2enabled){
offg.drawString("Player 2: "+score2,(int) 50,(int)60);
}
offg.drawString("Level:"+level,(int)50, (int)70);
offg.drawString("Bullets Fired Player1:"+bulletsfired,(int) 50,(int)80);
if (p2enabled){
offg.drawString("Bullets Fired Player2:"+bulletsfired2,(int) 50,(int)90);
}
}
//*************************************
g.drawImage(offscreen, 0, 0, this);
repaint();
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
rightkey=true;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
leftkey=true;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_UP){
upkey=true;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
downkey=true;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_W){
wkey=true;
}
if(e.getKeyCode()==KeyEvent.VK_A){
akey=true;
}
if(e.getKeyCode()==KeyEvent.VK_S){
skey=true;
}
if(e.getKeyCode()==KeyEvent.VK_D){
dkey=true;
}
if(e.getKeyCode()==KeyEvent.VK_R){
if (player.dead==true){
player.xpos=250;
player.ypos=250;
player.dead=false;
player.xspeed=0;
player.yspeed=0;
}
}
if(e.getKeyCode()==KeyEvent.VK_F){
if(player2.dead==true){
player2.xpos=225;
player2.ypos=225;
player2.dead=false;
player2.xspeed=0;
player2.yspeed=0;
}
}
if(e.getKeyCode()==KeyEvent.VK_Z){
asteroidlist.add(new astroid());
}
if (e.getKeyCode()==KeyEvent.VK_C){
player.dead=true;
}
if(e.getKeyCode()== KeyEvent.VK_SPACE){
if(player.guncd>20){
if (player.dead == false){
bulletlist.add(new bullets(player.angle, player.xpos, player.ypos,player));
bulletsfired=bulletsfired+1;
gunsound.play();
}
player.guncd=0;
}
}
if(e.getKeyCode()==KeyEvent.VK_V){
if(player2.guncd>20){
if(player2.dead==false){
bulletlist.add(new bullets(player2.angle,player2.xpos,player2.ypos,player2));
bulletsfired2=bulletsfired2+1;
gunsound.play();
}
player2.guncd=0;
}
}
if(e.getKeyCode()==KeyEvent.VK_X){
player2.dead=true;
}
if (e.getKeyCode()==KeyEvent.VK_1){
if (startscreen){
startscreen=false;
p2enabled=false;
}
}
if (e.getKeyCode()==KeyEvent.VK_2){
if(startscreen){
p2enabled=true;
startscreen=false;
}
}
if (e.getKeyCode()==KeyEvent.VK_3){
if(startscreen){
pvpmode = !pvpmode;
}
}
}
public void keyTyped(KeyEvent e){
}
public void Update(Graphics g){
paint(g);
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
rightkey=false;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
leftkey=false;
}
if(e.getKeyCode()==KeyEvent.VK_UP){
upkey=false;
}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
downkey=false;
}
if(e.getKeyCode()==KeyEvent.VK_W){
wkey=false;
}
if(e.getKeyCode()==KeyEvent.VK_A){
akey=false;
}
if(e.getKeyCode()==KeyEvent.VK_S){
skey=false;
}
if(e.getKeyCode()==KeyEvent.VK_D){
dkey=false;
}
}
public void actionPerformed(ActionEvent e){
if (pvpmode) asteroidlist.clear();
player.updateposition();
player.guncd=player.guncd +1;
keycheck();
if (p2enabled){
player2.updateposition();
player2.guncd=player2.guncd +1;
}
if (asteroidlist.size()==0){
levelup();
}
for(int index=0; index<asteroidlist.size(); index++){
asteroidlist.get(index).updateposition();
asteroidlist.get(index).rotateright();
if (asteroidlist.get(index).dead==true)
{
asteroidlist.remove(index);
}
}
for(int index=0; index<bulletlist.size(); index++){
bulletlist.get(index).updateposition();
bulletlist.get(index).rotateleft();
bulletlist.get(index).life=bulletlist.get(index).life-1;
if (bulletlist.get(index).life<0){
bulletlist.get(index).dead=true;
}
if (bulletlist.get(index).dead==true)
{
bulletlist.remove(index);
}
}
for(int index3=0; index3<debrislist.size(); index3++){
debrislist.get(index3).life-=1;
debrislist.get(index3).updateposition();
if (debrislist.get(index3).life<0){
debrislist.remove(index3);
}
}
checkcollisions();
if((score+score2)>stringtoint(localhighscore)){
highscore.FileWrite((score + score2));
highscore.FileRead();
localhighscore=highscore.read;
}
}
public void levelup(){
if (pvpmode == false){
level=level+1;
for(int i=0;i<level; i++){
asteroidlist.add(new astroid());
}
}
}
public void Start(){
gametimer.start();
}
public void Stop(){
gametimer.stop();
}
public boolean collide(sprite thing1,sprite thing2){
for(int index=0; index<thing1.shape.npoints; index++){
if(thing2.drawshape.contains(thing1.drawshape.xpoints[index],thing1.drawshape.ypoints[index])){
return true;
}
}
for(int index=0; index<thing2.shape.npoints; index++){
if(thing1.drawshape.contains(thing2.drawshape.xpoints[index],thing2.drawshape.ypoints[index])){
return true;
}
}
return false;
}
public void checkcollisions(){
if (pvpmode){
for (int index=0; index<bulletlist.size(); index++){
if(bulletlist.get(index).parent==player2){
if(collide(player, bulletlist.get(index))){
player.dead=true;
explosion.play();
}
}
if(bulletlist.get(index).parent==player){
if (collide(player2, bulletlist.get(index))){
player2.dead=true;
explosion.play();
}
}
}
}
for (int index = 0; index < asteroidlist.size(); index++){
if (collide(player, asteroidlist.get(index))){
player.dead = true;
}
if (p2enabled){
if (collide(player2, asteroidlist.get(index))){
player2.dead=true;
}
}
for (int index2 = 0; index2<bulletlist.size(); index2++)
{
if (collide (asteroidlist.get(index), bulletlist.get(index2))){
explosion.play();
for(int index3=0; index3<4; index3++)
{
debrislist.add(new debris(asteroidlist.get(index).xpos,asteroidlist.get(index).ypos));
}
if (asteroidlist.get(index).size!=1){
asteroidlist.add(new astroid(asteroidlist.get(index).xpos,asteroidlist.get(index).ypos,asteroidlist.get(index).size-1));
asteroidlist.add(new astroid(asteroidlist.get(index).xpos,asteroidlist.get(index).ypos,asteroidlist.get(index).size-1));
}
if (bulletlist.get(index2).parent == player){
score = score + 2;
}
if (bulletlist.get(index2).parent == player2){
score2 = score2 + 2;
}
bulletlist.remove(index2);
asteroidlist.get(index).dead=true;
}
}
}
}
public void keycheck(){
if(rightkey){
player.rotateleft();
}
if(leftkey){
player.rotateright();
}
if(upkey){
player.accelerate();
}
if(downkey){
player.decelerate();
}
if(wkey){
player2.accelerate();
}
if(akey){
player2.rotateright();
}
if(skey){
player2.decelerate();
}
if(dkey){
player2.rotateleft();
}
}
} |
|
|
|
|
|
Insectoid
|
Posted: Sat Feb 12, 2011 11:32 am Post subject: RE:game not working |
|
|
Do you see a main class in there? I don't. |
|
|
|
|
|
newvegasfan01
|
Posted: Sat Feb 12, 2011 11:36 am Post subject: Re: game not working |
|
|
It worked 4 months ago just fine |
|
|
|
|
|
Insectoid
|
Posted: Sat Feb 12, 2011 12:01 pm Post subject: RE:game not working |
|
|
You probably had a main class then. Likely in a different file. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
newvegasfan01
|
Posted: Sat Feb 12, 2011 12:04 pm Post subject: Re: game not working |
|
|
If I did I can't find it anywhere |
|
|
|
|
|
ProgrammingFun
|
Posted: Sat Feb 12, 2011 2:00 pm Post subject: RE:game not working |
|
|
Try looking thru you code and then make a main class appropriately... |
|
|
|
|
|
Insectoid
|
Posted: Sat Feb 12, 2011 2:13 pm Post subject: RE:game not working |
|
|
Java won't compile your program without a main class. Without a main class your program would do nothing anyway. Main is the class that executes at runtime. Everything happens inside main.
Something must have happened to it, or your program never ran. |
|
|
|
|
|
ProgrammingFun
|
Posted: Sat Feb 12, 2011 3:11 pm Post subject: RE:game not working |
|
|
This is why commenting your code is important....
Does your code work as a bunch of methods that link to each other (therefore the main class calling only to a method) or are there other things inside the main class?
You can use the answer of the above question to make a main class...
Java: |
public static void main (String[] args )
{
}
|
|
|
|
|
|
|
|
|