pacman
|
@ -0,0 +1,12 @@
|
||||||
|
package Logging;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
public class Test {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -11,8 +11,8 @@ public class GameContainer extends JPanel implements Runnable {
|
||||||
final static int WIDTH_WINDOW = 1000;
|
final static int WIDTH_WINDOW = 1000;
|
||||||
final static int HEIGH_WINDOW = 700;
|
final static int HEIGH_WINDOW = 700;
|
||||||
Dimension fensterSize = new Dimension(WIDTH_WINDOW,HEIGH_WINDOW);
|
Dimension fensterSize = new Dimension(WIDTH_WINDOW,HEIGH_WINDOW);
|
||||||
|
|
||||||
PacMan pacman;
|
PacMan pacman;
|
||||||
|
|
||||||
Geist[] geists = new Geist[3];
|
Geist[] geists = new Geist[3];
|
||||||
|
|
||||||
ArrayList<SpielKarte> waende;
|
ArrayList<SpielKarte> waende;
|
||||||
|
@ -29,7 +29,7 @@ public class GameContainer extends JPanel implements Runnable {
|
||||||
GameContainer(){
|
GameContainer(){
|
||||||
this.waende = new ArrayList<>();
|
this.waende = new ArrayList<>();
|
||||||
draw_waende();
|
draw_waende();
|
||||||
draw_PacMan();
|
draw_pacman();
|
||||||
this.setFocusable(true);
|
this.setFocusable(true);
|
||||||
this.setPreferredSize(fensterSize);
|
this.setPreferredSize(fensterSize);
|
||||||
this.addKeyListener(new Steuern());
|
this.addKeyListener(new Steuern());
|
||||||
|
@ -38,7 +38,7 @@ public class GameContainer extends JPanel implements Runnable {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw_PacMan() {
|
public void draw_pacman() {
|
||||||
pacman = new PacMan(WIDTH_WINDOW/2,HEIGH_WINDOW/2);
|
pacman = new PacMan(WIDTH_WINDOW/2,HEIGH_WINDOW/2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,8 +67,8 @@ public class GameContainer extends JPanel implements Runnable {
|
||||||
for (SpielKarte w:waende )
|
for (SpielKarte w:waende )
|
||||||
w.draw(g);
|
w.draw(g);
|
||||||
|
|
||||||
// Zeige Pacman
|
pacman.draw(g);
|
||||||
pacman.draw(g);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,11 +85,18 @@ public class GameContainer extends JPanel implements Runnable {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pacman.y <= 5 || pacman.x <= 5) {
|
if (pacman.y <= 5 ) {
|
||||||
pacman.set_x_bewegung(0);
|
|
||||||
pacman.set_y_bewegung(0);
|
pacman.set_y_bewegung(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (pacman.x < 0)
|
||||||
|
pacman.x = WIDTH_WINDOW ;
|
||||||
|
|
||||||
|
if (pacman.x < 0)
|
||||||
|
pacman.x = WIDTH_WINDOW ;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -100,7 +107,7 @@ public class GameContainer extends JPanel implements Runnable {
|
||||||
move();
|
move();
|
||||||
checkKollision();
|
checkKollision();
|
||||||
repaint();
|
repaint();
|
||||||
Thread.sleep(25);
|
Thread.sleep(20);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,63 +3,114 @@ package Übungen.Pac_Man;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
|
|
||||||
|
import javax.swing.ImageIcon;
|
||||||
|
|
||||||
public class PacMan extends Rectangle {
|
public class PacMan extends Rectangle {
|
||||||
|
|
||||||
private static int pacMan_width = 20;
|
private static int pacMan_width = 30;
|
||||||
private static int pacMan_height = 20;
|
private static int pacMan_height = 30;
|
||||||
private int ybewegung;
|
private int ybewegung;
|
||||||
private int xbewegung;
|
private int xbewegung;
|
||||||
private int speed = 8;
|
private int speed = 8;
|
||||||
|
|
||||||
|
Image[] pacmanImagesRight;
|
||||||
|
Image[] pacmanImagesLeft;
|
||||||
|
Image[] pacmanImagesUp;
|
||||||
|
Image[] pacmanImagesDown;
|
||||||
|
|
||||||
PacMan(int x, int y) {
|
Image currentImage;
|
||||||
|
int animationIndex = 0;
|
||||||
|
long lastTime = System.nanoTime();
|
||||||
|
|
||||||
|
PacMan(int x, int y) {
|
||||||
|
super(x, y, pacMan_width, pacMan_height);
|
||||||
|
|
||||||
|
pacmanImagesRight = new Image[]{
|
||||||
|
new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmanright.png").getImage(),
|
||||||
|
new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmanopenright.png").getImage()
|
||||||
|
};
|
||||||
|
pacmanImagesLeft = new Image[]{
|
||||||
|
new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmanleft.png").getImage(),
|
||||||
|
new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmanopenleft.png").getImage()
|
||||||
|
};
|
||||||
|
pacmanImagesUp = new Image[]{
|
||||||
|
new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmanup.png").getImage(),
|
||||||
|
new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmanopenup.png").getImage()
|
||||||
|
};
|
||||||
|
pacmanImagesDown = new Image[]{
|
||||||
|
new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmandown.png").getImage(),
|
||||||
|
new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmanopendown.png").getImage()
|
||||||
|
};
|
||||||
|
|
||||||
super(x, y, pacMan_width, pacMan_height);
|
// Standardmäßig das rechte Bild anzeigen
|
||||||
}
|
currentImage = pacmanImagesRight[0];
|
||||||
|
}
|
||||||
|
|
||||||
public void draw(Graphics g) {
|
public void draw(Graphics g) {
|
||||||
g.setColor(Color.GREEN);
|
long currentTime = System.nanoTime();
|
||||||
g.fillRect(x, y, pacMan_width, pacMan_height);
|
// Alle 200ms zwischen den zwei Animation-Bildern wechseln
|
||||||
}
|
if ((currentTime - lastTime) > 200_000_000) {
|
||||||
|
animationIndex = (animationIndex + 1) % 2; // 0 -> 1 -> 0 wechseln
|
||||||
|
lastTime = currentTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Je nach Bewegung das passende Bild setzen
|
||||||
|
if (xbewegung > 0)
|
||||||
|
currentImage = pacmanImagesRight[animationIndex];
|
||||||
|
else if (xbewegung < 0)
|
||||||
|
currentImage = pacmanImagesLeft[animationIndex];
|
||||||
|
else if (ybewegung > 0)
|
||||||
|
currentImage = pacmanImagesDown[animationIndex];
|
||||||
|
else if (ybewegung < 0)
|
||||||
|
currentImage = pacmanImagesUp[animationIndex];
|
||||||
|
|
||||||
|
|
||||||
|
g.drawImage(currentImage, x, y, pacMan_width, pacMan_height, null);
|
||||||
|
}
|
||||||
|
|
||||||
public void keyPressed(KeyEvent e) {
|
|
||||||
|
|
||||||
if (e.getKeyCode() == KeyEvent.VK_W) {
|
public void keyPressed(KeyEvent e) {
|
||||||
set_y_bewegung(-speed);
|
if (e.getKeyCode() == KeyEvent.VK_W) {
|
||||||
set_x_bewegung(0);
|
currentImage = pacmanImagesUp[animationIndex];
|
||||||
}
|
set_y_bewegung(-speed);
|
||||||
|
set_x_bewegung(0);
|
||||||
|
}
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_S) {
|
||||||
|
currentImage = pacmanImagesDown[animationIndex];
|
||||||
|
set_y_bewegung(speed);
|
||||||
|
set_x_bewegung(0);
|
||||||
|
}
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_D) {
|
||||||
|
currentImage = pacmanImagesRight[animationIndex];
|
||||||
|
set_y_bewegung(0);
|
||||||
|
set_x_bewegung(speed);
|
||||||
|
}
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_A) {
|
||||||
|
currentImage = pacmanImagesLeft[animationIndex];
|
||||||
|
set_y_bewegung(0);
|
||||||
|
set_x_bewegung(-speed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (e.getKeyCode() == KeyEvent.VK_S) {
|
public void set_y_bewegung(int y) {
|
||||||
set_y_bewegung(speed);
|
this.ybewegung = y;
|
||||||
set_x_bewegung(0);
|
}
|
||||||
}
|
|
||||||
|
public void set_x_bewegung(int x) {
|
||||||
|
this.xbewegung = x;
|
||||||
|
}
|
||||||
|
|
||||||
if (e.getKeyCode() == KeyEvent.VK_D) {
|
// Bewegung updaten
|
||||||
set_y_bewegung(0);
|
public void move() {
|
||||||
set_x_bewegung(speed);
|
y += ybewegung;
|
||||||
}
|
x += xbewegung;
|
||||||
|
}
|
||||||
|
|
||||||
if (e.getKeyCode() == KeyEvent.VK_A) {
|
public void check_kollision(int windowWidth, int windowHeight) {
|
||||||
set_y_bewegung(0);
|
if (x < 0) x = 0;
|
||||||
set_x_bewegung(-speed);
|
if (y < 0) y = 0;
|
||||||
}
|
if (x + pacMan_width > windowWidth) x = windowWidth - pacMan_width;
|
||||||
|
if (y + pacMan_height > windowHeight) y = windowHeight - pacMan_height;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set_y_bewegung(int y) {
|
|
||||||
this.ybewegung = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set_x_bewegung(int x) {
|
|
||||||
this.xbewegung = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void move() {
|
|
||||||
y += ybewegung;
|
|
||||||
x += xbewegung;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void check_kollision() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
After Width: | Height: | Size: 741 B |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 1.2 KiB |
|
@ -1,35 +1,61 @@
|
||||||
package Übungen.Pac_Man;
|
package Übungen.Pac_Man;
|
||||||
|
|
||||||
import java.awt.EventQueue;
|
import java.awt.EventQueue;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import javax.swing.ImageIcon;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JPanel;
|
|
||||||
import javax.swing.border.EmptyBorder;
|
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JSlider;
|
|
||||||
import javax.swing.JTree;
|
|
||||||
|
|
||||||
public class test extends JFrame {
|
public class test extends JFrame implements Runnable {
|
||||||
|
|
||||||
|
Thread test;
|
||||||
|
ArrayList<ImageIcon> pacman;
|
||||||
|
JLabel setman;
|
||||||
|
int currentImageIndex = 0; // Um den aktuellen Index des Bildes zu verfolgen
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
test() {
|
||||||
private JPanel contentPane;
|
setman = new JLabel();
|
||||||
|
pacman = new ArrayList<>();
|
||||||
|
|
||||||
|
// Bilder zur Liste hinzufügen
|
||||||
|
pacman.add(new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmanright.png"));
|
||||||
|
pacman.add(new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmanopenright.png"));
|
||||||
|
// pacman.add(new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmandown.png"));
|
||||||
|
|
||||||
public test() {
|
this.setTitle("Pacman Animation");
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
this.setSize(700, 700);
|
||||||
setBounds(100, 100, 1000, 700);
|
this.setResizable(false);
|
||||||
contentPane = new JPanel();
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
this.setLocationRelativeTo(null);
|
||||||
|
this.add(setman);
|
||||||
|
|
||||||
setContentPane(contentPane);
|
// Thread starten
|
||||||
contentPane.setLayout(null);
|
test = new Thread(this);
|
||||||
|
test.start();
|
||||||
|
|
||||||
JLabel lblNewLabel = new JLabel("New label");
|
this.setVisible(true);
|
||||||
lblNewLabel.setBounds(230, 257, 169, 22);
|
}
|
||||||
contentPane.add(lblNewLabel);
|
|
||||||
|
public static void main(String[] args) {
|
||||||
JLabel lblNewLabel_1 = new JLabel("New label");
|
new test();
|
||||||
lblNewLabel_1.setBounds(230, 95, 46, 144);
|
}
|
||||||
contentPane.add(lblNewLabel_1);
|
|
||||||
|
public void zeigePacMan() {
|
||||||
|
setman.setIcon(pacman.get(currentImageIndex));
|
||||||
|
// Erhöht den Index und stellt sicher, dass er nicht größer als die Liste wird
|
||||||
|
currentImageIndex = (currentImageIndex + 1) % pacman.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
zeigePacMan();
|
||||||
|
Thread.sleep(300);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,7 @@ public class GamePanle extends JPanel implements Runnable {
|
||||||
g.setFont(new Font("Arial", Font.BOLD, 20));
|
g.setFont(new Font("Arial", Font.BOLD, 20));
|
||||||
g.setColor(Color.YELLOW);
|
g.setColor(Color.YELLOW);
|
||||||
g.drawString("ComputerGegener", WIDTH_WINDOW/2 + 100, 35);
|
g.drawString("ComputerGegener", WIDTH_WINDOW/2 + 100, 35);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void mov() {
|
public void mov() {
|
||||||
|
|