如何解决我在 pygame 上显示图像时遇到问题,看起来有些滞后而且我也有声音延迟问题我试过减少缓冲区
我试图编写一个简单的游戏来提高自己在 pygame 上的能力,我在我的代码中没有看到任何问题,但是敌人的图像看起来很滞后,就像它有时会消失并在很短的时间内恢复.我不认为这与我的硬件有关。它不仅仅是一个文件,它是多个相互导入的文件,所以我将它们全部保留下来。呃,顺便说一句,代码不是很短,由于字符限制,我无法发布整个代码,所以这是我遇到问题的可能部分。我也有关于 hit_sound 的声音延迟问题,我尝试减少缓冲区。不完全有效。
P.S:下面可能有一些语法问题,可能是因为我复制粘贴了代码。原始代码中没有任何语法,所以如果您看到任何内容,请不要介意。
enemy_class.py
import pygame,sys,random,warrior_class,main
class enemycl():
def __init__(self):
pygame.init()
self.enemy_skull_time = pygame.time.get_ticks()
self.enemy_skull_animation_delay_time = 50
self.ani_sayac_enemy_skull = 0
self.enemy_skull_former_move = 2
self.enemy_skull_move = self.enemy_skull_former_move
self.enemy_skull_move_time = pygame.time.get_ticks()
self.enemy_skull_move_time_delay = 100
self.enemy_skull_x = 100
self.enemy_skull_y = warrior_on_enemy.ground_level - 10
self.enemy_skull_look_right = True
self.push_count_max = 10
self.push_count = self.push_count_max
self.push_distance = 40
self.enemy_skull_max_hp = 100
self.enemy_skull_hp = self.enemy_skull_max_hp
enemy_animation_skull0 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0000.png')).convert_alpha()
enemy_animation_skull1 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0001.png')).convert_alpha()
enemy_animation_skull2 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0002.png')).convert_alpha()
enemy_animation_skull3 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0003.png')).convert_alpha()
enemy_animation_skull4 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0004.png')).convert_alpha()
enemy_animation_skull5 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0005.png')).convert_alpha()
enemy_animation_skull6 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0006.png')).convert_alpha()
enemy_animation_skull7 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0007.png')).convert_alpha()
enemy_animation_skull8 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0008.png')).convert_alpha()
enemy_animation_skull9 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0009.png')).convert_alpha()
enemy_animation_skull10 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0010.png')).convert_alpha()
enemy_animation_skull11 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0011.png')).convert_alpha()
enemy_animation_skull12 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0012.png')).convert_alpha()
enemy_animation_skull13 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0013.png')).convert_alpha()
enemy_animation_skull14 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0014.png')).convert_alpha()
enemy_animation_skull15 = pygame.transform.scale2x(pygame.image.load(
'assets/enemy_animation/floating_skull_enemy/style_A/PNG/frame0015.png')).convert_alpha()
self.enemy_animation_skull_dead = pygame.transform.scale2x(pygame.image.load('assets/enemy_animation/floating_skull_enemy/style_A/PNG/enemy_skull_dead.png'))
self.animation_list_enemy_skull = [
enemy_animation_skull0,enemy_animation_skull1,enemy_animation_skull2,enemy_animation_skull3,enemy_animation_skull4,enemy_animation_skull5,enemy_animation_skull6,enemy_animation_skull7,enemy_animation_skull8,enemy_animation_skull9,enemy_animation_skull10,enemy_animation_skull11,enemy_animation_skull12,enemy_animation_skull13,enemy_animation_skull14,enemy_animation_skull15
]
self.enemy_skull_image = enemy_animation_skull0
self.enemy_skull_mask = pygame.mask.from_surface(self.enemy_skull_image)
self.warrior_mask = pygame.mask.from_surface(warrior_on_enemy.character_img)
self.iscolliding = False
def draw(self):
if self.enemy_skull_hp > 0:
self.enemy_skull_image = self.animation_list_enemy_skull[self.ani_sayac_enemy_skull]
if pygame.time.get_ticks() - self.enemy_skull_time >= self.enemy_skull_animation_delay_time:
if self.ani_sayac_enemy_skull < len(self.animation_list_enemy_skull) - 1:
self.ani_sayac_enemy_skull += 1
elif self.ani_sayac_enemy_skull >= len(self.animation_list_enemy_skull) - 1:
self.ani_sayac_enemy_skull = 0
self.enemy_skull_time = pygame.time.get_ticks()
if self.enemy_skull_look_right:
pass
elif not self.enemy_skull_look_right:
self.enemy_skull_image = pygame.transform.flip(self.enemy_skull_image,True,False)
if pygame.time.get_ticks() - self.enemy_skull_move_time >= self.enemy_skull_move_time_delay:
if self.enemy_skull_x <= 0 or self.enemy_skull_x >= 880:
self.enemy_skull_move = -self.enemy_skull_move
self.enemy_skull_former_move = self.enemy_skull_move
if self.enemy_skull_move > 0:
self.enemy_skull_look_right = True
elif self.enemy_skull_move < 0:
self.enemy_skull_look_right = False
self.enemy_skull_x += self.enemy_skull_move
elif self.enemy_skull_hp <= 0 and self.enemy_skull_look_right:
self.enemy_skull_image = self.enemy_animation_skull_dead
elif self.enemy_skull_hp <= 0 and not self.enemy_skull_look_right:
self.enemy_skull_image = pygame.transform.flip(self.enemy_animation_skull_dead,False)
pygame.draw.rect(main.maincm.window,(255,0),(735,25,200,25))
pygame.draw.rect(main.maincm.window,(0,255,(735+((self.enemy_skull_max_hp-self.enemy_skull_hp)*2),self.enemy_skull_hP*2,25))
main.maincm.window.blit(self.enemy_skull_image,(self.enemy_skull_x,self.enemy_skull_y))
pygame.display.update()
def game_loop(self):
self.draw()
self.warrior_rect = warrior_class.warriorc.character_img.get_rect(
topleft=(warrior_on_enemy.player_x,warrior_on_enemy.player_y))
self.enemy_skull_rect = self.enemy_skull_image.get_rect(
topleft=(self.enemy_skull_x,warrior_class.warriorc.ground_level - 10))
self.enemy_skull_mask = pygame.mask.from_surface(self.enemy_skull_image)
self.warrior_mask = pygame.mask.from_surface(warrior_on_enemy.character_img)
self.offset = (
(round(warrior_on_enemy.player_x - self.enemy_skull_x)),round((warrior_on_enemy.player_y - self.enemy_skull_y)))
self.iscolliding = self.enemy_skull_mask.overlap(self.warrior_mask,self.offset)
if self.push_count < self.push_count_max:
if not warrior_on_enemy.look_right:
self.enemy_skull_x -= self.push_distance/self.push_count_max
self.push_count += 1
elif warrior_on_enemy.look_right:
self.enemy_skull_x += self.push_distance / self.push_count_max
self.push_count += 1
warrior_on_enemy = warrior_class.animation()
enemy_on_enemy = enemycl()
main.py
import pygame
class display_class():
def __init__(self):
pygame.init()
pygame.mixer.init()
# Setting display.
self.window_width = 960
self.window_height = 540
self.window = pygame.display.set_mode((self.window_width,self.window_height))
self.clock = pygame.time.Clock()
pygame.display.set_caption('Warrior')
icon = pygame.image.load('Adventurer-1.5/Individual_Sprites/status/sword_shte/adventurer-swrd-shte-00.png')
pygame.display.set_icon(icon)
self.FPS = 75
maincm = display_class()
unit_collision.py
import pygame,enemy_class,main
pygame.mixer.pre_init(44100,-16,1,512)
pygame.init()
hit_sound = pygame.mixer.sound('assets/melee_sounds/minecraft_Hit_-_Sound_Effect__143020_01032021.wav')
rect_color = (0,255)
collisiontime = pygame.time.get_ticks()
first_collide = 0
def collision():
global rect_color
global first_collide
if enemy_class.enemy_on_enemy.iscolliding:
if not enemy_class.warrior_on_enemy.isattacking and enemy_class.enemy_on_enemy.enemy_skull_hp > 0:
if first_collide < 1:
enemy_class.warrior_on_enemy.warrior_health -= 10
first_collide += 1
elif enemy_class.warrior_on_enemy.isattacking:
if first_collide < 1:
enemy_class.enemy_on_enemy.enemy_skull_hp -= 5
hit_sound.play(loops=0,maxtime=0,fade_ms=0)
enemy_class.enemy_on_enemy.enemy_skull_move = 0
enemy_class.enemy_on_enemy.push_count = 0
first_collide += 1
rect_color = (255,0)
else:
if not -enemy_class.enemy_on_enemy.enemy_skull_former_move <= enemy_class.enemy_on_enemy.enemy_skull_move <= enemy_class.enemy_on_enemy.enemy_skull_former_move or enemy_class.enemy_on_enemy.enemy_skull_move == 0:
enemy_class.enemy_on_enemy.enemy_skull_move = enemy_class.enemy_on_enemy.enemy_skull_former_move
rect_color = (0,255)
first_collide = 0
if False:
pygame.draw.rect(main.maincm.window,rect_color,enemy_class.enemy_on_enemy.warrior_rect,1)
pygame.draw.rect(main.maincm.window,enemy_class.enemy_on_enemy.enemy_skull_rect,1)
pygame.display.update()
warrior_calisir.py
import pygame,main,unit_collision
warrior = warrior_class.animation()
enemy = enemy_class.enemycl()
while True:
enemy_class.warrior_on_enemy.game_loop()
enemy_class.enemy_on_enemy.game_loop()
unit_collision.collision()
main.maincm.clock.tick(main.maincm.FPS)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。