python - 如何将精灵(图像)分配给矩形?
问题描述
我正在尝试学习如何使用 python 和 pygame 为项目创建迷宫游戏。我遇到的问题是,在所有示例中,我发现它们只有一个简单的正方形作为字符。我有一个 16x16px 的图像(我想使用的角色精灵),所以它是我正在使用的示例的正确尺寸,我什至设法让精灵加载一次。但是当它发生时,我的角色无法移动......
那么如何让这个矩形表示为我存储在与我的代码相同的文件夹中的 16x16px 图像而不是彩色方块?
这些是我在尝试更改播放器精灵时给我的结果的 2 个区域:
class Player(object):
def __init__(self):
self.rect = pygame.Rect(32, 32, 16, 16)
for wall in walls:
pygame.draw.rect(screen, (255, 255, 255), wall.rect)
pygame.draw.rect(screen, (255, 0, 0), end_rect)
pygame.draw.rect(screen, (255, 200, 0), player.rect)
pygame.display.flip()
任何帮助将非常感激。
(更新)完整游戏:
import os
import random
import pygame
IMAGE = pygame.image.load('mudkip.png')
all_sprites = pygame.sprite.Group()
# Class for character
class Player(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__(all_sprites)
# Get the image from the variable
self.image = IMAGE
# Get the rect from the image
self.rect = self.image.get_rect(topleft=pos)
def draw(self):
screen.blit(self.image, self.rect)
def move(self, dx, dy):
# Move each axis separately. Note that this checks for collisions both times.
if dx != 0:
self.move_single_axis(dx, 0)
if dy != 0:
self.move_single_axis(0, dy)
def move_single_axis(self, dx, dy):
# Move the rect
self.rect.x += dx
self.rect.y += dy
# If you collide with a wall, move out based on velocity
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0: # Moving right; Hit the left side of the wall
self.rect.right = wall.rect.left
if dx < 0: # Moving left; Hit the right side of the wall
self.rect.left = wall.rect.right
if dy > 0: # Moving down; Hit the top side of the wall
self.rect.bottom = wall.rect.top
if dy < 0: # Moving up; Hit the bottom side of the wall
self.rect.top = wall.rect.bottom
# Nice class to hold a wall rect
class Wall(object):
def __init__(self, pos):
walls.append(self)
self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
# Initialise pygame
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
# Set up the display
pygame.display.set_caption("Get to the red square!")
screen = pygame.display.set_mode((560, 240))
clock = pygame.time.Clock()
walls = [] # List to hold the walls
player = Player((30, 30)) # Create the player
# Holds the level layout in a list of strings.
level = [
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"W W W",
"W W WWWWWW W",
"W WWWW W W",
"W W WWWW wwwwww W",
"W WWW WWWW W",
"W W W W W",
"WE W W WWW W W",
"W WWW WWW W W W",
"W W W W W W",
"WWW W WWWWW W W",
"W W WW WWWWWWWWW W",
"W W WWWW WWW W",
"W W W W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]
# Parse the level string above. W = wall, E = exit
x = y = 0
for row in level:
for col in row:
if col == "W":
Wall((x, y))
if col == "E":
end_rect = pygame.Rect(x, y, 16, 16)
x += 16
y += 16
x = 0
running = True
while running:
clock.tick(60)
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
running = False
# Move the player if an arrow key is pressed
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
player.move(-2, 0)
if key[pygame.K_RIGHT]:
player.move(2, 0)
if key[pygame.K_UP]:
player.move(0, -2)
if key[pygame.K_DOWN]:
player.move(0, 2)
# Just added this to make it slightly fun ;)
if player.rect.colliderect(end_rect):
raise SystemExit('You win!')
# Draw the scene
all_sprites.draw(0)
screen.fill((0, 0, 0))
for wall in walls:
pygame.draw.rect(screen, (255, 255, 255), wall.rect)
pygame.draw.rect(screen, (255, 0, 0), end_rect)
pygame.display.flip()
all_sprites.draw() 出错:
all_sprites.draw()
TypeError: draw() missing 1 required positional argument: 'surface'
all_sprites.draw(0) 出错:
line 129, in <module>
all_sprites.draw(0)
lib\site-packages\pygame\sprite.py", line 474, in draw
surface_blit = surface.blit
AttributeError: 'int' object has no attribute 'blit'
解决方案
好的,所以您需要的第一件事是加载图像,看起来您已经完成了。
要把它放在你的精灵中,你必须这样做:
class Player(pygame.sprite.Sprite):
def __init__(self, pos): # Additional position argument, formatted like (x, y)
# Get the image from the variable
self.image = IMAGE
# Get the rect from the image
self.rect = self.image.get_rect(topleft=pos)
然后稍后,您添加一个draw
将您的角色添加到屏幕的功能。它相对简单。
def draw(self):
screen.blit(self.image, self.rect)
# Note, you may have move your global vars before the sprite classes to prevent a NameError now.
最后,你把它们和一些添加的东西放在一起,让你的新基类以后添加东西。
您不应该添加def draw
到主游戏循环中;)
# Earlier in the code...
all_sprites = pygame.sprite.Group() # This will allow all the sprites to be drawn in one fell swoop instead of it being done manually.
class Player(pygame.sprite.Sprite): # Make sure to inherit the sprite object!
def __init__(self, pos):
super().__init__(all_sprites) # Add the new sprite to the sprite group
self.image = IMAGE
# Get the rect from the image
self.rect = self.image.get_rect(topleft=pos)
def draw(self):
screen.blit(self.image, self.rect)
# Then in the game loop...
all_sprites.draw() # This will call draw() in every active sprite object.
推荐阅读
- python - 使用熊猫读取csv时如何指定行尾
- android - Parse 中的 initialize (com.parse.Parse.Configuration) 无法应用于 MainActivity 错误?
- javascript - 如何获取 OwlCarousel2 中上一个和下一个项目的索引?
- c# - 有没有理由在 c# 的构造函数中创建一个方法?
- javascript - 如何正确使用嵌套的异步代码?
- xpath - 如何使用 XPath 选择跟随兄弟
- c# - C# 创建的 ZIP 文件无效
- c# - 我无法从实例变量访问静态类字段
- c# - 如何检查我试图插入数据库的值是否已经存在
- c++ - 不是带有模板的类、命名空间或枚举