用pygame中的图像替换矩形
我是Pygame和Python的新手。今天,我试图编码简化的上层运动。我做到了,它运行没有任何问题。但是无论如何我都有问题:“玩家”是一个矩形,但我希望他成为图像或类似的东西。有没有办法将RECT转换为图像?
哦,这是您需要的代码,我是从一个星期前提出的另一个问题(或更长的时间)提出的:
import pygame
pygame.init()
win = pygame.display.set_mode((700, 700))
pygame.display.set_caption("TopDown")
clock = pygame.time.Clock()
player = pygame.Rect(350, 350, 40, 60)
vel = 1
run = True
while run:
clock.tick(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
player.x += (keys[pygame.K_d] - keys[pygame.K_a]) * vel
player.y += (keys[pygame.K_s] - keys[pygame.K_w]) * vel
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 255, 255), player)
pygame.display.update()
pygame.quit()
I am new to pygame and python in general. Today I was trying to code a simplified TopDown movement. I did it and it runs without any issues. But i've got a problem anyways: The "player" is a rectangle but I want him to be an image or something like that. Is there a way to 'convert' a rect to an image?
Oh, and here's the code if you need it, I made it from another question I asked a week ago (or a bit longer):
import pygame
pygame.init()
win = pygame.display.set_mode((700, 700))
pygame.display.set_caption("TopDown")
clock = pygame.time.Clock()
player = pygame.Rect(350, 350, 40, 60)
vel = 1
run = True
while run:
clock.tick(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
player.x += (keys[pygame.K_d] - keys[pygame.K_a]) * vel
player.y += (keys[pygame.K_s] - keys[pygame.K_w]) * vel
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 255, 255), player)
pygame.display.update()
pygame.quit()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将图像加载
pygame.image.image.image.image.load()
。此功能返回apygame.surface.surface
将 surface 放在显示屏上,“ nofollow noreferrer”>
pygame.surface.blit
:最小示例:
Load the image with
pygame.image.load()
. This function returns apygame.Surface
object:Put the Surface onto the display with
pygame.Surface.blit
:Minimal example: