gpt4 book ai didi

python - 如何将 Sprite (图像)分配给矩形?

转载 作者:行者123 更新时间:2023-12-01 08:04:42 26 4
gpt4 key购买 nike

我正在尝试学习如何使用 python 和 pygame 为项目创建迷宫游戏。我遇到的问题是,在所有示例中,我发现它们只有一个简单的正方形作为字符。我有一个 16x16px 的图像(我想要使用的角色 Sprite ),因此它对于我正在使用的示例来说是正确的尺寸,而且我什至设法让 Sprite 加载一次。但当它发生时,我的角色无法移动......

那么如何让这个矩形表示为我存储在与代码相同的文件夹中的 16x16px 图像,而不是彩色方 block ?

这些是我在尝试更改玩家 Sprite 时给出的两个区域:

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'

最佳答案

好吧,所以您需要的第一件事就是加载图像,看起来您已经完成了。

要将其放入您的 Sprite 中,您必须执行以下操作:

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 - 如何将 Sprite (图像)分配给矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55594922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com