Pong Ohne ComputerGegner
parent
4052a1392b
commit
f8eb877a38
|
@ -19,17 +19,18 @@ public class MouseMotionListenerBeispiel {
|
|||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
mousePosition.setText(" Mouse Position: (" + e.getX() + ", " + e.getY() + ")");
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
mousePosition.setText(" Mouse Position: (" + e.getX() + ", " + e.getY() + ")");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
frame.setSize(300, 250); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 250
|
||||
frame.setSize(700, 700); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 250
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
|
||||
frame.setLayout(new FlowLayout()); // لترتيب الأشياء التي أضفناها فيها FlowLayout هنا جعلنا النافذة تستخدم الـ
|
||||
frame.setVisible(true); // هنا جعلنا النافذة مرئية
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package GUIAnwendungen;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
|
@ -40,3 +41,5 @@ public class Jframe {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
Binary file not shown.
|
@ -1,5 +1,6 @@
|
|||
package Übungen;
|
||||
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
@ -7,107 +8,227 @@ import java.awt.event.KeyListener;
|
|||
|
||||
public class PongGame {
|
||||
|
||||
static int xRect1 = 10;
|
||||
static int yRect1 = 10;
|
||||
static int bewegung = 1;
|
||||
//Eigenschaften des Rechtecks1
|
||||
static JPanel rect1;
|
||||
static int xPositionRect1 = 300;
|
||||
static int yPositionRect1 = 550;
|
||||
|
||||
public static void main(String[] args) {
|
||||
static int rectWidth = 100;
|
||||
static int rectHeight = 7;
|
||||
|
||||
static int daleyRect = 300;
|
||||
static int bewegungRect = 1;
|
||||
//--------------------------
|
||||
|
||||
//Eigenschaften des Kreises
|
||||
static JPanel kreis;
|
||||
static int xPositioKreis = 300;
|
||||
static int yPositionKreis = 400;
|
||||
|
||||
static int kreisWidth = 10;
|
||||
static int kreisHeight = 10;
|
||||
|
||||
static int daleyKreis = 5;
|
||||
static int bewegungKreisX = 1;
|
||||
static int bewegungKreisY = 1;
|
||||
//---------------------------
|
||||
|
||||
//Window Eigenschaften
|
||||
static int widthWindow = 700;
|
||||
static int heihtWindow = 600;
|
||||
//------------------------------
|
||||
|
||||
//Eigenschaften des Rechtecks2
|
||||
static JPanel rect2;
|
||||
static int xPositionRect2 = widthWindow/2;
|
||||
static int yPositionRect2 = 5;
|
||||
static int rect2Width = 100;
|
||||
static int rect2Height = 7;
|
||||
static int daleyRect2 = 300;
|
||||
static int bewegungRect2 = 1;
|
||||
|
||||
Graphics g;
|
||||
public static void main(String[] args) {
|
||||
JFrame window = new JFrame();
|
||||
window.setSize(700, 700);
|
||||
window.setSize(widthWindow, heihtWindow);
|
||||
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
window.setTitle("PongGame");
|
||||
window.setLayout(null);
|
||||
window.setResizable(false);
|
||||
|
||||
rect2 = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(0, 0, rect2Width, rect2Height);
|
||||
}
|
||||
|
||||
};
|
||||
rect2.setBounds(xPositionRect2,yPositionRect2,rect2Width,rect2Height);
|
||||
Timer timerRect2 = new Timer(daleyRect2, e -> {
|
||||
window.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_D) {
|
||||
bewegungRect2 = 1;
|
||||
if (xPositionRect2 >= widthWindow - 95)
|
||||
bewegungRect2 = 0;
|
||||
|
||||
else
|
||||
xPositionRect2 += bewegungRect2;
|
||||
|
||||
rect2.setBounds(xPositionRect2, yPositionRect2, rect2Width, rect2Height);
|
||||
// Fordere das Panel auf, sich neu zu zeichnen
|
||||
rect2.repaint();
|
||||
}
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_A) {
|
||||
bewegungRect2 = 1;
|
||||
if (bewegungRect2 <= 0)
|
||||
bewegungRect = 0;
|
||||
else
|
||||
xPositionRect2 -= bewegungRect2;
|
||||
|
||||
}
|
||||
rect2.setBounds(xPositionRect2, yPositionRect2, rect2Width, rect2Height);
|
||||
// Fordere das Panel auf, sich neu zu zeichnen
|
||||
rect2.repaint();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
timerRect2.start();
|
||||
|
||||
|
||||
|
||||
kreis = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.white);
|
||||
g.drawOval(0, 0, kreisWidth, kreisHeight);
|
||||
|
||||
}
|
||||
};
|
||||
kreis.setBounds(xPositioKreis, yPositionKreis, kreisWidth, kreisHeight);
|
||||
|
||||
|
||||
Timer timerKreis = new Timer(daleyKreis, e -> {
|
||||
// Erstelle Rechteck für Ball und Rechteck
|
||||
Rectangle ballBounds = new Rectangle(xPositioKreis, yPositionKreis, kreisWidth, kreisHeight);
|
||||
Rectangle rectBounds = new Rectangle(xPositionRect1, yPositionRect1, rectWidth, rectHeight);
|
||||
Rectangle rectBounds2 = new Rectangle(xPositionRect2, yPositionRect2, rect2Width, rect2Height);
|
||||
|
||||
JPanel rect1 = new JPanel() {
|
||||
// Wenn der Kreis die Oberkante erreicht, kehrt er die Richtung um
|
||||
if (ballBounds.intersects(rectBounds2))
|
||||
bewegungKreisY = -bewegungKreisY;
|
||||
|
||||
// if (yPositionKreis >= window.getHeight() - kreisHeight)
|
||||
// bewegungKreisY = -bewegungKreisY;
|
||||
//
|
||||
|
||||
|
||||
// Überprüfe Kollision zwischen Ball und Rechteck
|
||||
if (ballBounds.intersects(rectBounds)) {
|
||||
bewegungKreisY = -bewegungKreisY;
|
||||
}
|
||||
|
||||
|
||||
if (xPositioKreis <= 0)
|
||||
bewegungKreisX = -bewegungKreisX;
|
||||
|
||||
if (xPositioKreis >= window.getWidth() - kreisWidth)
|
||||
bewegungKreisX = -bewegungKreisX;
|
||||
|
||||
yPositionKreis -= bewegungKreisY;
|
||||
xPositioKreis += bewegungKreisX;
|
||||
|
||||
kreis.setBounds(xPositioKreis, yPositionKreis, kreisWidth, kreisHeight);
|
||||
kreis.repaint();
|
||||
|
||||
});
|
||||
timerKreis.start();
|
||||
|
||||
|
||||
rect1 = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g); // Sicherstellen, dass das Panel korrekt gezeichnet wird
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(0, 0, 10, 90);
|
||||
g.fillRect(0, 0, rectWidth, rectHeight);
|
||||
}
|
||||
};
|
||||
rect1.setBounds(xRect1, yRect1, 10, 90);
|
||||
rect1.setBounds(xPositionRect1, yPositionRect1, rectWidth, rectHeight);
|
||||
|
||||
Timer timerRect = new Timer(daleyRect, e -> {
|
||||
window.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
Timer timer = new Timer(710, e -> {
|
||||
window.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
}
|
||||
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
|
||||
bewegungRect = 1;
|
||||
if (xPositionRect1 >= widthWindow - 95)
|
||||
bewegungRect = 0;
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode() ==KeyEvent.VK_DOWN) {
|
||||
// Bewege das Rechteck
|
||||
yRect1 += bewegung ;
|
||||
else
|
||||
xPositionRect1 += bewegungRect;
|
||||
|
||||
// Aktualisiere dann die Position des Panels
|
||||
rect1.setBounds(xRect1, yRect1, 10, 90);
|
||||
// Fordere das Panel auf, sich neu zu zeichnen
|
||||
rect1.repaint();
|
||||
}
|
||||
rect1.setBounds(xPositionRect1, yPositionRect1, rectWidth, rectHeight);
|
||||
// Fordere das Panel auf, sich neu zu zeichnen
|
||||
rect1.repaint();
|
||||
}
|
||||
|
||||
}
|
||||
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
|
||||
bewegungRect = 1;
|
||||
if (xPositionRect1 <= 0)
|
||||
bewegungRect = 0;
|
||||
else
|
||||
xPositionRect1 -= bewegungRect;
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
}
|
||||
rect1.setBounds(xPositionRect1, yPositionRect1, rectWidth, rectHeight);
|
||||
// Fordere das Panel auf, sich neu zu zeichnen
|
||||
rect1.repaint();
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Starte den Timer
|
||||
timer.start();
|
||||
});
|
||||
|
||||
timerRect.start();
|
||||
|
||||
|
||||
// Setze den Fokus auf das Fenster, um die KeyListener zu aktivieren
|
||||
window.setFocusable(true);
|
||||
window.requestFocusInWindow();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
JPanel rect2 = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(0, 0, 10, 90);
|
||||
}
|
||||
};
|
||||
rect2.setBounds(650, 10, 10, 90);
|
||||
|
||||
window.add(rect1);
|
||||
window.add(rect2);
|
||||
window.add(kreis);
|
||||
|
||||
/*
|
||||
* JFrame besteht aus mehreren Schichten: Eine davon ist das ContentPane, welches der Bereich ist, in den die meisten Komponenten wie JPanel, JButton, etc.
|
||||
|
|
|
@ -0,0 +1,124 @@
|
|||
package Übungen.Ponggame;
|
||||
import java.util.Random;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Ball extends JPanel implements Runnable {
|
||||
// Ball Eigenschaften
|
||||
final int width = 15;
|
||||
final int height = 15;
|
||||
int yPosition;
|
||||
int xPosition;
|
||||
Rectangle ball;
|
||||
|
||||
Spieler spieler1 = new Spieler(7, 700 / 2, 1);
|
||||
Spieler spieler2 = new Spieler(700 - 40, 700 / 2, 2);
|
||||
|
||||
// Score Labels
|
||||
JLabel scoreSpieler1 = new JLabel ("Score: " + spieler1.getScore());
|
||||
JLabel scoreSpieler2 = new JLabel ("Score: " + spieler2.getScore());
|
||||
|
||||
int delay = 4;
|
||||
double ybewegung;
|
||||
double xbewegung;
|
||||
|
||||
Random rand = new Random();
|
||||
|
||||
public Ball() {
|
||||
this.xPosition = 700 / 2;
|
||||
this.yPosition = 700 / 2;
|
||||
ball = new Rectangle(xPosition, yPosition, width, height);
|
||||
this.setOpaque(true);
|
||||
|
||||
// Zufällige Richtung für den Start des Balls
|
||||
xbewegung = (rand.nextBoolean() ? 1 : -1) * (1 + rand.nextDouble());
|
||||
ybewegung = (rand.nextBoolean() ? 1 : -1) * (1 + rand.nextDouble());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.red);
|
||||
g.fillRect(ball.x, ball.y, ball.width, ball.height);
|
||||
}
|
||||
|
||||
public void move() {
|
||||
if (ball.y <= 0 || ball.y >= 650)
|
||||
ybewegung = -ybewegung;
|
||||
|
||||
|
||||
|
||||
|
||||
ball.y += ybewegung;
|
||||
ball.x += xbewegung;
|
||||
setBounds(ball.x, ball.y, ball.width, ball.height);
|
||||
repaint();
|
||||
|
||||
checkKollision();
|
||||
}
|
||||
|
||||
public void setxbewegung(double xbewegung) {
|
||||
this.xbewegung = xbewegung;
|
||||
}
|
||||
|
||||
public void setybewegung(double ybewegung) {
|
||||
this.ybewegung = ybewegung;
|
||||
}
|
||||
|
||||
public void checkKollision() {
|
||||
if (ball.intersects(spieler1.schläger)) {
|
||||
xbewegung = Math.abs(xbewegung) * (1 + rand.nextDouble() * 0.1);
|
||||
ybewegung = ybewegung + (rand.nextDouble() - 0.5) * 0.5;
|
||||
}
|
||||
|
||||
if (ball.intersects(spieler2.schläger)) {
|
||||
xbewegung = -Math.abs(xbewegung) * (1 + rand.nextDouble() * 0.1);
|
||||
ybewegung = ybewegung + (rand.nextDouble() - 0.5) * 0.5;
|
||||
}
|
||||
|
||||
if (ball.x <= 0) {
|
||||
spieler2.setScore(spieler2.getScore() + 1);
|
||||
System.out.println("Spieler 2 Score: " + spieler2.getScore());
|
||||
resetBall();
|
||||
}
|
||||
|
||||
if (ball.x >= 680) {
|
||||
spieler1.setScore(spieler1.getScore() + 1);
|
||||
System.out.println("Spieler 1 Score: " + spieler1.getScore());
|
||||
resetBall();
|
||||
}
|
||||
updateScores();
|
||||
}
|
||||
|
||||
public void resetBall() {
|
||||
ball.x = 700 / 2;
|
||||
ball.y = 700 / 2;
|
||||
|
||||
// Neue zufällige Bewegungsrichtung
|
||||
xbewegung = (rand.nextBoolean() ? 1 : -1) * (1 + rand.nextDouble());
|
||||
ybewegung = (rand.nextBoolean() ? 1 : -1) * (1 + rand.nextDouble());
|
||||
|
||||
setBounds(ball.x, ball.y, ball.width, ball.height);
|
||||
repaint();
|
||||
}
|
||||
|
||||
// Methode zum Aktualisieren der Score-Labels
|
||||
public void updateScores() {
|
||||
scoreSpieler1.setText("Score: " + spieler1.getScore());
|
||||
scoreSpieler2.setText("Score: " + spieler2.getScore());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
move();
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package Übungen.Ponggame;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
public class GameWindwo extends JFrame {
|
||||
// Fenster Eigenschaften
|
||||
static final int WINDOW_WIDTH = 700;
|
||||
static final int WINDOW_HEIGHT = 700;
|
||||
Dimension screenSize = new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
|
||||
// Fenseter Elemente
|
||||
Panel blackScreen = new Panel();
|
||||
Ball ball = new Ball();
|
||||
|
||||
public GameWindwo() {
|
||||
|
||||
this.setTitle("PongGame");
|
||||
this.setSize(screenSize);
|
||||
this.setResizable(false);
|
||||
this.setVisible(true);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.setLocationRelativeTo(null);
|
||||
this.setLayout(null);
|
||||
this.getContentPane().setBackground(Color.BLACK);
|
||||
|
||||
ball.setBounds(700 / 2, 700 / 2, 10, 10);
|
||||
|
||||
ball.spieler1.setBounds(7, WINDOW_HEIGHT / 2, 8, 60);
|
||||
ball.scoreSpieler1.setForeground(Color.white);
|
||||
ball.scoreSpieler1.setBounds(10, 10, 100, 10);
|
||||
|
||||
ball.spieler2.setBounds(WINDOW_WIDTH - 40, WINDOW_HEIGHT / 2, 8, 60);
|
||||
ball.scoreSpieler2.setForeground(Color.white);
|
||||
ball.scoreSpieler2.setBounds(WINDOW_WIDTH - 100, 10, 100, 10);
|
||||
|
||||
this.add(ball);
|
||||
this.add(ball.spieler1);
|
||||
this.add(ball.scoreSpieler1);
|
||||
this.add(ball.spieler2);
|
||||
this.add(ball.scoreSpieler2);
|
||||
|
||||
this.add(blackScreen);
|
||||
getInputs();
|
||||
}
|
||||
|
||||
public void getInputs() {
|
||||
addKeyListener(new KeyListener() {
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
ball.spieler1.KeyPressed(e);
|
||||
ball.spieler2.KeyPressed(e);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
ball.spieler1.keyReleased(e);
|
||||
ball.spieler2.keyReleased(e);
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package Übungen.Ponggame;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class Panel extends JPanel {
|
||||
private static final int WINDOW_WIDTH = 700;
|
||||
private static final int WINDOW_HEIGHT = 700;
|
||||
|
||||
Panel(){
|
||||
|
||||
this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
this.setBackground(Color.black);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
|
||||
package Übungen.Ponggame;
|
||||
|
||||
|
||||
public class PongGame {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GameWindwo g1 = new GameWindwo();
|
||||
Thread ball = new Thread(g1.ball);
|
||||
ball.start();
|
||||
Thread spieler1 = new Thread(g1.ball.spieler1);
|
||||
spieler1.start();
|
||||
|
||||
Thread spieler2 = new Thread(g1.ball.spieler2);
|
||||
spieler2.start();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
package Übungen.Ponggame;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
public class Spieler extends JPanel implements Runnable{
|
||||
|
||||
int id;
|
||||
final int width = 8;
|
||||
final int height = 60;
|
||||
int yPosition;
|
||||
int xPosition;
|
||||
int score;
|
||||
int ybewegung;
|
||||
|
||||
int dealy = 2;
|
||||
Rectangle schläger;
|
||||
|
||||
public Spieler(int x, int y, int id) {
|
||||
this.xPosition = x;
|
||||
this.yPosition = y;
|
||||
this.id = id;
|
||||
this.score = 0;
|
||||
schläger = new Rectangle(x, y, width, height);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(schläger.x, schläger.y, schläger.width, schläger.height);
|
||||
|
||||
}
|
||||
|
||||
public void move() {
|
||||
schläger.y += ybewegung;
|
||||
if (schläger.y <= 0)
|
||||
schläger.y = 0;
|
||||
|
||||
if (schläger.y >= 600)
|
||||
schläger.y = 600;
|
||||
|
||||
|
||||
setBounds(schläger.x, schläger.y, schläger.width, schläger.height);
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void setYbewegung(int ybewegung) {
|
||||
this.ybewegung = ybewegung;
|
||||
}
|
||||
|
||||
public void KeyPressed(KeyEvent e) {
|
||||
switch (id) {
|
||||
case 1:
|
||||
if (e.getKeyCode() == KeyEvent.VK_W)
|
||||
setYbewegung(-1);
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_S)
|
||||
setYbewegung(1);
|
||||
|
||||
break;
|
||||
case 2:
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_UP)
|
||||
setYbewegung(-1);
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_DOWN)
|
||||
setYbewegung(1);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
switch (id) {
|
||||
case 1:
|
||||
if (e.getKeyCode() == KeyEvent.VK_W ||e.getKeyCode() == KeyEvent.VK_S)
|
||||
setYbewegung(0);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN)
|
||||
setYbewegung(0);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
|
||||
public void setScore(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
move();
|
||||
Thread.sleep(dealy);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package Übungen;
|
||||
|
||||
import java.awt.Rectangle;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class Test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Erstelle ein Fenster
|
||||
JFrame frame = new JFrame();
|
||||
Rectangle r1 = new Rectangle(10,10,100,20);
|
||||
|
||||
frame.setVisible(true);
|
||||
frame.setSize(420, 500);
|
||||
frame.setTitle("Mein erstes Fenster");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setResizable(false);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue