Alle Änderungen 04.09.2026 wichtig Hausaufgabe bis 11.09.2026 ist Player Klasse erstellen
This commit is contained in:
parent
737f7b693e
commit
bc007c2274
3 changed files with 78 additions and 89 deletions
51
game.py
51
game.py
|
|
@ -122,7 +122,6 @@ class Game:
|
|||
for wall in self.walls:
|
||||
pygame.draw.rect(self.display, "grey", wall)
|
||||
|
||||
player_pos_before = self.player_pos
|
||||
|
||||
velocity = self.speed * self.dt
|
||||
|
||||
|
|
@ -130,17 +129,39 @@ class Game:
|
|||
# Smooth Movement
|
||||
keys = pygame.key.get_pressed()
|
||||
|
||||
if keys[pygame.K_w]:
|
||||
self.player_pos.y -= velocity
|
||||
direction = pygame.Vector2(
|
||||
keys[pygame.K_d] - keys[pygame.K_a],
|
||||
keys[pygame.K_s] - keys[pygame.K_w]
|
||||
)
|
||||
|
||||
if keys[pygame.K_s]:
|
||||
self.player_pos.y += velocity
|
||||
movement = direction * self.speed * self.dt
|
||||
|
||||
if keys[pygame.K_a]:
|
||||
self.player_pos.x -= velocity
|
||||
self.player_pos += movement
|
||||
|
||||
if keys[pygame.K_d]:
|
||||
self.player_pos.x += velocity
|
||||
# player_rect.center = round(player_pos)
|
||||
self.player_rect.centerx = round(self.player_pos.x)
|
||||
|
||||
for wall in self.walls:
|
||||
if self.player_rect.colliderect(wall):
|
||||
if movement.x > 0:
|
||||
self.player_rect.right = wall.left
|
||||
|
||||
elif movement.x < 0:
|
||||
self.player_rect.left = wall.right
|
||||
|
||||
self.player_pos.x = self.player_rect.centerx
|
||||
|
||||
self.player_rect.centery = round(self.player_pos.y)
|
||||
|
||||
for wall in self.walls:
|
||||
if self.player_rect.colliderect(wall):
|
||||
if movement.y > 0:
|
||||
self.player_rect.bottom = wall.top
|
||||
|
||||
elif movement.y < 0:
|
||||
self.player_rect.top = wall.bottom
|
||||
|
||||
self.player_pos.y = self.player_rect.centery
|
||||
|
||||
|
||||
self.player_pos[1] = max(self.player_pos[1], self.player_height/2)
|
||||
|
|
@ -151,14 +172,6 @@ class Game:
|
|||
|
||||
self.player_pos[0] = min(self.player_pos[0], SCREEN_WIDTH/2-self.player_width/2)
|
||||
|
||||
for wall in self.walls:
|
||||
if self.collide_objects(wall, self.player_rect):
|
||||
self.player_pos = player_pos_before
|
||||
|
||||
if self.player_pos == player_pos_before:
|
||||
pass
|
||||
else:
|
||||
print("Ungleich")
|
||||
# print(f"Player Pos: {self.player_pos}, Player Pos Before: {player_pos_before}")
|
||||
|
||||
if self.rect_circle_collision(self.item_rect, self.flashlight_rect):
|
||||
|
|
@ -192,10 +205,6 @@ class Game:
|
|||
|
||||
return distance <= circle_r
|
||||
|
||||
def collide_objects(self, rect1: pygame.Rect, rect2: pygame.Rect) -> bool:
|
||||
if rect1.colliderect(rect2):
|
||||
return True
|
||||
|
||||
|
||||
game = Game()
|
||||
game.run()
|
||||
|
|
|
|||
|
|
@ -1,17 +1,5 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
import pygame
|
||||
|
||||
|
||||
@dataclass
|
||||
class Collisions:
|
||||
top: bool = False
|
||||
right: bool = False
|
||||
bottom: bool = False
|
||||
left: bool = False
|
||||
|
||||
|
||||
|
||||
# pygame setup
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((1280, 720))
|
||||
|
|
@ -23,11 +11,21 @@ player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
|
|||
|
||||
speed = 300
|
||||
|
||||
movement = [False, False, False, False]
|
||||
|
||||
velocity = [0, 0]
|
||||
|
||||
walls = [
|
||||
pygame.Rect(640, 260, 600, 30)
|
||||
]
|
||||
|
||||
player_rect = pygame.Rect(player_pos.x, player_pos.y, 40, 40)
|
||||
|
||||
player_rect.center = player_pos
|
||||
|
||||
while running:
|
||||
|
||||
dt = clock.tick(60) / 1000
|
||||
|
||||
# poll for events
|
||||
# pygame.QUIT event means the user clicked X to close your window
|
||||
|
||||
|
|
@ -36,76 +34,62 @@ while running:
|
|||
running = False
|
||||
|
||||
|
||||
|
||||
|
||||
wall_rect = pygame.Rect(640, 260, 600, 30)
|
||||
player_rect = pygame.Rect(player_pos.x, player_pos.y, 40, 40)
|
||||
|
||||
|
||||
# fill the screen with a color to wipe away anything from last frame
|
||||
screen.fill("purple")
|
||||
|
||||
for wall in walls:
|
||||
pygame.draw.rect(screen, "red", wall)
|
||||
|
||||
pygame.draw.rect(screen, "blue", player_rect)
|
||||
pygame.draw.rect(screen, "red", wall_rect)
|
||||
|
||||
velocity = speed * dt
|
||||
pygame.draw.line(screen, "yellow", (0, 0), player_pos)
|
||||
|
||||
|
||||
velocity = [0, 0]
|
||||
|
||||
|
||||
|
||||
keys = pygame.key.get_pressed()
|
||||
|
||||
if keys[pygame.K_w]:
|
||||
velocity -= speed * dt
|
||||
movement[0] = True
|
||||
|
||||
if keys[pygame.K_s]:
|
||||
velocity += speed * dt
|
||||
movement[2] = True
|
||||
|
||||
if keys[pygame.K_a]:
|
||||
velocity -= speed * dt
|
||||
movement[3] = True
|
||||
|
||||
if keys[pygame.K_d]:
|
||||
velocity += speed * dt
|
||||
movement[1] = True
|
||||
|
||||
collisions = Collisions()
|
||||
|
||||
frame_movement: tuple[float, float] = (
|
||||
(movement[0] + velocity[0]),
|
||||
movement[1] + velocity[1],
|
||||
direction = pygame.Vector2(
|
||||
keys[pygame.K_d] - keys[pygame.K_a],
|
||||
keys[pygame.K_s] - keys[pygame.K_w]
|
||||
)
|
||||
|
||||
entity_rect = player_rect
|
||||
if entity_rect.colliderect(wall_rect):
|
||||
if frame_movement[0] > 0:
|
||||
entity_rect.right = wall_rect.left
|
||||
collisions.right = True
|
||||
movement = direction * speed * dt
|
||||
|
||||
if frame_movement[0] < 0:
|
||||
entity_rect.left = wall_rect.right
|
||||
collisions.left = True
|
||||
player_pos += movement
|
||||
|
||||
player_pos.x = entity_rect.x
|
||||
# player_rect.center = round(player_pos)
|
||||
player_rect.centerx = round(player_pos.x)
|
||||
|
||||
for wall in walls:
|
||||
if player_rect.colliderect(wall):
|
||||
if movement.x > 0:
|
||||
player_rect.right = wall.left
|
||||
|
||||
player_pos[1] += frame_movement[1]
|
||||
elif movement.x < 0:
|
||||
player_rect.left = wall.right
|
||||
|
||||
entity_rect = player_rect
|
||||
player_pos.x = player_rect.centerx
|
||||
|
||||
player_rect.centery = round(player_pos.y)
|
||||
|
||||
for wall in walls:
|
||||
if player_rect.colliderect(wall):
|
||||
if movement.y > 0:
|
||||
player_rect.bottom = wall.top
|
||||
|
||||
elif movement.y < 0:
|
||||
player_rect.top = wall.bottom
|
||||
|
||||
player_pos.y = player_rect.centery
|
||||
|
||||
|
||||
if entity_rect.colliderect(wall_rect):
|
||||
if frame_movement[1] > 0:
|
||||
entity_rect.bottom = wall_rect.top
|
||||
collisions.bottom = True
|
||||
|
||||
if frame_movement[1] < 0:
|
||||
entity_rect.top = wall_rect.bottom
|
||||
collisions.top = True
|
||||
|
||||
player_pos[1] = entity_rect.y
|
||||
|
||||
movement = [False, False, False, False] # NORDEN OSTEN SÜDEN WESTEN
|
||||
|
||||
|
||||
|
||||
|
|
@ -115,6 +99,6 @@ while running:
|
|||
# limits FPS to 60
|
||||
# dt is delta time in seconds since last frame, used for framerate-
|
||||
# independent physics.
|
||||
dt = clock.tick(60) / 1000
|
||||
|
||||
|
||||
pygame.quit()
|
||||
|
|
@ -18,12 +18,8 @@ while running:
|
|||
screen.fill("purple")
|
||||
|
||||
# RENDER YOUR GAME HERE
|
||||
jays_vector = pygame.Vector2(100, 100)
|
||||
jays_anderer_vector = pygame.Vector2(600, 300)
|
||||
pygame.draw.line(screen, (0, 0, 0), (0, 0), jays_vector)
|
||||
pygame.draw.line(screen, (0, 0, 0), (0, 0), jays_anderer_vector)
|
||||
|
||||
pygame.draw.line(screen, (255, 255, 255), jays_vector, jays_anderer_vector)
|
||||
vector = pygame.Vector2(120, 110)
|
||||
print(2* vector)
|
||||
|
||||
|
||||
# flip() the display to put your work on screen
|
||||
|
|
|
|||
Loading…
Reference in a new issue