from github

This commit is contained in:
jonas
2024-10-29 17:18:30 +01:00
commit b0f85b5c87
63 changed files with 1781 additions and 0 deletions
+102
View File
@@ -0,0 +1,102 @@
import math
import time
from MoveAble import MoveAble
from Vec import Vec
import pygame as pg
class Player(MoveAble):
def __init__(self, start_position: Vec, start_size: Vec, speed, grid_cell_size, maze):
# Make the speed an even number
# speed += 0 if (speed % 2 == 0 or speed == 1) == 0 else 1
super().__init__(start_position, start_size, speed)
self.grid_cell_size = grid_cell_size
self.targetDirection = Vec(0, 0)
self.correct = False
self.score = 0
self.last_state_switched = time.time()
self.state = False
self.hp = 3
self.last_time_hit = 0
self.maze = maze
# Überschreibt den Vektor, der die Laufrichtung PacMans darstellt
def poll_keys(self, maze):
keys = pg.key.get_pressed()
free_y = self.position.x % self.grid_cell_size == 0
free_x = self.position.y % self.grid_cell_size == 0
if keys[pg.K_LEFT] or keys[pg.K_a]:
self.targetDirection = Vec(-1, 0)
if keys[pg.K_RIGHT] or keys[pg.K_d]:
self.targetDirection = Vec(1, 0)
if keys[pg.K_UP] or keys[pg.K_w]:
self.targetDirection = Vec(0, -1)
if keys[pg.K_DOWN] or keys[pg.K_s]:
self.targetDirection = Vec(0, 1)
allowed = maze.is_free(
Vec(round(self.position.x / self.grid_cell_size) + self.targetDirection.x,
round(self.position.y / self.grid_cell_size) + self.targetDirection.y))
# Erst wenn die gewünschte Laufrichtung begehbar ist, verändert sich die Laufrichtung PacMans.
if self.targetDirection.x != 0 and free_x and allowed:
self.velocity = Vec(self.targetDirection.x * self.speed, self.targetDirection.y * self.speed)
self.targetDirection = Vec(0, 0)
# Erst wenn die gewünschte Laufrichtung begehbar ist, verändert sich die Laufrichtung PacMans.
if self.targetDirection.y != 0 and free_y and allowed:
self.velocity = Vec(self.targetDirection.x * self.speed, self.targetDirection.y * self.speed)
self.targetDirection = Vec(0, 0)
def auto_place(self):
x = 15
for y in range(0, 29):
if self.maze.is_free(Vec(x, y)):
self.position = Vec(x * self.grid_cell_size, y * self.grid_cell_size)
break
self.velocity = Vec(0, 0)
def move(self):
# Rechne die Position von 0 - Bildschirmbreite, 0 - Bildschirmhöhe in 0-30, 0-30 um
test = Vec(self.position.x / self.grid_cell_size,
self.position.y / self.grid_cell_size)
# Mache die Komponenten (je nach richtung der Geschwindigkeit) zu Integer-Datentypen.
test.x = math.ceil(test.x) if self.velocity.x < 0 else math.floor(test.x)
test.y = math.ceil(test.y) if self.velocity.y < 0 else math.floor(test.y)
# Speichere die Richtung der Geschwindigkeit als Vektor der länge 1 ab
check_direction = self.velocity.get_normalized()
check_direction.to_int()
# Füge diese Richtung der Test (Position von Pacman im 30x30 Gitter) hinzu
test.add(check_direction)
# Überprüfe, ob das Labyrinth an der Stelle des resultierenden test Vektor frei ist (keine Wand bzw. 0)
if self.maze.is_free(test):
super().move()
def get_rotation(self):
player_rot = 0
if self.velocity.x < 0:
player_rot = 180
if self.velocity.y < 0:
player_rot = 90
if self.velocity.y > 0:
player_rot = -90
return player_rot
def get_state(self):
if time.time() - self.last_state_switched > 0.2:
self.state = not self.state
self.last_state_switched = time.time()
return self.state