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,9 +67,9 @@ 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);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void move() {
|
public void move() {
|
||||||
|
@ -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;
|
||||||
|
|
||||||
PacMan(int x, int y) {
|
Image[] pacmanImagesRight;
|
||||||
|
Image[] pacmanImagesLeft;
|
||||||
|
Image[] pacmanImagesUp;
|
||||||
|
Image[] pacmanImagesDown;
|
||||||
|
|
||||||
super(x, y, pacMan_width, pacMan_height);
|
Image currentImage;
|
||||||
}
|
int animationIndex = 0;
|
||||||
|
long lastTime = System.nanoTime();
|
||||||
|
|
||||||
public void draw(Graphics g) {
|
PacMan(int x, int y) {
|
||||||
g.setColor(Color.GREEN);
|
super(x, y, pacMan_width, pacMan_height);
|
||||||
g.fillRect(x, y, pacMan_width, pacMan_height);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void keyPressed(KeyEvent e) {
|
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()
|
||||||
|
};
|
||||||
|
|
||||||
if (e.getKeyCode() == KeyEvent.VK_W) {
|
// Standardmäßig das rechte Bild anzeigen
|
||||||
set_y_bewegung(-speed);
|
currentImage = pacmanImagesRight[0];
|
||||||
set_x_bewegung(0);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (e.getKeyCode() == KeyEvent.VK_S) {
|
public void draw(Graphics g) {
|
||||||
set_y_bewegung(speed);
|
long currentTime = System.nanoTime();
|
||||||
set_x_bewegung(0);
|
// Alle 200ms zwischen den zwei Animation-Bildern wechseln
|
||||||
}
|
if ((currentTime - lastTime) > 200_000_000) {
|
||||||
|
animationIndex = (animationIndex + 1) % 2; // 0 -> 1 -> 0 wechseln
|
||||||
|
lastTime = currentTime;
|
||||||
|
}
|
||||||
|
|
||||||
if (e.getKeyCode() == KeyEvent.VK_D) {
|
// Je nach Bewegung das passende Bild setzen
|
||||||
set_y_bewegung(0);
|
if (xbewegung > 0)
|
||||||
set_x_bewegung(speed);
|
currentImage = pacmanImagesRight[animationIndex];
|
||||||
}
|
else if (xbewegung < 0)
|
||||||
|
currentImage = pacmanImagesLeft[animationIndex];
|
||||||
|
else if (ybewegung > 0)
|
||||||
|
currentImage = pacmanImagesDown[animationIndex];
|
||||||
|
else if (ybewegung < 0)
|
||||||
|
currentImage = pacmanImagesUp[animationIndex];
|
||||||
|
|
||||||
if (e.getKeyCode() == KeyEvent.VK_A) {
|
|
||||||
set_y_bewegung(0);
|
|
||||||
set_x_bewegung(-speed);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
g.drawImage(currentImage, x, y, pacMan_width, pacMan_height, null);
|
||||||
|
}
|
||||||
|
|
||||||
public void set_y_bewegung(int y) {
|
|
||||||
this.ybewegung = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set_x_bewegung(int x) {
|
public void keyPressed(KeyEvent e) {
|
||||||
this.xbewegung = x;
|
if (e.getKeyCode() == KeyEvent.VK_W) {
|
||||||
}
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void move() {
|
public void set_y_bewegung(int y) {
|
||||||
y += ybewegung;
|
this.ybewegung = y;
|
||||||
x += xbewegung;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void check_kollision() {
|
public void set_x_bewegung(int x) {
|
||||||
|
this.xbewegung = x;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
// Bewegung updaten
|
||||||
|
public void move() {
|
||||||
|
y += ybewegung;
|
||||||
|
x += xbewegung;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void check_kollision(int windowWidth, int windowHeight) {
|
||||||
|
if (x < 0) x = 0;
|
||||||
|
if (y < 0) y = 0;
|
||||||
|
if (x + pacMan_width > windowWidth) x = windowWidth - pacMan_width;
|
||||||
|
if (y + pacMan_height > windowHeight) y = windowHeight - pacMan_height;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
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 {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
Thread test;
|
||||||
private JPanel contentPane;
|
ArrayList<ImageIcon> pacman;
|
||||||
|
JLabel setman;
|
||||||
|
int currentImageIndex = 0; // Um den aktuellen Index des Bildes zu verfolgen
|
||||||
|
|
||||||
|
test() {
|
||||||
|
setman = new JLabel();
|
||||||
|
pacman = new ArrayList<>();
|
||||||
|
|
||||||
public test() {
|
// Bilder zur Liste hinzufügen
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
pacman.add(new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmanright.png"));
|
||||||
setBounds(100, 100, 1000, 700);
|
pacman.add(new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmanopenright.png"));
|
||||||
contentPane = new JPanel();
|
// pacman.add(new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Pac_Man\\Pacman\\pacmandown.png"));
|
||||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
|
||||||
|
|
||||||
setContentPane(contentPane);
|
this.setTitle("Pacman Animation");
|
||||||
contentPane.setLayout(null);
|
this.setSize(700, 700);
|
||||||
|
this.setResizable(false);
|
||||||
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
this.setLocationRelativeTo(null);
|
||||||
|
this.add(setman);
|
||||||
|
|
||||||
JLabel lblNewLabel = new JLabel("New label");
|
// Thread starten
|
||||||
lblNewLabel.setBounds(230, 257, 169, 22);
|
test = new Thread(this);
|
||||||
contentPane.add(lblNewLabel);
|
test.start();
|
||||||
|
|
||||||
JLabel lblNewLabel_1 = new JLabel("New label");
|
this.setVisible(true);
|
||||||
lblNewLabel_1.setBounds(230, 95, 46, 144);
|
}
|
||||||
contentPane.add(lblNewLabel_1);
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new test();
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|