add tip list
This commit is contained in:
parent
7b9d7734ab
commit
c733cf64e1
12 changed files with 598 additions and 187 deletions
42
player.py
Normal file
42
player.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import pygame
|
||||
from settings import PLAYER_SIZE
|
||||
|
||||
class Player:
|
||||
def __init__(self, x, y, image):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.alive = True
|
||||
self.rect = pygame.Rect(x, y, PLAYER_SIZE, PLAYER_SIZE)
|
||||
self.dir = "up"
|
||||
self.image = image
|
||||
|
||||
def move(self, keys, map_w, map_h):
|
||||
if not self.alive:
|
||||
return
|
||||
dx = dy = 0
|
||||
if keys[pygame.K_w]:
|
||||
dy -= 5
|
||||
self.dir = "up"
|
||||
if keys[pygame.K_s]:
|
||||
dy += 5
|
||||
self.dir = "down"
|
||||
if keys[pygame.K_a]:
|
||||
dx -= 5
|
||||
self.dir = "left"
|
||||
if keys[pygame.K_d]:
|
||||
dx += 5
|
||||
self.dir = "right"
|
||||
self.x = max(0, min(map_w - PLAYER_SIZE, self.x + dx))
|
||||
self.y = max(0, min(map_h - PLAYER_SIZE, self.y + dy))
|
||||
self.rect.topleft = (self.x, self.y)
|
||||
|
||||
def draw(self, win, ox, oy):
|
||||
if not self.alive:
|
||||
return
|
||||
img = self.image
|
||||
angle = {"up": 90, "right": 0, "down": -90, "left": 180}[self.dir]
|
||||
if img:
|
||||
rotated_img = pygame.transform.rotate(img, angle)
|
||||
win.blit(rotated_img, (self.x - ox, self.y - oy))
|
||||
else:
|
||||
pygame.draw.rect(win, (200, 200, 0), (self.x - ox, self.y - oy, PLAYER_SIZE, PLAYER_SIZE))
|
||||
Loading…
Add table
Add a link
Reference in a new issue