我试图在类中的 pygame 中绘制精灵,但是出现错误。
这是我的代码:
import pygame
# Initiate Pygame
pygame.init()
# Set The Screen Size
screen = pygame.display.set_mode([800, 600])
# Set The Window Title & Icon
pygame.display.set_caption("Pacman")
# Player
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Pacman_Player.tiff").convert
self.rect = self.image.get_rect()
self.draw = screen.blit(self.rect, 200, 200)
# Background Image
backImg_1 = pygame.image.load("maze.png").convert()
backImg = pygame.transform.scale(backImg_1, (800, 600))
# Game Loop
running = True
while running:
# Player
Player = Player()
# Background Image
screen.blit(backImg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
这是我尝试运行此代码时收到的错误消息:
File "/Users/nagz/Pacman/Pacman.py", line 34, in <module>
Player = Player()
File "/Users/nagz/Pacman/Pacman.py", line 19, in __init__
self.rect = self.image.get_rect()
AttributeError: 'builtin_function_or_method' object has no attribute 'get_rect'
错误消息说 pygame 中没有
get_rect
方法,但是 pygame 中有一个 get_rect
方法。
您需要在
处调用
Surface.convert()
函数
self.image = pygame.image.load("Pacman_Player.tiff").convert
成为
self.image = pygame.image.load("Pacman_Player.tiff").convert()
Surface.convert()
。
对于新错误(由OP在评论中提供),
File "/Users/nagz/Pacman/Pacman.py", line 34, in <module>
Player = Player() File "/Users/nagz/Pacman/Pacman.py", line 20, in __init__
self.draw = screen.blit(self.rect, 200, 200)
TypeError: argument 1 must be pygame.Surface, not pygame.Rect
只需更换
screen.blit(self.rect, 200, 200)
与
color = 255, 0, 0
pygame.draw.rect(screen, color 200, 200, self.rect.w, self.rect.h)
import pygame
# Initiate Pygame
pygame.init()
# Set The Screen Size
screen = pygame.display.set_mode([800, 600])
# Set The Window Title & Icon
pygame.display.set_caption("Pacman")
# Player
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Pacman_Player.tiff").convert()
self.rect = self.image.get_rect()
self.draw = screen.blit(self.rect, 200, 200)
# Background Image
backImg_1 = pygame.image.load("maze.png").convert()
backImg = pygame.transform.scale(backImg_1, (800, 600))
# Game Loop
running = True
while running:
# Player
Player = Player()
# Background Image
screen.blit(backImg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
您在
()
之后缺少 convert
,请将其更改为:
self.image = pygame.image.load("Pacman_Player.tiff").convert()
并更改:
self.draw = screen.blit(self.rect, 200, 200)
到
self.draw = lambda: screen.blit(self.image, (200, 200))
这样,每当你想绘制精灵时,你都可以调用
self.draw()
。
代码如下:
import pygame
# Initiate Pygame
pygame.init()
# Set The Screen Size
screen = pygame.display.set_mode([800, 600])
# Set The Window Title & Icon
pygame.display.set_caption("Pacman")
# Player
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Pacman_Player.tiff").convert()
self.rect = self.image.get_rect()
self.draw = lambda: screen.blit(self.image, (200,200))
# Background Image
backImg_1 = pygame.image.load("maze.png").convert()
backImg = pygame.transform.scale(backImg_1, (800, 600))
# Game Loop
running = True
while running:
# Player
Player = Player()
# Background Image
screen.blit(backImg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
类玩家(pygame.sprite.Sprite): def init(自身): 超级()。init() self.image = pygame.Surface((50, 50)) # 玩家图像占位符 self.image.fill(蓝色) self.rect = self.image.get_rect(center=(宽度 // 2, 高度 // 2)) self.speed = PLAYER_SPEED 自我健康 = 100 self.ammo = {'AK47': 30, 'M16A4': 30, 'MP5K': 30, '手枪1': 15, '手枪2': 15} self.weapon = 'AK47' self.项目符号 = [] self.knife_equipped = False
def update(self, keys):
# Handle movement
if keys[pygame.K_LEFT] and self.rect.left > 0:
self.rect.x -= self.speed
if keys[pygame.K_RIGHT] and self.rect.right < WIDTH:
self.rect.x += self.speed
if keys[pygame.K_UP] and self.rect.top > 0:
self.rect.y -= self.speed
if keys[pygame.K_DOWN] and self.rect.bottom < HEIGHT:
self.rect.y += self.speed
def shoot(self):
"""Shoot bullets."""
if self.weapon != 'Knife' and self.ammo[self.weapon] > 0:
bullet = Bullet(self.rect.centerx, self.rect.top, self.weapon)
self.bullets.append(bullet)
self.ammo[self.weapon] -= 1
return bullet
return None
def switch_weapon(self, weapon):
"""Switch weapons."""
if weapon in WEAPONS:
self.weapon = weapon
def use_knife(self):
"""Switch to knife mode."""
self.weapon = 'Knife'
self.knife_equipped = True
def draw_health(self):
"""Draw the player's health bar on the screen."""
health_bar = pygame.Rect(10, 10, HEALTH_BAR_WIDTH * (self.health / 100), HEALTH_BAR_HEIGHT)
pygame.draw.rect(screen, RED, health_bar)
def draw_ammo(self):
"""Display ammo count."""
ammo_text = font.render(f"Ammo: {self.ammo[self.weapon]}", True, BLACK)
screen.blit(ammo_text, (10, 40))