My pong_game
commit
e14c22976c
|
@ -0,0 +1,3 @@
|
||||||
|
/bin/
|
||||||
|
/.classpath
|
||||||
|
/.project
|
|
@ -0,0 +1,2 @@
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
encoding/<project>=UTF-8
|
|
@ -0,0 +1,11 @@
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=23
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=23
|
||||||
|
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||||
|
org.eclipse.jdt.core.compiler.release=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.source=23
|
|
@ -0,0 +1,9 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
module MyPong_Game {
|
||||||
|
requires java.desktop;
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package pong_game;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class Ball extends Rectangle {
|
||||||
|
|
||||||
|
int yBewegung;
|
||||||
|
int xBewegung;
|
||||||
|
int speed = 4;
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
|
Ball(int x, int y, int width, int height) {
|
||||||
|
super(x, y, width, height);
|
||||||
|
xBewegung = random.nextInt(10) - 5;
|
||||||
|
if (xBewegung == 0)
|
||||||
|
xBewegung++;
|
||||||
|
xBewegung(xBewegung * speed);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw(Graphics g) {
|
||||||
|
g.setColor(Color.green);
|
||||||
|
g.fillOval(x, y, width, height);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void xBewegung(int x) {
|
||||||
|
xBewegung = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void yBewegung(int y) {
|
||||||
|
yBewegung = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void move() {
|
||||||
|
x += xBewegung;
|
||||||
|
y += yBewegung;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,166 @@
|
||||||
|
package pong_game;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import java.util.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class GamePanle extends JPanel implements Runnable {
|
||||||
|
|
||||||
|
final static int WIDTH_WINDOW = 1000;
|
||||||
|
final static int HEIGH_WINDOW = 700;
|
||||||
|
Dimension fensterSize = new Dimension(WIDTH_WINDOW, HEIGH_WINDOW);
|
||||||
|
|
||||||
|
final static int WIDTH_RECT = 20;
|
||||||
|
final static int HEIGHT_RECT = 100;
|
||||||
|
|
||||||
|
final static int WIDTH_BALL = 20;
|
||||||
|
final static int HEIGHT_BALL = 20;
|
||||||
|
|
||||||
|
Schläger spieler1, spieler2;
|
||||||
|
Ball ball;
|
||||||
|
Random random;
|
||||||
|
Thread game;
|
||||||
|
int scoreSpieler1 = 0;
|
||||||
|
int scoreSpieler2 = 0;
|
||||||
|
int speed = 2;
|
||||||
|
|
||||||
|
GamePanle() {
|
||||||
|
zeichneRects();
|
||||||
|
this.setFocusable(true);
|
||||||
|
this.setPreferredSize(fensterSize);
|
||||||
|
this.addKeyListener(new AL());
|
||||||
|
random = new Random();
|
||||||
|
game = new Thread(this);
|
||||||
|
game.start();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void zeichneRects() {
|
||||||
|
spieler1 = new Schläger(0, (HEIGH_WINDOW / 2) - (HEIGHT_RECT / 2), WIDTH_RECT, HEIGHT_RECT, 1);
|
||||||
|
spieler2 = new Schläger(WIDTH_WINDOW - WIDTH_RECT, (HEIGH_WINDOW / 2) - (HEIGHT_RECT / 2), WIDTH_RECT,
|
||||||
|
HEIGHT_RECT, 2);
|
||||||
|
ball = new Ball(WIDTH_WINDOW / 2, 100, WIDTH_BALL, HEIGHT_BALL);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void paint(Graphics g) {
|
||||||
|
super.paint(g);
|
||||||
|
|
||||||
|
// Screen farbe
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.fillRect(0, 0, WIDTH_WINDOW, HEIGH_WINDOW);
|
||||||
|
g.setColor(Color.GRAY);
|
||||||
|
g.fillRect(WIDTH_WINDOW / 2, 0, 5, HEIGH_WINDOW);
|
||||||
|
|
||||||
|
spieler1.draw(g);
|
||||||
|
spieler2.draw(g);
|
||||||
|
ball.draw(g);
|
||||||
|
|
||||||
|
g.setFont(new Font("Arial", Font.BOLD, 30));
|
||||||
|
g.setColor(Color.WHITE);
|
||||||
|
g.drawString(scoreSpieler1 + "", WIDTH_WINDOW / 2 - 50, 35);
|
||||||
|
|
||||||
|
g.setColor(Color.WHITE);
|
||||||
|
g.drawString(scoreSpieler2 + "", WIDTH_WINDOW / 2 + 50, 35);
|
||||||
|
|
||||||
|
g.setFont(new Font("Arial", Font.BOLD, 20));
|
||||||
|
g.setColor(Color.YELLOW);
|
||||||
|
g.drawString("Obai", WIDTH_WINDOW / 2 - 120, 35);
|
||||||
|
|
||||||
|
g.setFont(new Font("Arial", Font.BOLD, 20));
|
||||||
|
g.setColor(Color.YELLOW);
|
||||||
|
g.drawString("ComputerGegener", WIDTH_WINDOW / 2 + 100, 35);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void mov() {
|
||||||
|
|
||||||
|
spieler1.move();
|
||||||
|
spieler2.movegegener();
|
||||||
|
ball.move();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void checkRänder() {
|
||||||
|
if (spieler1.y <= 0)
|
||||||
|
spieler1.y = 0;
|
||||||
|
|
||||||
|
if (spieler1.y >= HEIGH_WINDOW - HEIGHT_RECT)
|
||||||
|
spieler1.y = HEIGH_WINDOW - HEIGHT_RECT;
|
||||||
|
|
||||||
|
if (ball.x > WIDTH_WINDOW / 2 && ball.y < spieler2.y)
|
||||||
|
spieler2.setBewegungGegener(-10);
|
||||||
|
|
||||||
|
else if (ball.x > WIDTH_WINDOW / 2 && ball.y > spieler2.y)
|
||||||
|
spieler2.setBewegungGegener(10);
|
||||||
|
|
||||||
|
else
|
||||||
|
spieler2.setBewegungGegener(0);
|
||||||
|
|
||||||
|
if (spieler2.y <= 0)
|
||||||
|
spieler2.y = 0;
|
||||||
|
|
||||||
|
if (spieler2.y >= HEIGH_WINDOW - HEIGHT_RECT)
|
||||||
|
spieler2.y = HEIGH_WINDOW - HEIGHT_RECT;
|
||||||
|
|
||||||
|
if (ball.intersects(spieler1)) {
|
||||||
|
int bewegexRandom = random.nextInt(8) + 4;
|
||||||
|
ball.xBewegung(bewegexRandom);
|
||||||
|
int bewegeyRandom = random.nextInt(5) - 2;
|
||||||
|
ball.yBewegung(bewegeyRandom);
|
||||||
|
}
|
||||||
|
if (ball.intersects(spieler2)) {
|
||||||
|
|
||||||
|
int bewegexRandom = random.nextInt(8) + 4;
|
||||||
|
ball.xBewegung(-bewegexRandom);
|
||||||
|
int bewegeyRandom = random.nextInt(5) - 2;
|
||||||
|
ball.yBewegung(bewegeyRandom);
|
||||||
|
|
||||||
|
}
|
||||||
|
if (ball.x <= 0) {
|
||||||
|
scoreSpieler2++;
|
||||||
|
zeichneRects();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ball.x >= WIDTH_WINDOW) {
|
||||||
|
scoreSpieler1++;
|
||||||
|
zeichneRects();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ball.y >= HEIGH_WINDOW - HEIGHT_BALL)
|
||||||
|
ball.yBewegung = -ball.yBewegung;
|
||||||
|
|
||||||
|
if (ball.y <= 0)
|
||||||
|
ball.yBewegung = -ball.yBewegung;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
mov();
|
||||||
|
checkRänder();
|
||||||
|
repaint();
|
||||||
|
Thread.sleep(5);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AL extends KeyAdapter {
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
spieler1.keyPressed(e);
|
||||||
|
spieler2.keyPressed(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
spieler1.keyReleased(e);
|
||||||
|
spieler2.keyReleased(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package pong_game;
|
||||||
|
|
||||||
|
public class GamePlay {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new GameWindow();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package pong_game;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class GameWindow extends JFrame {
|
||||||
|
|
||||||
|
GamePanle game;
|
||||||
|
|
||||||
|
GameWindow() {
|
||||||
|
game = new GamePanle();
|
||||||
|
this.add(game);
|
||||||
|
this.setTitle("Pong Game");
|
||||||
|
this.setResizable(false);
|
||||||
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
this.pack();
|
||||||
|
this.setLocationRelativeTo(null);
|
||||||
|
this.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,62 @@
|
||||||
|
package pong_game;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
|
||||||
|
public class Schläger extends Rectangle {
|
||||||
|
|
||||||
|
int id;
|
||||||
|
int yBewegung;
|
||||||
|
|
||||||
|
int gegnerBewegungy;
|
||||||
|
int speed = 10;
|
||||||
|
|
||||||
|
Schläger(int x, int y, int width, int height, int id){
|
||||||
|
super(x,y,width,height);
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void move() {
|
||||||
|
y += yBewegung;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void movegegener() {
|
||||||
|
y += gegnerBewegungy;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
if (id == 1) {
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_W)
|
||||||
|
setBewegung(-speed);
|
||||||
|
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_S)
|
||||||
|
setBewegung(speed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
if (id == 1) {
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_W ||e.getKeyCode() == KeyEvent.VK_S)
|
||||||
|
setBewegung(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBewegung(int bewegung) {
|
||||||
|
yBewegung = bewegung;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBewegungGegener(int bewegung) {
|
||||||
|
gegnerBewegungy = bewegung;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw(Graphics g) {
|
||||||
|
if (id == 1)
|
||||||
|
g.setColor(Color.RED);
|
||||||
|
else
|
||||||
|
g.setColor(Color.BLUE);
|
||||||
|
|
||||||
|
g.fillRect(x,y,width,height);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue