funktioniert nicht
This commit is contained in:
parent
d3d80fc8f0
commit
8b42ab04f1
12 changed files with 134 additions and 101 deletions
Binary file not shown.
|
Before Width: | Height: | Size: 68 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 64 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 143 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 78 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 97 KiB |
23
game.py
23
game.py
|
|
@ -30,7 +30,7 @@ class Game:
|
|||
self.dt: int = 0
|
||||
# Spieler
|
||||
self.player_pos = pygame.Vector2(
|
||||
self.screen.get_width() / 2, self.screen.get_height() / 2
|
||||
self.screen.get_width() / 2, self.screen.get_height() / 2-200
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -38,9 +38,9 @@ class Game:
|
|||
self.assets = {
|
||||
"floor_01": load_image("level/ground/floor_1.png"),
|
||||
"plant_01": load_image("items/furniture/plants/plant.png"),
|
||||
"wall": load_image("level/walls/wall_0.png"),
|
||||
"wall": load_image("level/walls/wall_0.png").convert_alpha(),
|
||||
|
||||
"player": load_image("entity/player/player.png"),
|
||||
"player": load_image("entity/player/player.png").convert_alpha(),
|
||||
}
|
||||
self.player = Player(self.player_pos, width=PLAYER_SIZE, height=PLAYER_SIZE, image=self.assets["player"])
|
||||
self.flashlight_player = Flashlight(self, self.player)
|
||||
|
|
@ -49,13 +49,14 @@ class Game:
|
|||
game=self,
|
||||
items=[],
|
||||
walls=[
|
||||
Wall(self, 0, 0, SCREEN_WIDTH//TILE_SIZE, 1),
|
||||
Wall(self, 0, TILE_SIZE, 1, SCREEN_HEIGHT//TILE_SIZE),
|
||||
Wall(
|
||||
self, TILE_SIZE, SCREEN_HEIGHT - TILE_SIZE, SCREEN_WIDTH//TILE_SIZE, 1
|
||||
),
|
||||
Wall(self, SCREEN_WIDTH - TILE_SIZE, 0, 1, SCREEN_HEIGHT//TILE_SIZE),
|
||||
Wall(self, 500, 500, 2, 2)
|
||||
# Wall(self, 0, 0, SCREEN_WIDTH//TILE_SIZE, 1),
|
||||
# Wall(self, 0, TILE_SIZE, 1, SCREEN_HEIGHT//TILE_SIZE),
|
||||
# Wall(
|
||||
# self, TILE_SIZE, SCREEN_HEIGHT - TILE_SIZE, SCREEN_WIDTH//TILE_SIZE, 1
|
||||
# ),
|
||||
# Wall(self, SCREEN_WIDTH - TILE_SIZE, 0, 1, SCREEN_HEIGHT//TILE_SIZE),
|
||||
# Wall(self, 500, 500, 2, 2)
|
||||
Wall(self, 300, 300, 1, 1)
|
||||
],
|
||||
)
|
||||
|
||||
|
|
@ -95,7 +96,7 @@ class Game:
|
|||
self.gegner.update(self.dt, (1, 1))
|
||||
|
||||
self.player.update(walls=self.walls, dt=self.dt)
|
||||
|
||||
self.level.update()
|
||||
self.flashlight_player.update()
|
||||
|
||||
for item in self.items:
|
||||
|
|
|
|||
|
|
@ -15,6 +15,9 @@ class Level:
|
|||
# Level
|
||||
self.walls = walls
|
||||
|
||||
def update(self):
|
||||
for wall in self.walls:
|
||||
wall.update()
|
||||
def render(self, surface: pygame.Surface):
|
||||
for y in range(ROWS):
|
||||
for x in range(COLS):
|
||||
|
|
|
|||
|
|
@ -5,15 +5,14 @@ from scripts.constants import *
|
|||
|
||||
|
||||
class Player:
|
||||
def __init__(self, pos: pygame.Vector2, width, height, image=None):
|
||||
def __init__(self, pos: pygame.Vector2, width, height, image: pygame.Surface=None):
|
||||
self.pos: Vector2 = pos
|
||||
self.width: float = width
|
||||
self.height: float = height
|
||||
self.speed = 150
|
||||
self.image = image
|
||||
self.rect = pygame.Rect(
|
||||
self.pos[0] - self.width / 2, self.pos[1] - self.height / 2, width, height
|
||||
)
|
||||
self.mask = pygame.mask.from_surface(self.image)
|
||||
self.rect = image.get_frect()
|
||||
|
||||
|
||||
def update(self, walls, dt):
|
||||
|
|
@ -25,6 +24,8 @@ class Player:
|
|||
if direction.length_squared() > 0:
|
||||
direction = direction.normalize()
|
||||
|
||||
|
||||
|
||||
movement: pygame.Vector2 = direction * self.speed * dt
|
||||
|
||||
self.pos.x += movement.x
|
||||
|
|
@ -36,12 +37,14 @@ class Player:
|
|||
self.rect.centerx = round(self.pos.x)
|
||||
|
||||
for wall in walls:
|
||||
wall = wall.get_rect()
|
||||
if self.rect.colliderect(wall):
|
||||
offset = (int(self.rect.x) - int(wall.rect.x), int(self.rect.y) - int(wall.rect.y))
|
||||
|
||||
wall_rect = wall.get_rect()
|
||||
if self.mask.overlap(wall.mask, offset):
|
||||
if movement.x > 0:
|
||||
self.rect.right = wall.left
|
||||
self.rect.right = wall_rect.left
|
||||
elif movement.x < 0:
|
||||
self.rect.left = wall.right
|
||||
self.rect.left = wall_rect.right
|
||||
self.pos.x = self.rect.centerx
|
||||
|
||||
self.pos.y += movement.y
|
||||
|
|
@ -53,12 +56,14 @@ class Player:
|
|||
self.rect.centery = round(self.pos.y)
|
||||
|
||||
for wall in walls:
|
||||
wall = wall.get_rect()
|
||||
if self.rect.colliderect(wall):
|
||||
offset = (int(self.rect.x) - wall.rect.x, int(self.rect.y) - wall.rect.y)
|
||||
|
||||
wall_rect = wall.get_rect()
|
||||
if self.mask.overlap(wall.mask, offset):
|
||||
if movement.y > 0:
|
||||
self.rect.bottom = wall.top
|
||||
self.rect.bottom = wall_rect.top
|
||||
elif movement.y < 0:
|
||||
self.rect.top = wall.bottom
|
||||
self.rect.top = wall_rect.bottom
|
||||
self.pos.y = self.rect.centery
|
||||
|
||||
|
||||
|
|
@ -67,6 +72,6 @@ class Player:
|
|||
pygame.draw.rect(surface, self.color, self.rect)
|
||||
else:
|
||||
surface.blit(
|
||||
self.image,
|
||||
self.mask.to_surface(),
|
||||
(self.pos[0]-TILE_SIZE/2, self.pos[1]-TILE_SIZE/2-10, self.image.get_width(), self.image.get_height()),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -10,10 +10,11 @@ class Wall:
|
|||
self.y = y
|
||||
self.cols = cols
|
||||
self.rows = rows
|
||||
self.rect = pygame.Rect(x, y, cols*TILE_SIZE, rows*TILE_SIZE)
|
||||
self.image = self.game.assets["wall"]
|
||||
self.rect = pygame.FRect(x, y, cols*TILE_SIZE, rows*TILE_SIZE)
|
||||
self.mask = self.mask = pygame.Mask(self.rect.size, fill=True)
|
||||
self.drawed_rect = None
|
||||
self.grid: list[list[pygame.Rect]] = []
|
||||
self.image = self.game.assets["wall"]
|
||||
|
||||
for row in range(self.rows): # rows
|
||||
current_row = []
|
||||
|
|
@ -22,14 +23,16 @@ class Wall:
|
|||
|
||||
self.grid.append(current_row)
|
||||
|
||||
self.see_rect = self.mask.get_rect()
|
||||
print(f"X: {self.see_rect.x}, Y: {self.see_rect.y}")
|
||||
def update(self):
|
||||
pass
|
||||
|
||||
def render(self, surface: pygame.Surface):
|
||||
for row in self.grid:
|
||||
for wall_rect in row:
|
||||
surface.blit(self.image, wall_rect)
|
||||
surface.blit(self.mask.to_surface(), wall_rect)
|
||||
|
||||
pygame.draw.rect(surface, "red", self.see_rect, 1)
|
||||
|
||||
def get_rect(self) -> pygame.Rect:
|
||||
return self.rect
|
||||
|
|
|
|||
|
|
@ -1,104 +1,92 @@
|
|||
from pathlib import Path
|
||||
|
||||
import pygame
|
||||
from player import Player
|
||||
|
||||
# pygame setup
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((1280, 720))
|
||||
|
||||
WINDOW_WIDTH = 640
|
||||
WINDOW_HEIGHT = 580
|
||||
CENTER_X = WINDOW_WIDTH / 2
|
||||
CENTER_Y = WINDOW_HEIGHT / 2
|
||||
|
||||
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
|
||||
clock = pygame.time.Clock()
|
||||
speed = 200
|
||||
radius = 50
|
||||
|
||||
player_pos = pygame.Vector2(CENTER_X - 50, CENTER_Y - 50)
|
||||
player = Player("white", pygame.image.load(Path("./testen/player.png")), player_pos)
|
||||
obstacles = [pygame.Rect((CENTER_X + 100, CENTER_Y - 100), (200, 200))]
|
||||
|
||||
running = True
|
||||
dt = 0
|
||||
|
||||
player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
|
||||
|
||||
speed = 300
|
||||
|
||||
|
||||
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
|
||||
dt = clock.tick(60) / 1000.0
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
player.update(obstacles)
|
||||
|
||||
movement = dt * speed
|
||||
|
||||
|
||||
|
||||
|
||||
# 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.line(screen, "yellow", (0, 0), player_pos)
|
||||
|
||||
|
||||
velocity = [0, 0]
|
||||
|
||||
|
||||
movement_x = 0
|
||||
movement_y = 0
|
||||
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_w]:
|
||||
movement_y -= movement
|
||||
if keys[pygame.K_s]:
|
||||
movement_y += movement
|
||||
if keys[pygame.K_a]:
|
||||
movement_x -= movement
|
||||
if keys[pygame.K_d]:
|
||||
movement_x += movement
|
||||
|
||||
steps = int(movement) + 1
|
||||
step_x = movement_x / steps
|
||||
step_y = movement_y / steps
|
||||
|
||||
direction = pygame.Vector2(
|
||||
keys[pygame.K_d] - keys[pygame.K_a],
|
||||
keys[pygame.K_s] - keys[pygame.K_w]
|
||||
)
|
||||
for step in range(steps):
|
||||
previous_x = player.pos.x
|
||||
player.pos.x += step_x
|
||||
|
||||
movement = direction * speed * dt
|
||||
if player.pos.x - player.rect.width/2 < 0:
|
||||
player.pos.x = player.rect.width/2
|
||||
if player.pos.x + player.rect.width/2 > WINDOW_WIDTH:
|
||||
player.pos.x = WINDOW_WIDTH - player.rect.width/2
|
||||
|
||||
player_pos += movement
|
||||
if player.overlaps_rect(obstacle):
|
||||
player.pos.x = previous_x
|
||||
|
||||
# player_rect.center = round(player_pos)
|
||||
player_rect.centerx = round(player_pos.x)
|
||||
previous_y = player.pos.y
|
||||
player.pos.y += step_y
|
||||
|
||||
for wall in walls:
|
||||
if player_rect.colliderect(wall):
|
||||
if movement.x > 0:
|
||||
player_rect.right = wall.left
|
||||
if player.pos.y - player.rect.height/2 < 0:
|
||||
player.pos.y = player.rect.height/2
|
||||
if player.pos.y + player.rect.height/2 > WINDOW_HEIGHT:
|
||||
player.pos.y = WINDOW_HEIGHT - player.rect.height/2
|
||||
|
||||
elif movement.x < 0:
|
||||
player_rect.left = wall.right
|
||||
|
||||
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 player.overlaps_rect(obstacle):
|
||||
player.pos.y = previous_y
|
||||
|
||||
|
||||
|
||||
window.fill(player.color)
|
||||
|
||||
pygame.draw.rect(window, "grey", obstacle)
|
||||
player.render(window)
|
||||
|
||||
# flip() the display to put your work on screen
|
||||
pygame.display.flip()
|
||||
|
||||
# limits FPS to 60
|
||||
# dt is delta time in seconds since last frame, used for framerate-
|
||||
# independent physics.
|
||||
|
||||
|
||||
pygame.quit()
|
||||
|
||||
|
||||
"""
|
||||
Hausaufgabe: Pixelgenaue Kollision zwischen dem Spielerbild und der Wand ohne ruckeln, geschmeidig + MASKE D:
|
||||
|
||||
'Die Hausaufgabe ist, eine pixelgenaue Kollision zwischen dem Spielerbild und der Wand hinzubekommen, ohne zu ruckeln,
|
||||
sodass du dich geschmeidig entlang der Wand bewegen kannst und eine Maske verwendest.'
|
||||
|
||||
"""
|
||||
BIN
testen/player.png
Normal file
BIN
testen/player.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
33
testen/player.py
Normal file
33
testen/player.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import pygame
|
||||
|
||||
|
||||
class Player:
|
||||
def __init__(self, color, image: pygame.Surface, pos: pygame.Vector2):
|
||||
self.pos = pos
|
||||
self.image = image
|
||||
self.rect = image.get_frect(center=pos)
|
||||
self.mask = pygame.mask.from_surface(self.image)
|
||||
self.color = color
|
||||
|
||||
def overlaps_rect(self, rect):
|
||||
pass
|
||||
|
||||
def update(self, obstacle: pygame.Rect):
|
||||
self.rect.x = self.pos.x
|
||||
self.rect.y = self.pos.y
|
||||
|
||||
# offset = (int(self.rect.x) - int(obstacle.x), int(self.rect.y) - int(obstacle.y))
|
||||
# obstacle_mask = pygame.Mask((obstacle.width, obstacle.height))
|
||||
|
||||
# if self.mask.overlap(obstacle_mask, offset=offset):
|
||||
# print("OVERLAP")
|
||||
|
||||
# if self.rect.colliderect(obstacle):
|
||||
# self.color = "red"
|
||||
|
||||
# else:
|
||||
# self.color = "white"
|
||||
|
||||
def render(self, surface: pygame.Surface):
|
||||
surface.blit(self.image, self.rect)
|
||||
|
||||
Loading…
Reference in a new issue