飞机大战,不报错,键盘按下不响应
#目前只是实现将飞机显示到屏幕上,并且想要通过w,s,a,d按键来控制移动方向
import sys
import traceback
from pygame.locals import *
import pygame
pygame.init()
pygame.mixer.init()
#载入游戏音乐
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.2)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)
class Myplane(pygame.sprite.Sprite):
def __init__(self,myplane_1_image,bg_size):
pygame.sprite.Sprite.__init__(self)#继承类
self.myplane_1_image = pygame.image.load(myplane_1_image).convert_alpha()
self.myplane_1_rect = self.myplane_1_image.get_rect()
self.myplane_1_rect.left,self.myplane_1_rect.top = \
(bg_size - self.myplane_1_rect.width) // 2 ,\
bg_size - self.myplane_1_rect.height
self.screen_width,self.screen_height = bg_size,bg_size
speed = 0
def move(self):
#如果按下W,A,S,D,表示飞机分别向上下左右移动
press_can_K_w = True
press_can_K_s = True
press_can_K_a = True
press_can_K_d = True
if self.myplane_1_rect.left< 0:#超过左边界,键盘a按下无效
press_can_K_a = False
if self.myplane_1_rect.left >self.screen_width - self.myplane_1_rect.width :
press_can_K_d = False
ifself.myplane_1_rect.top< 0 :
press_can_K_w = False
if self.myplane_1_rect.top>self.screen_height - self.myplane_1_rect.height:
press_can_K_d = False
def main():
pygame.init()
myplane_1_image = "image/me1.png"
bg_size = width,height = 480,700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load("image/background.png").convert()
BLACK = (0,0,0)
GREEN = (0,255,0)
RED = (255,0,0)
pygame.mixer.music.play(-1) #无限循环背景
clock = pygame.time.Clock()
pygame.key.set_repeat(100,100)
myplane = Myplane(myplane_1_image,bg_size)
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
myplane.move()
if event.type == K_w and press_can_K_w == True :
speed = -1
if event.type == K_s and press_can_K_s == True :
speed = 1
if event.type == K_a and press_can_K_a == True :
speed = -1
if event.type == K_d and press_can_K_d == True :
speed = 1
screen.blit(background,(0,0))
screen.blit(myplane.myplane_1_image,myplane.myplane_1_rect)
pygame.display.flip()
clock.tick(60)
if __name__ =="__main__":
#在双击打开文件时,如果出现异常,将异常显示出来
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
代码思路完全没对上啊,你既然设置了速度,但是又没有基于这个速度对图片位置进行过任何操作,你的move方法只加了限制又没有增加移动的情况图片的left和top都没有改变当然就不会动啊。
另外一点是脱离了边界要让False重新置回True的,只能说你的思路还是太绕了,导致你到处缺东西。。 Hoiste 发表于 2020-4-30 13:32
代码思路完全没对上啊,你既然设置了速度,但是又没有基于这个速度对图片位置进行过任何操作,你的move方法 ...
现在呢,你说的地方我尝试改了一下,可能还少点啥,还是动不了,求指点
import sys
import traceback
from pygame.locals import *
import pygame
pygame.init()
pygame.mixer.init()
#载入游戏音乐
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.2)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)
class Myplane(pygame.sprite.Sprite):
def __init__(self,myplane_1_image,bg_size):
pygame.sprite.Sprite.__init__(self)#继承类
self.myplane_1_image = pygame.image.load(myplane_1_image).convert_alpha()
self.myplane_1_rect = self.myplane_1_image.get_rect()
self.myplane_1_rect.left,self.myplane_1_rect.top = \
(bg_size - self.myplane_1_rect.width) // 2 ,\
bg_size - self.myplane_1_rect.height
self.screen_width,self.screen_height = bg_size,bg_size
self.speed =
def move(self):
#如果按下W,A,S,D,表示飞机分别向上下左右移动
press_can_K_w = True
press_can_K_s = True
press_can_K_a = True
press_can_K_d = True
if self.myplane_1_rect.left< 0:#超过左边界,键盘a按下无效
press_can_K_a = False
elif self.myplane_1_rect.left >self.screen_width - self.myplane_1_rect.width :
press_can_K_d = False
elifself.myplane_1_rect.top< 0 :
press_can_K_w = False
elif self.myplane_1_rect.top>self.screen_height - self.myplane_1_rect.height:
press_can_K_d = False
else:
press_can_K_w = True
press_can_K_s = True
press_can_K_a = True
press_can_K_d = True
def main():
pygame.init()
myplane_1_image = "image/me1.png"
bg_size = width,height = 480,700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load("image/background.png").convert()
BLACK = (0,0,0)
GREEN = (0,255,0)
RED = (255,0,0)
pygame.mixer.music.play(-1) #无限循环背景
clock = pygame.time.Clock()
pygame.key.set_repeat(100,100)
myplane = Myplane(myplane_1_image,bg_size)
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
myplane.move()
if event.type == K_w and press_can_K_w == True :
myplane.rect.center -= 1
if event.type == K_s and press_can_K_s == True :
myplane.rect.center -= 1
if event.type == K_a and press_can_K_a == True :
myplane.rect.center -= 1
if event.type == K_d and press_can_K_d == True :
myplane.rect.center += 1
screen.blit(background,(0,0))
screen.blit(myplane.myplane_1_image,myplane.myplane_1_rect)
pygame.display.flip()
clock.tick(60)
if __name__ =="__main__":
#在双击打开文件时,如果出现异常,将异常显示出来
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
还是把问题弄得太复杂了,边界按键失效比超过边界时把坐标锁在边界要多一些事件,而且主函数事件的缩进有点问题,如果坚持要用这个方法的话得重新设计一下,要不然给后面其他功能增加麻烦的。 Hoiste 发表于 2020-4-30 14:33
还是把问题弄得太复杂了,边界按键失效比超过边界时把坐标锁在边界要多一些事件,而且主函数事件的缩进有点 ...
而且在类里面设置的press_can_K_a等变量在外面访问要带带上对象,问题累积起来会让这个程序变得更加累赘。。。 按你的想法改好了,问题主要有几个:
一、你对边界的限制的几个变量应该作为属性放在__init__下面,并且和self绑定,下面对应的方法下的同名变量也改为同样的名字。
二、主函数main中的rect在Myplane类中叫做myplane_1_rect,所以没有rect这个属性报错。
三、rect.center是一个坐标元组,无法被修改,所以你应该改成按键改变left,top这些可修改的量。
四、运行的时候你应该就发现了,飞机的移动非常不流畅,这就是事件中操作移动带来的问题了,所以建议把整个代码改成小甲鱼课程里用的
key_pressed = pygame.key.get_pressed()
if key_pressed or key_pressed:
pass
if key_pressed or key_pressed:
pass
if key_pressed or key_pressed:
pass
if key_pressed or key_pressed:
pass
这种类型,具体的代码自己思考一下吧。
import sys
import traceback
from pygame.locals import *
import pygame
pygame.init()
pygame.mixer.init()
#载入游戏音乐
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.2)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)
class Myplane(pygame.sprite.Sprite):
def __init__(self,myplane_1_image,bg_size):
pygame.sprite.Sprite.__init__(self)#继承类
self.myplane_1_image = pygame.image.load(myplane_1_image).convert_alpha()
self.myplane_1_rect = self.myplane_1_image.get_rect()
self.myplane_1_rect.left,self.myplane_1_rect.top = \
(bg_size - self.myplane_1_rect.width) // 2 ,\
bg_size - self.myplane_1_rect.height
self.screen_width,self.screen_height = bg_size,bg_size
self.speed =
#如果按下W,A,S,D,表示飞机分别向上下左右移动
self.press_can_K_w = True
self.press_can_K_s = True
self.press_can_K_a = True
self.press_can_K_d = True
def move(self):
if self.myplane_1_rect.left< 0:#超过左边界,键盘a按下无效
self.press_can_K_a = False
elif self.myplane_1_rect.left >self.screen_width - self.myplane_1_rect.width :
self.press_can_K_d = False
elifself.myplane_1_rect.top< 0 :
self.press_can_K_w = False
elif self.myplane_1_rect.top>self.screen_height - self.myplane_1_rect.height:
self.press_can_K_d = False
else:
self.press_can_K_w = True
self.press_can_K_s = True
self.press_can_K_a = True
self.press_can_K_d = True
def main():
pygame.init()
myplane_1_image = "images/me1.png"
bg_size = width,height = 480,700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load("images/background.png").convert()
BLACK = (0,0,0)
GREEN = (0,255,0)
RED = (255,0,0)
pygame.mixer.music.play(-1) #无限循环背景
clock = pygame.time.Clock()
pygame.key.set_repeat(100,100)
myplane = Myplane(myplane_1_image,bg_size)
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
myplane.move()
if event.key == K_w and myplane.press_can_K_w == True :
myplane.myplane_1_rect.top -= 10
if event.key == K_s and myplane.press_can_K_s == True :
myplane.myplane_1_rect.top += 10
if event.key == K_a and myplane.press_can_K_a == True :
myplane.myplane_1_rect.left -= 10
if event.key == K_d and myplane.press_can_K_d == True :
myplane.myplane_1_rect.left += 10
screen.blit(background,(0,0))
screen.blit(myplane.myplane_1_image,myplane.myplane_1_rect)
pygame.display.flip()
clock.tick(60)
if __name__ =="__main__":
#在双击打开文件时,如果出现异常,将异常显示出来
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
Hoiste 发表于 2020-4-30 14:38
而且在类里面设置的press_can_K_a等变量在外面访问要带带上对象,问题累积起来会让这个程序变得更加累赘 ...
myplane.press_can_K_w == True这样? Hoiste 发表于 2020-4-30 14:53
按你的想法改好了,问题主要有几个:
一、你对边界的限制的几个变量应该作为属性放在__init__下面,并且和 ...
谢谢,万分感谢,这是自己的思路,确实乱 猪猪虾 发表于 2020-4-30 15:07
谢谢,万分感谢,这是自己的思路,确实乱
我发现把这改小一些,就不卡了
pygame.key.set_repeat(10,10)
页:
[1]