pygame飞机大战问题求助2
file:///E:/DXYF2415/Documents/WeChat%20Files/wxid_xrudwy5xte0n22/FileStorage/Video/2023-10/8c13fc48440b196af45ea73499a5a4dc.mp4麻烦大家查看一下这个视频,刚刚有几位鱼油帮忙还是没解决这个bug,搞不懂是怎么回事
myplane代码如下:
import pygame
class MyPlane(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('images/me1.png').convert_alpha()
self.rect = self.image.get_rect()
self.width, self.height = bg_size, bg_size
self.rect.left, self.rect.top = (self.width - self.rect.width) // 2, self.height - self.rect.height - 60
self.speed = 10
def moveUp(self):
if self.rect.top > 0:
self.rect.top -= self.speed
else:
self.rect.top = 0
def moveDown(self):
if self.rect.bottom < self.height:
self.rect.top += self.speed
else:
self.rect.bottom = self.height - 60
def moveLeft(self):
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.rect.left = 0
def moveRight(self):
if self.rect.right < self.width:
self.rect.right += self.speed
else:
self.rect.right = self.width
主体代码如下:
import pygame
import sys
import traceback
import myplane
from pygame.locals import *
pygame.init()
pygame.mixer.init()
bg_size = width, height = 480, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('飞机大战-- FishC Demo')
background = pygame.image.load('images/background.png').convert()
#载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.5)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)
def main():
pygame.mixer.music.play(-1)
me = myplane.MyPlane(bg_size)
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed or key_pressed:
me.moveUp()
if key_pressed or key_pressed:
me.moveDown()
if key_pressed or key_pressed:
me.moveLeft()
if key_pressed or key_pressed:
me.moveRight()
screen.blit(background, (0, 0))
screen.blit(me.image,me.rect)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input() 请问你的问题是什么,视频无法观看?如果是这样的话,可以描述一下你遇到的问题,我们会尽力帮你解决。
球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:} 根据视频中的现象,可以看出飞机移动时出现了闪烁的问题。这是因为在每次循环中,都会重新绘制背景和飞机,导致屏幕闪烁。解决方法是将背景和飞机的绘制放在游戏循环外面,只在飞机移动时更新飞机的位置即可。
修改后的代码如下:
import pygame
import sys
import traceback
import myplane
from pygame.locals import *
pygame.init()
pygame.mixer.init()
bg_size = width, height = 480, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('飞机大战-- FishC Demo')
background = pygame.image.load('images/background.png').convert()
#载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.5)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)
def main():
pygame.mixer.music.play(-1)
me = myplane.MyPlane(bg_size)
clock = pygame.time.Clock()
# 在游戏循环外面绘制背景和飞机
screen.blit(background, (0, 0))
screen.blit(me.image, me.rect)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed or key_pressed:
me.moveUp()
if key_pressed or key_pressed:
me.moveDown()
if key_pressed or key_pressed:
me.moveLeft()
if key_pressed or key_pressed:
me.moveRight()
# 只更新飞机的位置
screen.blit(background, (0, 0))
screen.blit(me.image, me.rect)
pygame.display.update(me.rect)
clock.tick(60)
if __name__ == '__main__':
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
希望能帮到你。 代码太长了,解决了就好!
页:
[1]