master
3009594 2024-09-17 21:33:58 +02:00
parent ebed469694
commit 73eab43f6a
17 changed files with 174 additions and 78 deletions

Binary file not shown.

Binary file not shown.

View File

@ -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) {
}
}

View File

@ -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();
} }

View File

@ -3,46 +3,93 @@ 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;
Image currentImage;
int animationIndex = 0;
long lastTime = System.nanoTime();
PacMan(int x, int y) {
super(x, y, pacMan_width, pacMan_height); 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()
};
// 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;
} }
public void keyPressed(KeyEvent e) { // 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) { if (e.getKeyCode() == KeyEvent.VK_W) {
currentImage = pacmanImagesUp[animationIndex];
set_y_bewegung(-speed); set_y_bewegung(-speed);
set_x_bewegung(0); set_x_bewegung(0);
} }
if (e.getKeyCode() == KeyEvent.VK_S) { if (e.getKeyCode() == KeyEvent.VK_S) {
currentImage = pacmanImagesDown[animationIndex];
set_y_bewegung(speed); set_y_bewegung(speed);
set_x_bewegung(0); set_x_bewegung(0);
} }
if (e.getKeyCode() == KeyEvent.VK_D) { if (e.getKeyCode() == KeyEvent.VK_D) {
currentImage = pacmanImagesRight[animationIndex];
set_y_bewegung(0); set_y_bewegung(0);
set_x_bewegung(speed); set_x_bewegung(speed);
} }
if (e.getKeyCode() == KeyEvent.VK_A) { if (e.getKeyCode() == KeyEvent.VK_A) {
currentImage = pacmanImagesLeft[animationIndex];
set_y_bewegung(0); set_y_bewegung(0);
set_x_bewegung(-speed); set_x_bewegung(-speed);
} }
} }
public void set_y_bewegung(int y) { public void set_y_bewegung(int y) {
@ -53,13 +100,17 @@ public class PacMan extends Rectangle {
this.xbewegung = x; this.xbewegung = x;
} }
// Bewegung updaten
public void move() { public void move() {
y += ybewegung; y += ybewegung;
x += xbewegung; x += xbewegung;
} }
public void check_kollision() { 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;
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 741 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -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();
}
}
} }
} }