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 HEIGH_WINDOW = 700;
|
||||
Dimension fensterSize = new Dimension(WIDTH_WINDOW,HEIGH_WINDOW);
|
||||
|
||||
PacMan pacman;
|
||||
|
||||
Geist[] geists = new Geist[3];
|
||||
|
||||
ArrayList<SpielKarte> waende;
|
||||
|
@ -29,7 +29,7 @@ public class GameContainer extends JPanel implements Runnable {
|
|||
GameContainer(){
|
||||
this.waende = new ArrayList<>();
|
||||
draw_waende();
|
||||
draw_PacMan();
|
||||
draw_pacman();
|
||||
this.setFocusable(true);
|
||||
this.setPreferredSize(fensterSize);
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -67,9 +67,9 @@ public class GameContainer extends JPanel implements Runnable {
|
|||
for (SpielKarte w:waende )
|
||||
w.draw(g);
|
||||
|
||||
// Zeige Pacman
|
||||
pacman.draw(g);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void move() {
|
||||
|
@ -85,11 +85,18 @@ public class GameContainer extends JPanel implements Runnable {
|
|||
|
||||
}
|
||||
|
||||
if (pacman.y <= 5 || pacman.x <= 5) {
|
||||
pacman.set_x_bewegung(0);
|
||||
if (pacman.y <= 5 ) {
|
||||
pacman.set_y_bewegung(0);
|
||||
}
|
||||
|
||||
if (pacman.x < 0)
|
||||
pacman.x = WIDTH_WINDOW ;
|
||||
|
||||
if (pacman.x < 0)
|
||||
pacman.x = WIDTH_WINDOW ;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -100,7 +107,7 @@ public class GameContainer extends JPanel implements Runnable {
|
|||
move();
|
||||
checkKollision();
|
||||
repaint();
|
||||
Thread.sleep(25);
|
||||
Thread.sleep(20);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
@ -3,63 +3,114 @@ package Übungen.Pac_Man;
|
|||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class PacMan extends Rectangle {
|
||||
|
||||
private static int pacMan_width = 20;
|
||||
private static int pacMan_height = 20;
|
||||
private int ybewegung;
|
||||
private int xbewegung;
|
||||
private int speed = 8;
|
||||
private static int pacMan_width = 30;
|
||||
private static int pacMan_height = 30;
|
||||
private int ybewegung;
|
||||
private int xbewegung;
|
||||
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) {
|
||||
g.setColor(Color.GREEN);
|
||||
g.fillRect(x, y, pacMan_width, pacMan_height);
|
||||
}
|
||||
PacMan(int x, int y) {
|
||||
super(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) {
|
||||
set_y_bewegung(-speed);
|
||||
set_x_bewegung(0);
|
||||
}
|
||||
// Standardmäßig das rechte Bild anzeigen
|
||||
currentImage = pacmanImagesRight[0];
|
||||
}
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_S) {
|
||||
set_y_bewegung(speed);
|
||||
set_x_bewegung(0);
|
||||
}
|
||||
public void draw(Graphics g) {
|
||||
long currentTime = System.nanoTime();
|
||||
// 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) {
|
||||
set_y_bewegung(0);
|
||||
set_x_bewegung(speed);
|
||||
}
|
||||
// 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];
|
||||
|
||||
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) {
|
||||
this.xbewegung = x;
|
||||
}
|
||||
public void keyPressed(KeyEvent e) {
|
||||
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() {
|
||||
y += ybewegung;
|
||||
x += xbewegung;
|
||||
}
|
||||
public void set_y_bewegung(int y) {
|
||||
this.ybewegung = y;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
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;
|
||||
private JPanel contentPane;
|
||||
Thread test;
|
||||
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() {
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(100, 100, 1000, 700);
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
// 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"));
|
||||
|
||||
setContentPane(contentPane);
|
||||
contentPane.setLayout(null);
|
||||
this.setTitle("Pacman Animation");
|
||||
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");
|
||||
lblNewLabel.setBounds(230, 257, 169, 22);
|
||||
contentPane.add(lblNewLabel);
|
||||
// Thread starten
|
||||
test = new Thread(this);
|
||||
test.start();
|
||||
|
||||
JLabel lblNewLabel_1 = new JLabel("New label");
|
||||
lblNewLabel_1.setBounds(230, 95, 46, 144);
|
||||
contentPane.add(lblNewLabel_1);
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|