Compare commits

..

No commits in common. "0476dadfa4f0770c6e42fc7b4d192a82a236a0b6" and "6c535d3073b2c792a66e057c1248f271fc020c06" have entirely different histories.

53
game.py
View file

@ -151,15 +151,17 @@ class Player:
self.game = game self.game = game
self.pos = pos self.pos = pos
self.width = width self.player_width = width
self.height = height self.player_height = height
self.speed = 150 self.speed = 150
self.rect = pygame.Rect(self.pos[0]-self.width/2, self.pos[1]-self.height/2, width, height) self.rect = pygame.Rect(self.pos[0]-self.player_width/2, self.pos[1]-self.player_height/2, width, height)
def update(self, walls, dt): def update(self, walls, dt):
self.player_rect = pygame.Rect(self.pos[0]-10, self.pos[1]-10, self.player_width, self.player_height)
# Smooth Movement # Smooth Movement
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
@ -178,57 +180,54 @@ class Player:
self.pos.x += movement.x self.pos.x += movement.x
if self.pos.x - self.width/2 < 0:
self.pos.x = self.width/2
if self.pos.x + self.width/2 > SCREEN_WIDTH/2:
self.pos.x = SCREEN_WIDTH/2 - self.width/2
# self.pos += movement # self.pos += movement
# player_rect.center = round(player_pos) # player_rect.center = round(player_pos)
self.rect.centerx = round(self.pos.x) self.player_rect.centerx = round(self.pos.x)
for wall in walls: for wall in walls:
if self.rect.colliderect(wall): if self.player_rect.colliderect(wall):
if movement.x > 0: if movement.x > 0:
self.rect.right = wall.left self.player_rect.right = wall.left
elif movement.x < 0: elif movement.x < 0:
self.rect.left = wall.right self.player_rect.left = wall.right
self.pos.x = self.rect.centerx self.pos.x = self.player_rect.centerx
# if self.pos.x < self.player_rect.width/2 or self.pos.x + self.player_rect.width > SCREEN_WIDTH:
# self.pos.x = old_position_x
if self.pos.x - self.player_rect.width/2 < 0:
self.pos.x = self.player_rect.width/2
if self.pos.x + self.player_rect.width/2 > SCREEN_WIDTH:
self.pos.x = SCREEN_WIDTH - self.player_rect.width/2
self.pos.y += movement.y self.pos.y += movement.y
if self.pos.y - self.height/2 < 0: self.player_rect.centery = round(self.pos.y)
self.pos.y = self.height/2
if self.pos.y + self.height/2 > SCREEN_HEIGHT/2:
self.pos.y = SCREEN_HEIGHT/2 - self.height/2
self.rect.centery = round(self.pos.y)
for wall in walls: for wall in walls:
if self.rect.colliderect(wall): if self.player_rect.colliderect(wall):
if movement.y > 0: if movement.y > 0:
self.rect.bottom = wall.top self.player_rect.bottom = wall.top
elif movement.y < 0: elif movement.y < 0:
self.rect.top = wall.bottom self.player_rect.top = wall.bottom
self.pos.y = self.rect.centery
self.pos.y = self.player_rect.centery
if self.pos.y - self.player_rect.height/2 < 0:
self.pos.y = self.player_rect.height/2
if self.pos.y + self.player_rect.height/2 > SCREEN_HEIGHT:
self.pos.y = SCREEN_HEIGHT - self.player_rect.height/2
def render(self, surface): def render(self, surface):
pygame.draw.rect(surface, (255, 0, 0), self.rect) pygame.draw.rect(surface, (255, 0, 0), self.player_rect)