Hi,
ich bin grad dabei ein kleines Sidescroll Jump and Run Spiel zu programmieren. Jedoch komme ich nicht weiter. Der Char wird gezeichnet, jedoch bewegt er sich nicht und auch die Koordinaten verändern sich nicht, wenn ich die Pfeiltasten drücke. Mehr sollte er eigentlich auch nicht machen. Das Scrolling, Collision detection und so weiter kommt später.
Hier meine Klassen:
Main Class:
Player Class:
C_JR Class:
Ich bedanke mich schonmal für eure Hilfe.
ich bin grad dabei ein kleines Sidescroll Jump and Run Spiel zu programmieren. Jedoch komme ich nicht weiter. Der Char wird gezeichnet, jedoch bewegt er sich nicht und auch die Koordinaten verändern sich nicht, wenn ich die Pfeiltasten drücke. Mehr sollte er eigentlich auch nicht machen. Das Scrolling, Collision detection und so weiter kommt später.
Hier meine Klassen:
Main Class:
Java:
package JumpRunGame;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends Applet implements Runnable,KeyListener{
private Thread th;
private Image db_Image;
private Graphics db_g;
private boolean keyleft;
private boolean keyright;
private Image picchar;
private Player player;
public void init(){
setBackground(Color.BLUE);
player = new Player(100,200,this);
keyleft = false;
keyright = false;
getImages();
}
public void start(){
th = new Thread(this);
th.start();
}
public void run(){
while(true){
player.playermove();
repaint();
try{
Thread.sleep(14);
}catch(InterruptedException ex){}
}
}
public void stop(){
th.stop();
}
public void destroy(){
th.stop();
}
private void getImages(){
picchar = getImage(getCodeBase(), "Images/char/char.gif");
}
@Override
public void keyTyped(KeyEvent e){}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_A){
keyleft = true;
player.walkingleft(true);
}else if(e.getKeyCode() == KeyEvent.VK_B){
keyright = true;
player.walkingright(true);
}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT){
keyleft = false;
player.walkingleft(false);
}else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
keyright = false;
player.walkingright(false);
}
}
public void update(Graphics g){
if(db_Image == null){
db_Image = createImage(this.getSize().width, this.getSize().height);
db_g = db_Image.getGraphics();
}
db_g.setColor(getBackground());
db_g.fillRect(0, 0, this.getSize().width, this.getSize().height);
db_g.setColor(getForeground());
paint(db_g);
g.drawImage(db_Image,0,0,this);
}
public void paint(Graphics g){
player.paintPlayer(g);
}
}
Player Class:
Java:
package JumpRunGame;
import java.awt.*;
public class Player{
private final int WALKSPEED = 1;
private final int FALLSPEED = 1;
private int xposleft;
private int xposright;
private int yposup;
private int yposdown;
private boolean walkingleft;
private boolean walkingright;
private Image picchar;
private Component parent;
public Player(int x, int y, Component parent){
xposleft = x;
xposright = x + C_JR.player_image_width;
yposup = y;
yposdown = y + C_JR.player_image_height;
this.parent = parent;
}
public int getXPosLeft(){
return xposleft;
}
public int getXPosRight(){
return xposright;
}
public int getYPosUp(){
return yposup;
}
public int getYPosDown(){
return yposdown;
}
public void walkingleft(boolean value){
walkingleft = value;
}
public void walkingright(boolean value){
walkingright = value;
}
public void playermove(){
if(walkingleft){
xposleft -= WALKSPEED;
xposright -= WALKSPEED;
}else if(walkingright){
xposleft += WALKSPEED;
xposright += WALKSPEED;
}
}
public void paintPlayer(Graphics g){
g.drawImage(picchar, xposleft, yposup, parent);
g.drawString(xposleft + " " + yposup, 20, 20);
}
}
C_JR Class:
Java:
package JumpRunGame;
public class C_JR {
public static final int applet_height = 300;
public static final int applet_width = 300;
public static final int player_image_height = 12;
public static final int player_image_width = 10;
public static final int levelelement_height = 15;
public static final int levelelement_width = 15;
}
Ich bedanke mich schonmal für eure Hilfe.
Zuletzt bearbeitet von einem Moderator: