gpt4 book ai didi

python - 优化后我的 Sprite 不再显示了。为什么我的 Sprite 没有显示在我的 pygame 代码中?

转载 作者:太空宇宙 更新时间:2023-11-03 19:49:50 25 4
gpt4 key购买 nike

我在创建类并使用 def 作为角色时遇到了问题优化代码,就像我正在观看的视频一样。引用视频:https://www.youtube.com/watch?v=xfnRywBv5VM代码运行,但除了我的背景之外,我的 Sprite 不会显示。我不太确定出了什么问题,所以我只是发布了整个代码。我是 python、pygame 和一般编码的新手,所以请避免使用深奥的术语。谢谢!

# Initialize Pygame
import pygame
pygame.init()

# Game Window
window = pygame.display.set_mode((480, 480))

# Game Name
pygame.display.set_caption("Block Z-Day")

# Animation
walkLeft = pygame.image.load('Block Z-Day Sprites/1left.png')
walkRight = pygame.image.load('Block Z-Day Sprites/2right.png')
walkDown = pygame.image.load('Block Z-Day Sprites/3down.png')
walkUp = pygame.image.load('Block Z-Day Sprites/4up.png')
stand = pygame.image.load('Block Z-Day Sprites/5stand.png')
bg = pygame.image.load('Block Z-Day Sprites/background.jpg')
clock = pygame.time.Clock()


# Variable init (initialization)
class player():
def __init__(self, x_location, y_location, width, height):
# Character Starting Location and Size Declaration
self.x_location = x_location
self.y_location = y_location
self.width = width
self.height = height
# Character Speed Declaration
self.velocity = 3
# Declaration for the Main Loop
self.left = False
self.right = False
self.down = False
self.up = False
def character(self, window):
# Character Face
if self.left:
window.blit(walkLeft, (self.x_location, self.y_location))
elif self.right:
window.blit(walkRight, (self.x_location, self.y_location))
elif self.down:
window.blit(walkDown, (self.x_location, self.y_location))
elif self.up:
window.blit(walkUp, (self.x_location, self.y_location))
else:
window.blit(stand, (self.x_location, self.y_location))

# Character Attributes (void)
def gamewindow():
# Background
window.blit(bg, (0, 0))
# Shows the Character (refreshes display)
pygame.display.update()

# Main Loop
dude = player( 240, 240, 40, 50)
run = True
while run:
clock.tick(60)
# User Inputs
for event in pygame.event.get():
# Quit Button
if event.type == pygame.QUIT:
run = False
# Character Movement and Boundaries
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and dude.x_location > 0:
dude.x_location -= dude.velocity
dude.left = True
dude.right = False
dude.down = False
dude.up = False
elif keys[pygame.K_d] and dude.x_location < 480 - dude.width:
dude.x_location += dude.velocity
dude.left = False
dude.right = True
dude.down = False
dude.up = False
elif keys[pygame.K_s] and dude.y_location < 480 - dude.height:
dude.y_location += dude.velocity
dude.left = False
dude.right = False
dude.down = True
dude.up = False
elif keys[pygame.K_w] and dude.y_location > 0:
dude.y_location -= dude.velocity
dude.left = False
dude.right = False
dude.down = False
dude.up = True
else:
dude.left = False
dude.right = False
dude.down = False
dude.up = False
dude.stand = True
gamewindow()

最佳答案

您错过了在 gamewindow 中绘制玩家 (dude):

# Character Attributes (void)
def gamewindow():
# Background
window.blit(bg, (0, 0))

# draw character
dude.character(window) # <------

# Shows the Character (refreshes display)
pygame.display.update()

关于python - 优化后我的 Sprite 不再显示了。为什么我的 Sprite 没有显示在我的 pygame 代码中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59910413/

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