如何解决在 pygame 中创建一个带有运动的动画精灵
我正在制作一个游戏,到目前为止我设法创建了一个动画背景,这并不太难,因为它只是一个图像,但现在我想在移动时为我的主角添加动画,问题是如果我想移动角色,我必须定义它的矩形(“hitBox”),那么当我按下按键时发生的移动会应用到这个矩形,然后图像加载到矩形上,但我没有知道如何在仍将其放在矩形上的同时为该图像设置动画,并且仅在矩形移动时激活此动画。有人可以帮我在我的代码中找到一种方法来做到这一点。所以这是我的代码:
import pygame
import os
pygame.font.init()
pygame.init()
WIDTH,HEIGHT = 1920,1080
WIN = pygame.display.set_mode((WIDTH,HEIGHT),pygame.FULLSCREEN)
pygame.display.set_caption("first game")
down = False
WHITE = (255,255,255)
BLACK = (0,0)
RED = (255,0)
YELLOW = (255,0)
BORDER = pygame.Rect(WIDTH // 2 - 5,10,HEIGHT)
HEALTH_FONT = pygame.font.SysFont("comicsans",40)
WINNER_FONT = pygame.font.SysFont("comicsans",100)
FLOPPY_HEALTH = 10
COIN_HEALTH = 10
FPS = 60
VEL = 7
BULLET_VEL = 7
MAX_AMMO = 3
FLOPPY_HIT = pygame.USEREVENT + 1
COIN_HIT = pygame.USEREVENT + 2
main_char_height,main_char_width = 21,14
COIN_HEIGHT,COIN_WIDTH = 100,100
bg_animation = []
char_animation = []
main_char_x = 1000
main_char_y = 400
class Image:
def __init__(self,path,width,height,list):
self.path = path
self.width = width
self.height = height
self.image = pygame.image.load((self.path))
self.image = pygame.transform.scale(self.image,(self.width,self.height)).convert()
self.list = list
self.list.append(self.image)
bg_image1 = Image(os.path.join("assets","DUNGEON_PISKEL-1.png"),WIDTH,HEIGHT,bg_animation)
bg_image2 = Image(os.path.join("assets","DUNGEON_PISKEL-2.png"),bg_animation)
bg_image3 = Image(os.path.join("assets","DUNGEON_PISKEL-3.png"),bg_animation)
bg_image4 = Image(os.path.join("assets","DUNGEON_PISKEL-4.png"),bg_animation)
char_image1 = Image(os.path.join("assets","PISKEL_character_down-1.png"),main_char_width * 4,main_char_height * 4,char_animation)
char_image2 = Image(os.path.join("assets","PISKEL_character_down-2.png"),char_animation)
char_image3 = Image(os.path.join("assets","PISKEL_character_down-3.png"),char_animation)
char_image4 = Image(os.path.join("assets","PISKEL_character_down-4.png"),char_animation)
main_char_image = pygame.image.load(os.path.join("assets","PISKEL_character_down-2.png"))
main_char_image = pygame.transform.rotate(
pygame.transform.scale(main_char_image,(main_char_width * 4,main_char_height * 4)),0)
COIN_IMAGE = pygame.image.load(os.path.join("assets","coin.png")).convert()
COIN_IMAGE = pygame.transform.scale(COIN_IMAGE,(COIN_HEIGHT,COIN_WIDTH))
bgcount = 0
walkcount = 0
def draw_window(x,y,character,coin,floppy_bullet,coin_bullets,FLOPPY_HEALTH,COIN_HEALTH):
global bgcount,walkcount,down
global char_animation
if bgcount + 1 >= 40:
bgcount = 0
WIN.blit(bg_animation[bgcount // 10],(-13,-10))
bgcount += 1
WIN.blit(main_char_image,(x,y))
floppy_health_text = HEALTH_FONT.render("HEALTH :" + str(FLOPPY_HEALTH),1,WHITE)
WIN.blit(floppy_health_text,(10,10))
coin_health_text = HEALTH_FONT.render("HEALTH :" + str(COIN_HEALTH),WHITE)
WIN.blit(coin_health_text,(WIDTH - coin_health_text.get_width() - 10,10))
for bullet in floppy_bullet:
pygame.draw.rect(WIN,RED,bullet)
for bullet in coin_bullets:
pygame.draw.rect(WIN,bullet)
WIN.blit(COIN_IMAGE,(coin.x,coin.y))
pygame.display.update()
def handle_movement_WASD(keys_pressed,object,x,y):
global down
global main_char_y
global main_char_x
global walkcount
down = False
if keys_pressed[pygame.K_a] and x - VEL > 40: # left
main_char_x -= VEL
if keys_pressed[pygame.K_d] and x - VEL < WIDTH - 220: # right
main_char_x += VEL
if keys_pressed[pygame.K_w] and y - VEL > 100: # up
main_char_y -= VEL
if keys_pressed[pygame.K_s] and y + VEL + object.height < HEIGHT: # down
main_char_y += VEL
down=True
else:
down= False
walkcount = 0
"""def handle_movement_arrows(keys_pressed,y):
if keys_pressed[pygame.K_LEFT] and x + VEL > 115: # left
x -= VEL
if keys_pressed[pygame.K_RIGHT] and x - VEL < WIDTH - 220: # right
x += VEL
if keys_pressed[pygame.K_UP] and y - VEL > 110: # up
y -= VEL
if keys_pressed[pygame.K_DOWN] and y + VEL + object.height < HEIGHT - 60: # down
y += VEL"""
def handle_bullets(floppy_bullet,coin_bullet,floppy,coin):
for bullet in floppy_bullet:
bullet.x += BULLET_VEL
if coin.colliderect(bullet):
pygame.event.post(pygame.event.Event(COIN_HIT))
floppy_bullet.remove(bullet)
elif bullet.x > WIDTH:
floppy_bullet.remove(bullet)
for bullet in coin_bullet:
bullet.x -= BULLET_VEL
if floppy.colliderect(bullet):
pygame.event.post(pygame.event.Event(FLOPPY_HIT))
coin_bullet.remove(bullet)
elif bullet.x < 0:
coin_bullet.remove(bullet)
def draw_winner(text):
draw_text = WINNER_FONT.render(text,WHITE)
WIN.blit(draw_text,(WIDTH / 2 - draw_text.get_width() / 2,HEIGHT / 2 - draw_text.get_height() / 2))
pygame.display.update()
pygame.time.delay(5000)
def main():
main_character = pygame.Rect(main_char_x,main_char_y,main_char_height,main_char_width)
coin = pygame.Rect(1400,400,COIN_HEIGHT,COIN_WIDTH)
floppy_bullet = []
coin_bullet = []
FLOPPY_HEALTH = 10
COIN_HEALTH = 10
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LCTRL and len(floppy_bullet) < MAX_AMMO:
bullet = pygame.Rect(main_character.x + main_character.width,main_character.y + main_character.height // 2 - 2,5)
floppy_bullet.append(bullet)
if event.key == pygame.K_RCTRL and len(coin_bullet) < MAX_AMMO:
bullet = pygame.Rect(coin.x,coin.y + coin.height // 2 - 2,5)
coin_bullet.append(bullet)
if event.type == FLOPPY_HIT:
FLOPPY_HEALTH -= 1
if event.type == COIN_HIT:
COIN_HEALTH -= 1
winner_text = ""
if FLOPPY_HEALTH <= 0:
winner_text = "COIN WINS"
if COIN_HEALTH <= 0:
winner_text = "FLOPPY WINS"
if winner_text != "":
draw_winner(winner_text)
break
keys_pressed = pygame.key.get_pressed()
handle_movement_WASD(keys_pressed,main_character,main_char_x,main_char_y)
#handle_movement_arrows(keys_pressed,coin)
handle_bullets(floppy_bullet,coin)
draw_window(main_char_x,COIN_HEALTH)
pygame.quit()
if __name__ == "__main__":
main()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。