Reputation: 19
I am making a simple java applet game for my programming class and have run into another problem. In the game the user can use the up and down arrows to move between four "lanes" across the y-axis. Space will draw in image of a bullet moving across the x-axis down the lane and removes the bullet when it reaches a certain point on the x-axis. Right now I am trying to make it so that there can be more than one bullet on the screen at a time.
I've been watching a YouTube series by MyBringBack over game development in Java Applets and found the solution, kind of. I know that I should make the "player" an Object in a separate class allowing me to call multiple instances of it with ease. The problem is that in the YouTube series, he is using Shapes while I am using Images. I understand that there should not be too difficult if I've watched him do it, but I'm having a hard time determining which methods and variables I need in the Object class as well.
I figured since I had to do it for bullet I might as well for the other objects as well ("player","cow","fastZombie","tankyZombie",normalZombie", and "bloodSplat"). I started by trying to put everything that dealt with "player" in a separate class, however when I did, variables that were used in the original ZombieAttackMain class now could not be accessed by ZombieAttackMain and variables in ZombieAttackMain could not be accessed by the new class. I'm going to post the original code of ZombieAttackMain and if anyone has any suggestions, they would be greatly appreciated. Thank you!
Edit: Within the ZombieAttackMain I could no longer access PLAYER_X_POSITION and playerYPosition. While in the Player class i created it could not access POSITION_1_Y, POSITION_2_Y, POSITION_3_Y, or POSITION_4_Y.
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ZombieAttackMain extends Applet implements Runnable, KeyListener{
//all the comments with $ I removed and put in another class called Player and could not be accessed by ZombieAttackMain
//all the comments with # I copied and put in Player class
//all the comments with % could not be accessed by Player class
private Image background, normalZombie, fastZombie, tankyZombie,bullet, bloodSplat, cow1, cow2, cow3, cow4;
private Image player; //$
private Graphics bufferGraphics;
private Image offScreen;
private final int POSITION_1_Y = 100; //%
private final int POSITION_2_Y = 255; //%
private final int POSITION_3_Y = 400; //%
private final int POSITION_4_Y = 550; //%
private final int PLAYER_X_POSITION = 250; //$
private final int NORMAL_ZOMBIE_DX = -2;
private final int FAST_ZOMBIE_DX = -3;
private final int TANKY_ZOMBIE_DX = -1;
private final int BULLET_DX = 10;
private final int BULLET_STARTING_X_POSITION = 405;
private int zombieStartingXPosition = 1000;
private int playerYPosition = POSITION_3_Y; //$
private int zombieYPosition;
private int bulletXPosition, bulletYPosition;
private int bloodSplatX, bloodSplatY;
private boolean showBullet;
private boolean showFastZombie;
private boolean showTankyZombie;
private boolean showNormalZombie;
private boolean showCow;
public void init(){
setSize(1100, 700);
normalZombie = getImage(getCodeBase(), "NormalZombie.png");
tankyZombie = getImage(getCodeBase(), "TankyZombie.png");
fastZombie = getImage(getCodeBase(), "FastZombie.png");
player = getImage(getCodeBase(), "Player.png");
bullet = getImage(getCodeBase(), "Bullet.png");
bloodSplat = getImage(getCodeBase(), "BloodSplat.png");
background = getImage(getCodeBase(), "Background.jpg");
cow1 = getImage(getCodeBase(), "Cow.png");
cow2 = getImage(getCodeBase(),"Cow.png");
cow3 = getImage(getCodeBase(),"Cow.png");
cow4 = getImage(getCodeBase(),"Cow.png");
addKeyListener(this);
}
public void start() {
Thread thread = new Thread(this);
thread.start();
}
public void run() {
while(true){
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void update(Graphics g){
if(offScreen == null){
offScreen = createImage(this.getWidth(), this.getHeight());
bufferGraphics = offScreen.getGraphics();
}
bufferGraphics.setColor(getBackground());
bufferGraphics.fillRect(0,0,this.getWidth(),this.getHeight());
bufferGraphics.setColor(getForeground());
paint(bufferGraphics);
g.drawImage(offScreen,0,0,this);
}
public void paint(Graphics g){
g.drawImage(background,0,0,null);
g.drawImage(cow1,-15,115,null);
g.drawImage(cow2,-15,255,null);
g.drawImage(cow3,-15,400,null);
g.drawImage(cow4,-15,550,null);
g.drawImage(normalZombie,zombieStartingXPosition,100,null);
g.drawImage(tankyZombie,zombieStartingXPosition,400,null);
g.drawImage(fastZombie,zombieStartingXPosition,255,null);
g.drawImage(player,PLAYER_X_POSITION,playerYPosition, null); //$
g.drawImage(bloodSplat,bloodSplatX,bloodSplatY,null);
if(showBullet){
g.drawImage(bullet,bulletXPosition,bulletYPosition,this);
bulletXPosition += BULLET_DX;
if(bulletXPosition > 1100){
showBullet = false;
}
}
}
public void moveUp(Image player){ //$
if(playerYPosition == POSITION_2_Y){ //$
playerYPosition = POSITION_1_Y; //$
} //$
if(playerYPosition == POSITION_3_Y){ //$
playerYPosition = POSITION_2_Y; //$
} //$
if(playerYPosition == POSITION_4_Y){ //$
playerYPosition = POSITION_3_Y; //$
} //$
} //$
public void moveDown(Image player){ //$
if(playerYPosition == POSITION_3_Y){ //$
playerYPosition = POSITION_4_Y; //$
} //$
if(playerYPosition == POSITION_2_Y){ //$
playerYPosition = POSITION_3_Y; //$
} //$
if(playerYPosition == POSITION_1_Y){ //$
playerYPosition = POSITION_2_Y; //$
} //$
} //$
public void spawnBullet(){
if(showBullet){
return;
}
showBullet = true;
bulletXPosition = BULLET_STARTING_X_POSITION;
bulletYPosition = playerYPosition +83;
}
public void keyPressed(KeyEvent e) { //#
switch(e.getKeyCode()){ //#
case KeyEvent.VK_UP: //$
moveUp(player); //$
break; //$
case KeyEvent.VK_DOWN: //$
moveDown(player); //$
break; //$
case KeyEvent.VK_SPACE:
spawnBullet();
break;
}
}
public void keyReleased(KeyEvent e) { //#
} //#
public void keyTyped(KeyEvent e) { //#
} //#
public void stop() {
}
public void destroy() {
}
}
Upvotes: 0
Views: 208
Reputation: 2167
With the limited amount of information given with respect to which variables you cannot access. I do agree with notArefill in his believe that you do not access the variables in the right fashion. Example (if say you've got a seperate Zombie class):
public class Zombie{
private double x;
public void setX(double x){ // Setter
this.x = x;
}
public double getX(){ // Getter
return x;
}
}
Then when you need to refer to X field from Zombies class you would do something like this in your main or wherever you need to set it/get it:
Zombie zombie = new Zombie(); // Create a new instance of your zombie or if available call existing zombi(say if stored in some sort of a collection)
zombie.setX(140.13); // To set the X coordinate
zombie.getX(); // To retrieve the X coordinate
Hope that helps.
Upvotes: 0
Reputation: 814
It is correct that you need to make all this classes separate. The reason why the variables could not be accessed is probably because you set them as private.So, you should access them with the "getters". If you don't know how to make getters for your classes and if your are using eclipse you can go to source --> generate getters and setters. So for example if your Bullet class has the bulletXposition,then from another class when you create the Bullet object ,let's say you name it as "bullet" and you can call bullet.getBulletXPosition and so on. In my opinion you should read the basics first and then attempt to make a game.
Upvotes: 1