我使用教程制作了这个动画,并将其设置为如果您面对右侧激活动画,但我所有的尝试移动动画到播放器的位置都失败了。动画需要使用空格键激活,并在onpress[pygame.K_Space]:函数中进行。代码发生在哪里并不是我关心的问题。
# --- Imports ---
import pygame, os, random
# --- Screen Dimensions ---
SCR_WIDTH = 1020
SCR_HEIGHT = 510
# --- Colors ---
WHITE = [240, 240, 240]
# --- Game Constants ---
FPS = 60
# --- Fonts ---
pygame.font.init()
TNR_FONT = pygame.font.SysFont('Times_New_Roman', 27)
TNR_LARGE_FONT = pygame.font.SysFont('Times_New_Roman', 65)
# --- Dictionaries ---
images = {}
# --- Classes ---
class MySprite(pygame.sprite.Sprite):
def __init__(self):
super(MySprite, self).__init__()
self.images = []
self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka1.png'))
self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka2.png'))
self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka3.png'))
self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka4.png'))
self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka5.png'))
self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka6.png'))
self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka7.png'))
self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka8.png'))
self.index = 0
self.image = self.images[self.index]
self.rect = pygame.Rect(5, 5, 150, 198)
def update(self):
self.index += 1
if self.index >= len(self.images):
self.index = 0
self.image = self.images[self.index]
# --- Functions
def clip(value, lower, upper):
return min(upper, max(value, lower))
def load_images():
path = 'Desktop/Files/Dungeon Minigame/'
filenames = [f for f in os.listdir(path) if f.endswith('.png')]
for name in filenames:
imagename = os.path.splitext(name)[0]
images[imagename] = pygame.image.load(os.path.join(path, name))
def game():
my_sprite = MySprite()
my_group = pygame.sprite.Group(my_sprite)
score = 0
lives = 3
playerX = 80
playerY = 100
direction = 'left'
def move(self, speed = 1):
if self.x > playerX:
self.x -= speed
elif self.x < playerX:
self.x += speed
if self.y < playerY:
self.y += speed
elif self.y > playerY:
self.y -= speed
def draw(self):
screen.blit(images['r_zombie'], (self.x, self.y))
while True:
screen.blit(images['background'], (0, 0))
score_text = TNR_FONT.render('Score: ' + str(score), True, WHITE)
lives_text = TNR_FONT.render('Lives: ', True, WHITE)
screen.blit(score_text, (20, 20))
screen.blit(lives_text, (840, 20))
heart_images = ["triple_empty_heart", "single_heart", "double_heart", "triple_heart"]
lives = clip(lives, 0, 3)
screen.blit(images[heart_images[lives]], (920, 0))
onpress = pygame.key.get_pressed()
dx = 0
if onpress[pygame.K_a]:
dx -= 3
direction = 'left'
if onpress[pygame.K_d]:
dx += 3
direction = 'right'
playerX += dx
dy = 0
if onpress[pygame.K_w]:
dy -= 3
if onpress[pygame.K_s]:
dy += 3
playerY += dy
if onpress[pygame.K_SPACE]:
if direction == 'right':
my_group.update()
my_group.draw(screen)
if dx > 0:
screen.blit(images['r_knight'], (playerX, playerY))
elif dx < 0:
screen.blit(images['l_knight'], (playerX, playerY))
if direction == 'left':
screen.blit(images['l_knight'], (playerX, playerY))
else:
screen.blit(images['r_knight'], (playerX, playerY))
playerX = clip(playerX, 8, 949)
playerY = clip(playerY, 5, 440)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
clock.tick(FPS)
pygame.display.update()
# --- Main ---
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((SCR_WIDTH, SCR_HEIGHT))
pygame.display.set_caption('Dungeon Minigame')
load_images()
game()发布于 2021-04-25 03:30:09
您必须设置MySprite对象的rect属性的位置。
my_sprite.rect.topleft = (playerX, playerY)pygame.sprite.Group.draw()和pygame.sprite.Group.update()是pygame.sprite.Group提供的方法。
前者将委托给所包含的pygame.sprite.Sprites的update方法-您必须实现该方法。请参阅pygame.sprite.Group.update()
在组中的所有Sprite上调用
update()方法...
后者使用包含的pygame.sprite.Sprite的image和rect属性来绘制对象-您必须确保pygame.sprite.Sprite具有所需的属性。请参阅pygame.sprite.Group.draw()
将包含的精灵绘制到曲面参数。这将使用源曲面的
Sprite.image属性和Sprite.rect。...
https://stackoverflow.com/questions/67246645
复制相似问题