我正在用 python 制作一个基于冒险家的视频游戏。我让冒险家通过跳跃冷却时间四处移动,并且能够向左或向右移动。
import pygame
from sys import exit as end
import random
import time
pygame.init()
HEIGHT = 1200
WIDTH = 600
screen = pygame.display.set_mode((HEIGHT, WIDTH))
center_screen = (int(HEIGHT // 2), int(WIDTH // 2))
player_scale = 3
clock = pygame.time.Clock()
FPS = 60
# Land
class Land():
def __init__(self, x, y, type):
self.x = x
self.y = y
self.type = type
# Player
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, speed, grav):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.jumping = False
self.grav = grav
self.moving_left = False
self.moving_right = False
self.speed = speed
self.animation_list = []
self.frame_index = 0
self.time = pygame.time.get_ticks()
self.type = "idle"
for i in range(3):
img = pygame.image.load(f"player/{self.type}/{i}.png")
img = pygame.transform.scale(img, (int(img.get_width() * player_scale), int(img.get_height() * player_scale)))
self.animation_list.append(img)
self.player = img
self.player_rect = img.get_rect(center = (self.x, self.y))
def moving(self):
dx = 0
dy = 0
self.jump_cooldown = 1
if self.moving_left:
dx = -self.speed
if self.moving_right:
dx = self.speed
if self.jumping:
self.jump_cooldown = 0
dy = player.grav
player.grav += 1
if player.grav == 21:
self.jumping = False
self.jump_cooldown = 1
self.type = "idle"
self.player_rect.x += dx
self.player_rect.y += dy
def update_animation(self):
ANIMATION_COOLDOWN = 200
self.player = self.animation_list[self.frame_index]
if pygame.time.get_ticks() - self.time > ANIMATION_COOLDOWN:
self.time = pygame.time.get_ticks()
self.frame_index += 1
if self.frame_index == len(self.animation_list):
self.frame_index = 0
def draw(self):
screen.blit(self.player, self.player_rect)
player = Player(center_screen[0], center_screen[1], 5, 1)
run = True
while True:
screen.fill((1, 255, 122))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
end()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
player.moving_left = True
if event.key == pygame.K_d:
player.moving_right = True
if event.key == pygame.K_SPACE and player.jump_cooldown == 1:
player.type = "jump"
player.jumping = True
player.grav = -20
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
player.moving_left = False
if event.key == pygame.K_d:
player.moving_right = False
if run:
player.moving()
player.draw()
player.update_animation()
else:
pass
clock.tick(FPS)
pygame.display.update()
现在我想做的是让
self.type
在我按空格键时切换到“跳跃”。| ||(用于跳转和空闲的字符精灵是3个imgs,全部名称为0.png、1.png和2.png)
我尝试将其更改为函数来更改它。还是没用。
I tried changing it into a function to change it. It still didn't work.
几乎实现了!在代码中正确更改了
self.type
,但需要根据当前的
self.type
重新加载动画。
以下是如何修改的代码以实现此目的的方法:
import pygame
from sys import exit as end
import random
import time
pygame.init()
HEIGHT = 1200
WIDTH = 600
screen = pygame.display.set_mode((HEIGHT, WIDTH))
center_screen = (int(HEIGHT // 2), int(WIDTH // 2))
player_scale = 3
clock = pygame.time.Clock()
FPS = 60
# Land
class Land():
def __init__(self, x, y, type):
self.x = x
self.y = y
self.type = type
# Player
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, speed, grav):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.jumping = False
self.grav = grav
self.moving_left = False
self.moving_right = False
self.speed = speed
self.animation_list = []
self.frame_index = 0
self.time = pygame.time.get_ticks()
self.type = "idle"
self.load_animation() # 加载初始动画
self.player = self.animation_list[0] # 初始化 self.player
self.player_rect = self.player.get_rect(center = (self.x, self.y))
def load_animation(self):
"""根据 self.type 加载动画列表"""
self.animation_list.clear() # 清除之前的动画列表
for i in range(3):
img = pygame.image.load(f"player/{self.type}/{i}.png")
img = pygame.transform.scale(img, (int(img.get_width() * player_scale), int(img.get_height() * player_scale)))
self.animation_list.append(img)
def moving(self):
dx = 0
dy = 0
self.jump_cooldown = 1
if self.moving_left:
dx = -self.speed
if self.moving_right:
dx = self.speed
if self.jumping:
self.jump_cooldown = 0
dy = self.grav
self.grav += 1
if self.grav == 21:
self.jumping = False
self.jump_cooldown = 1
self.type = "idle"
self.load_animation() # 착지 시 애니메이션 다시 로드
self.player_rect.x += dx
self.player_rect.y += dy
def update_animation(self):
ANIMATION_COOLDOWN = 200
self.player = self.animation_list[self.frame_index]
if pygame.time.get_ticks() - self.time > ANIMATION_COOLDOWN:
self.time = pygame.time.get_ticks()
self.frame_index += 1
if self.frame_index == len(self.animation_list):
self.frame_index = 0
def draw(self):
screen.blit(self.player, self.player_rect)
player = Player(center_screen[0], center_screen[1], 5, 1)
run = True
while True:
screen.fill((1, 255, 122))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
end()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
player.moving_left = True
if event.key == pygame.K_d:
player.moving_right = True
if event.key == pygame.K_SPACE and player.jump_cooldown == 1:
player.type = "jump"
player.load_animation() # 加载跳跃动画
player.jumping = True
player.grav = -20
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
player.moving_left = False
if event.key == pygame.K_d:
player.moving_right = False
if run:
player.moving()
player.draw()
player.update_animation()
else:
pass
clock.tick(FPS)
pygame.display.update()
更改说明:
-
load_animation
函数: 此函数现在清除之前的animation_list
并根据当前的self.type
加载新的动画帧。 -
在
__init__
中加载初始动画: 我们在__init__
中调用self.load_animation()
来加载玩家的默认“空闲”动画。 -
在更改
self.type
后重新加载动画: 在moving
函数中,当玩家着陆并将self.type
设置回“空闲”时,以及在按下空格键并将self.type
设置为“跳跃”时,我们调用self.load_animation()
。
现在,的代码应该在按下空格键时正确切换到跳跃动画,并在玩家着陆时切换回空闲动画。
标签:python,pygame From: 78799760