Hey Leute bis vor ein paar Tagen konnte ich quasi gar nichts was mit Java zu tun hat, jedeglich so Sachen wie schleifen Bedingungen und so, dann hab ich angefangen nach nem Tutorial ein Jump n Run zu programmieren, aber dieses hörte nach 4 Videos auf, sodass ich irgendwie versuche ein Jump n Run zu programmieren, der Code wird jetzt vielleicht in euren Augen scheußlich aussehen, aber ich bin zufrieden natürlich viele viele Sachen fehlen noch, aber jetzt stehe ich vor einem Problem bei dem ich nicht weiß wie ich es lösen soll. Wie krieg ich es hin, dass während mein Charakter läuft er gleichzeitig springen kann?
Hier ist der Code und bitte nicht zu sehr aufregen bin absoluter Neuling.
das ist nur kopiert vom tutorial
Hier ist der Code und bitte nicht zu sehr aufregen bin absoluter Neuling.
Java:
package mario;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.*;
public class Board extends JPanel implements ActionListener {
int x = 1;
int y = 1;
int k = 0;
int z = 0;
int px = 60;
int py = 60;
int key = 0;
int start = 1;
int zähler = 0;
int nx = 0;
int nx1 = 0;
int ny = 0;
int position = 0;
int steigey = 0;
int falley = 0;
int falley1 = 0;
int falley2 = 0;
boolean sprung = false;
boolean falle = false;
Timer time;
Image block;
Image luft;
Image grass;
Image pilzblock;
Image mario;
Image mario1;
int[][] Map = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,}
};
public Board () {
setFocusable(true);
ImageIcon a = new ImageIcon(getClass().getResource("block.png"));
block = a.getImage();
ImageIcon b = new ImageIcon(getClass().getResource("luft.png"));
luft = b.getImage();
ImageIcon c = new ImageIcon(getClass().getResource("grass.png"));
grass = c.getImage();
ImageIcon d = new ImageIcon(getClass().getResource("pilzblock.png"));
pilzblock = d.getImage();
ImageIcon e = new ImageIcon(getClass().getResource("mario.png"));
mario = e.getImage();
ImageIcon f = new ImageIcon(getClass().getResource("mario1.png"));
mario1 = f.getImage();
addKeyListener(new Al());
time = new Timer(5,this);
time.start();
}
public void actionPerformed(ActionEvent e){
berechne();
if(sprung ==false){
falle();
falle = true;
}
else {
steige();
}
repaint();
}
private void berechne() {
position = px/30;
for (int i =0; i<=14;i++){
if(Map[i][position]>0 && nx ==0){
falley1 = i*30-60;
nx = 1;
}
}
position = px/30+1;
for (int i =0; i<=14;i++){
if(Map[i][position]>0 && nx1 ==0){
falley2 = i*30-60;
nx1 = 1;
}
}
if (falley1 < falley2){
falley = falley1;
}
else {
falley = falley2;
}
if (nx1 ==0 && nx == 0){
falley =600;
}
nx = 0;
nx1 = 0;
}
private void falle() {
if(py<falley){
py+=5;
}
}
private void steige(){
if ( ny == 0){
steigey=py-100;
ny = 1;
}
if(py>steigey){
py-=5;
}
else {
sprung = false;
ny = 0;
}
}
public void paint (Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
for (int i =0; i<=14;i++){
for (int o =0; o<=29;o++){
k = z+o;
if(Map[i][k]==3){
g2d.drawImage(pilzblock,x,y,null);
}
if(Map[i][k]==2){
g2d.drawImage(grass,x,y,null);
}
if(Map[i][k]==1){
g2d.drawImage(block,x,y,null);
}
if(Map[i][k]==0){
g2d.drawImage(luft,x,y,null);
}
x = x+30;
}
x=1;
y= y+30;
}
x=1;
y=1;
g2d.drawImage(mario,px,py,null);
}
private class Al extends KeyAdapter{
public Al(){}
public void keyPressed(KeyEvent e){
key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
if (z>=0 ){
px-=10;
}
}
if(key == KeyEvent.VK_RIGHT){
px+=10;
}
if(key == KeyEvent.VK_SPACE){
sprung =true;
}
}
}
public void keyReleased(KeyEvent e){
key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT || key== KeyEvent.VK_RIGHT){
}
}
}
das ist nur kopiert vom tutorial
Java:
package mario;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class frame extends JFrame implements ActionListener {
private JButton schliessen;
private JButton einstellung;
private JButton info;
private JButton ende;
public static void main (String[]args) throws Exception{
frame frame = new frame("UI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLayout(null);
frame.setVisible(true);
}
public frame(String title) {
super(title);
schliessen = new JButton("Spiel starten");
schliessen.setBounds(120, 40, 160, 40);
schliessen.addActionListener(this);
add(schliessen);
einstellung = new JButton("Einstellungen");
einstellung.setBounds(120, 120, 160, 40);
einstellung.addActionListener(this);
add(einstellung);
info = new JButton("Credits");
info.setBounds(120, 200, 160, 40);
info.addActionListener(this);
add(info);
ende = new JButton("Beenden");
ende.setBounds(120, 280, 160, 40);
ende.addActionListener(this);
add(ende);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == schliessen){
fenster();
}
if (e.getSource() == info){
Object[] options = { "OK"};
JOptionPane.showOptionDialog(null, "Programmiert von JaDaHB1", "Information",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
}
if (e.getSource() == einstellung){
Object[] options = { "OK"};
JOptionPane.showOptionDialog(null, "Hier können sie Einstellungen vornehmen", "Information",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
}
if (e.getSource() == ende){
System.exit(0);
}
}
public static void fenster(){
JFrame fenster = new JFrame("Game");
fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenster.setSize(907,483);
fenster.setVisible(true);
fenster.setResizable(false);
fenster.add(new Board());
}
}
Zuletzt bearbeitet: