Hallo zusammen
Ich versuche zur Zeit ein Jump and Run zu programmieren. Im Moment bin ich dabei den Sprung zu programmieren, jedoch wird er nicht angezeigt. Gerechnet wird er zwar jedoch bewegt sich mein Männchen nicht.
Ich habe hier einmal den ganzen Code den ich geschrieben habe hingepackt, jedoch denke ich sind nur die Klassen "Jump" und "Label" wichtig.
Vielen Dank für eure Hilfe schon einmal im Voraus.
Ich versuche zur Zeit ein Jump and Run zu programmieren. Im Moment bin ich dabei den Sprung zu programmieren, jedoch wird er nicht angezeigt. Gerechnet wird er zwar jedoch bewegt sich mein Männchen nicht.
Ich habe hier einmal den ganzen Code den ich geschrieben habe hingepackt, jedoch denke ich sind nur die Klassen "Jump" und "Label" wichtig.
Vielen Dank für eure Hilfe schon einmal im Voraus.
Java:
package pack1;
public class Main {
public static void main(String[] args) {
new GUI();
new var();
new Label();
new KeyHandler();
new movement();
}
}
package pack1;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GUI{
public GUI() {
var.f1 = new JFrame();
var.f1.setVisible(true);
var.f1.setSize(var.screenwidth,var.screenhight);
var.f1.setTitle("Jump and Run");
var.f1.setLocationRelativeTo(null);
var.f1.setDefaultCloseOperation(var.f1.EXIT_ON_CLOSE);
var.f1.setLayout(null);
var.f1.setResizable(false);
var.f1.requestFocus();
var.f1.addKeyListener(new KeyHandler());
var.Label = new Label();
var.Label.setBounds(0, 0, var.screenwidth, var.screenhight);
var.Label.setVisible(true);
var.f1.add(var.Label);
}
}
package pack1;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JLabel;
public class Label extends JLabel{
int sprungposition;
public void paintComponent (Graphics g) {
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
var.xPosition1 = var.X_background+var.xPositionBild2-var.screenwidth;
var.xPosition2 = var.X_background+var.xPositionBild2;
sprungposition = var.Y_Figur;
System.out.println(sprungposition);
g2d.drawImage(var.img1,var.xPosition1,0,null); //Hintergrund
g2d.drawImage(var.img1,var.xPosition2,0,null); //Hintergrund
g2d.drawImage(var.img2,var.X_Figur,sprungposition,null); // Männchen
repaint();
}
}
package pack1;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyHandler implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_LEFT) {
var.moveleft = true;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
var.moveright = true;
}
if(e.getKeyCode()==KeyEvent.VK_UP) {
Jump x = new Jump();
x.run();
}
if(e.getKeyCode()==KeyEvent.VK_ESCAPE) {
System.exit(0);
}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_LEFT) {
var.moveleft = false;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
var.moveright = false;
}
}
}
package pack1;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class movement implements ActionListener {
Timer Timer;
boolean highpoint = false;
boolean startpoint = true;
int Jumphigh = -70;
int Jumpposition=0;
int Aufsprunggeschwindigkeit = 1;
public movement() {
Timer = new Timer(2,this);
Timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
if(var.X_background==var.xPositionBild2*-1) {
var.xPositionBild2 += var.screenwidth;
}
//Zunahme xPositionBild
if(var.moveright == true && var.X_Figur >= var.screenwidth-200) {
var.X_background -= 1;
}//Hintergrundbewegung Bild
if(var.X_Figur <= var.screenwidth-200) {
if(var.moveright == true) {
var.X_Figur +=1;
}}
if(var.moveleft == true && var.X_Figur>-50) {
var.X_Figur -= 1;
}//Bewegung Figur
//Jump
/*if(var.moveup==true) {
while(highpoint==false) {
try {
Jumpposition--;
var.Y_Figur--;
Thread.sleep(10);
System.out.println(var.Y_Figur);
if (Jumpposition==Jumphigh) {
highpoint=true;
Jumpposition = 0;
}
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
System.out.println(var.Y_Figur);
}
*/
}}
package pack1;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class var {
static JFrame f1;
static int screenwidth=1300 ,screenhight=780;
static int X_Figur;
static int Y_Figur = 430;
static int X_background=0;
static int xPositionBild2;
static int test;
static Label Label;
static boolean moveleft=false,moveright=false, moveup=false;
static Image img1,img2;
static int xPosition1;
static int xPosition2;
public var(){
ImageIcon u = new ImageIcon(getClass().getResource("Bild.jpg"));
img1 = u.getImage();
ImageIcon x = new ImageIcon(getClass().getResource("figur.png"));
img2 = x.getImage();
}
}
package pack1;
public class Jump implements Runnable{
boolean highpoint = false;
boolean startpoint = true;
int Jumphigh = -70;
int Jumpposition=0;
int Aufsprunggeschwindigkeit = 1;
@Override
public void run() {
while(highpoint==false) {
var.Y_Figur--;
Jumpposition--;
try {
Thread.sleep(10);
if (Jumpposition==Jumphigh) {
highpoint=true;
startpoint=false;
Jumpposition=0;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while(startpoint==false) {
var.Y_Figur++;
Jumpposition--;
try {
Thread.sleep(10);
if (Jumpposition==Jumphigh) {
startpoint=true;
highpoint =false;
Jumpposition=0;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}}