鱼C论坛

 找回密码
 立即注册
查看: 1242|回复: 8

[已解决]飞机大战,不报错,键盘按下不响应

[复制链接]
发表于 2020-4-30 12:21:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

#目前只是实现将飞机显示到屏幕上,并且想要通过w,s,a,d按键来控制移动方向


  1. import sys
  2. import traceback
  3. from pygame.locals import *
  4. import pygame

  5. pygame.init()
  6. pygame.mixer.init()


  7. #载入游戏音乐
  8. pygame.mixer.music.load("sound/game_music.ogg")
  9. pygame.mixer.music.set_volume(0.2)
  10. bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
  11. bullet_sound.set_volume(0.2)
  12. bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
  13. bomb_sound.set_volume(0.2)
  14. supply_sound = pygame.mixer.Sound("sound/supply.wav")
  15. supply_sound.set_volume(0.2)
  16. get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
  17. get_bomb_sound.set_volume(0.2)
  18. get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
  19. get_bullet_sound.set_volume(0.2)
  20. upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
  21. upgrade_sound.set_volume(0.2)
  22. enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
  23. enemy3_fly_sound.set_volume(0.2)
  24. enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
  25. enemy1_down_sound.set_volume(0.2)
  26. enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
  27. enemy2_down_sound.set_volume(0.2)
  28. enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
  29. enemy3_down_sound.set_volume(0.2)
  30. me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
  31. me_down_sound.set_volume(0.2)


  32. class Myplane(pygame.sprite.Sprite):
  33.     def __init__(self,myplane_1_image,bg_size):
  34.         pygame.sprite.Sprite.__init__(self)  #继承类

  35.         self.myplane_1_image = pygame.image.load(myplane_1_image).convert_alpha()
  36.         self.myplane_1_rect = self.myplane_1_image.get_rect()
  37.         self.myplane_1_rect.left,self.myplane_1_rect.top = \
  38.                                  (bg_size[0] - self.myplane_1_rect.width) // 2 ,\
  39.                                   bg_size[1] - self.myplane_1_rect.height
  40.         self.screen_width,self.screen_height = bg_size[0],bg_size[1]
  41.         speed = 0

  42.     def move(self):
  43.         #如果按下W,A,S,D,表示飞机分别向上下左右移动
  44.         press_can_K_w = True
  45.         press_can_K_s = True
  46.         press_can_K_a = True
  47.         press_can_K_d = True
  48.         
  49.         if self.myplane_1_rect.left  < 0  :  #超过左边界,键盘a按下无效
  50.             press_can_K_a = False

  51.         if self.myplane_1_rect.left >  self.screen_width - self.myplane_1_rect.width :
  52.             press_can_K_d = False

  53.         if  self.myplane_1_rect.top  < 0 :
  54.             press_can_K_w = False
  55.             
  56.         if self.myplane_1_rect.top>  self.screen_height - self.myplane_1_rect.height:
  57.             press_can_K_d = False
  58.    

  59. def main():
  60.     pygame.init()
  61.     myplane_1_image = "image/me1.png"
  62.     bg_size = width,height = 480,700
  63.     screen = pygame.display.set_mode(bg_size)
  64.     pygame.display.set_caption("飞机大战")
  65.     background = pygame.image.load("image/background.png").convert()

  66.     BLACK = (0,0,0)
  67.     GREEN = (0,255,0)
  68.     RED = (255,0,0)

  69.    
  70.     pygame.mixer.music.play(-1)   #无限循环背景
  71.     clock = pygame.time.Clock()
  72.     pygame.key.set_repeat(100,100)
  73.     myplane = Myplane(myplane_1_image,bg_size)   
  74.     running = True
  75.     while running:
  76.         for event in pygame.event.get():
  77.             if event.type == QUIT:
  78.                 pygame.quit()
  79.                 sys.exit()

  80.         if event.type == KEYDOWN:
  81.                 myplane.move()
  82.                 if event.type == K_w and press_can_K_w == True :
  83.                    speed[1] = -1
  84.                                       
  85.                 if event.type == K_s and press_can_K_s == True :
  86.                    speed[1] = 1   

  87.                 if event.type == K_a and press_can_K_a == True :
  88.                    speed[1] = -1   

  89.                 if event.type == K_d and press_can_K_d == True :
  90.                    speed[1] = 1   
  91.                   
  92.         screen.blit(background,(0,0))
  93.         screen.blit(myplane.myplane_1_image,myplane.myplane_1_rect)

  94.         
  95.         pygame.display.flip()
  96.         clock.tick(60)
  97.         
  98.         
  99. if __name__ =="__main__":
  100.     #在双击打开文件时,如果出现异常,将异常显示出来
  101.     try:
  102.         main()
  103.     except SystemExit:
  104.         pass
  105.     except:
  106.         traceback.print_exc()
  107.         pygame.quit()
  108.         input()

复制代码
最佳答案
2020-4-30 14:53:44
按你的想法改好了,问题主要有几个:
一、你对边界的限制的几个变量应该作为属性放在__init__下面,并且和self绑定,下面对应的方法下的同名变量也改为同样的名字。
二、主函数main中的rect在Myplane类中叫做myplane_1_rect,所以没有rect这个属性报错。
三、rect.center是一个坐标元组,无法被修改,所以你应该改成按键改变left,top这些可修改的量。
四、运行的时候你应该就发现了,飞机的移动非常不流畅,这就是事件中操作移动带来的问题了,所以建议把整个代码改成小甲鱼课程里用的
key_pressed = pygame.key.get_pressed()
if key_pressed[K_w] or key_pressed[K_UP]:
    pass
if key_pressed[K_s] or key_pressed[K_DOWN]:
    pass
if key_pressed[K_a] or key_pressed[K_LEFT]:
    pass
if key_pressed[K_d] or key_pressed[K_RIGHT]:
    pass
这种类型,具体的代码自己思考一下吧。
            
  1. import sys
  2. import traceback
  3. from pygame.locals import *
  4. import pygame

  5. pygame.init()
  6. pygame.mixer.init()


  7. #载入游戏音乐
  8. pygame.mixer.music.load("sound/game_music.ogg")
  9. pygame.mixer.music.set_volume(0.2)
  10. bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
  11. bullet_sound.set_volume(0.2)
  12. bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
  13. bomb_sound.set_volume(0.2)
  14. supply_sound = pygame.mixer.Sound("sound/supply.wav")
  15. supply_sound.set_volume(0.2)
  16. get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
  17. get_bomb_sound.set_volume(0.2)
  18. get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
  19. get_bullet_sound.set_volume(0.2)
  20. upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
  21. upgrade_sound.set_volume(0.2)
  22. enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
  23. enemy3_fly_sound.set_volume(0.2)
  24. enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
  25. enemy1_down_sound.set_volume(0.2)
  26. enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
  27. enemy2_down_sound.set_volume(0.2)
  28. enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
  29. enemy3_down_sound.set_volume(0.2)
  30. me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
  31. me_down_sound.set_volume(0.2)


  32. class Myplane(pygame.sprite.Sprite):
  33.     def __init__(self,myplane_1_image,bg_size):
  34.         pygame.sprite.Sprite.__init__(self)  #继承类

  35.         self.myplane_1_image = pygame.image.load(myplane_1_image).convert_alpha()
  36.         self.myplane_1_rect = self.myplane_1_image.get_rect()
  37.         self.myplane_1_rect.left,self.myplane_1_rect.top = \
  38.                                  (bg_size[0] - self.myplane_1_rect.width) // 2 ,\
  39.                                   bg_size[1] - self.myplane_1_rect.height
  40.         self.screen_width,self.screen_height = bg_size[0],bg_size[1]
  41.         self.speed = [0,0]

  42.         #如果按下W,A,S,D,表示飞机分别向上下左右移动
  43.         self.press_can_K_w = True
  44.         self.press_can_K_s = True
  45.         self.press_can_K_a = True
  46.         self.press_can_K_d = True

  47.     def move(self):        
  48.         if self.myplane_1_rect.left  < 0  :  #超过左边界,键盘a按下无效
  49.             self.press_can_K_a = False
  50.             
  51.         elif self.myplane_1_rect.left >  self.screen_width - self.myplane_1_rect.width :
  52.             self.press_can_K_d = False

  53.         elif  self.myplane_1_rect.top  < 0 :
  54.             self.press_can_K_w = False
  55.             
  56.         elif self.myplane_1_rect.top>  self.screen_height - self.myplane_1_rect.height:
  57.             self.press_can_K_d = False
  58.         else:
  59.             self.press_can_K_w = True
  60.             self.press_can_K_s = True
  61.             self.press_can_K_a = True
  62.             self.press_can_K_d = True


  63.    

  64. def main():
  65.     pygame.init()
  66.     myplane_1_image = "images/me1.png"
  67.     bg_size = width,height = 480,700
  68.     screen = pygame.display.set_mode(bg_size)
  69.     pygame.display.set_caption("飞机大战")
  70.     background = pygame.image.load("images/background.png").convert()

  71.     BLACK = (0,0,0)
  72.     GREEN = (0,255,0)
  73.     RED = (255,0,0)

  74.    
  75.     pygame.mixer.music.play(-1)   #无限循环背景
  76.     clock = pygame.time.Clock()
  77.     pygame.key.set_repeat(100,100)
  78.     myplane = Myplane(myplane_1_image,bg_size)   
  79.     running = True
  80.     while running:
  81.         for event in pygame.event.get():
  82.             if event.type == QUIT:
  83.                 pygame.quit()
  84.                 sys.exit()

  85.             elif event.type == KEYDOWN:
  86.                 myplane.move()
  87.                 if event.key == K_w and myplane.press_can_K_w == True :
  88.                     myplane.myplane_1_rect.top -= 10
  89.                        
  90.                 if event.key == K_s and myplane.press_can_K_s == True :
  91.                     myplane.myplane_1_rect.top += 10   

  92.                 if event.key == K_a and myplane.press_can_K_a == True :
  93.                     myplane.myplane_1_rect.left -= 10
  94.                        
  95.                 if event.key == K_d and myplane.press_can_K_d == True :
  96.                     myplane.myplane_1_rect.left += 10
  97.                        
  98.         screen.blit(background,(0,0))
  99.         screen.blit(myplane.myplane_1_image,myplane.myplane_1_rect)

  100.         
  101.         pygame.display.flip()
  102.         clock.tick(60)
  103.         
  104.         
  105. if __name__ =="__main__":
  106.     #在双击打开文件时,如果出现异常,将异常显示出来
  107.     try:
  108.         main()
  109.     except SystemExit:
  110.         pass
  111.     except:
  112.         traceback.print_exc()
  113.         pygame.quit()
  114.         input()









  115.         

  116.         
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-30 13:32:58 | 显示全部楼层
代码思路完全没对上啊,你既然设置了速度,但是又没有基于这个速度对图片位置进行过任何操作,你的move方法只加了限制又没有增加移动的情况图片的left和top都没有改变当然就不会动啊。
另外一点是脱离了边界要让False重新置回True的,只能说你的思路还是太绕了,导致你到处缺东西。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-30 14:08:26 | 显示全部楼层
Hoiste 发表于 2020-4-30 13:32
代码思路完全没对上啊,你既然设置了速度,但是又没有基于这个速度对图片位置进行过任何操作,你的move方法 ...

现在呢,你说的地方我尝试改了一下,可能还少点啥,还是动不了,求指点
  1. import sys
  2. import traceback
  3. from pygame.locals import *
  4. import pygame

  5. pygame.init()
  6. pygame.mixer.init()


  7. #载入游戏音乐
  8. pygame.mixer.music.load("sound/game_music.ogg")
  9. pygame.mixer.music.set_volume(0.2)
  10. bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
  11. bullet_sound.set_volume(0.2)
  12. bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
  13. bomb_sound.set_volume(0.2)
  14. supply_sound = pygame.mixer.Sound("sound/supply.wav")
  15. supply_sound.set_volume(0.2)
  16. get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
  17. get_bomb_sound.set_volume(0.2)
  18. get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
  19. get_bullet_sound.set_volume(0.2)
  20. upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
  21. upgrade_sound.set_volume(0.2)
  22. enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
  23. enemy3_fly_sound.set_volume(0.2)
  24. enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
  25. enemy1_down_sound.set_volume(0.2)
  26. enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
  27. enemy2_down_sound.set_volume(0.2)
  28. enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
  29. enemy3_down_sound.set_volume(0.2)
  30. me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
  31. me_down_sound.set_volume(0.2)


  32. class Myplane(pygame.sprite.Sprite):
  33.     def __init__(self,myplane_1_image,bg_size):
  34.         pygame.sprite.Sprite.__init__(self)  #继承类

  35.         self.myplane_1_image = pygame.image.load(myplane_1_image).convert_alpha()
  36.         self.myplane_1_rect = self.myplane_1_image.get_rect()
  37.         self.myplane_1_rect.left,self.myplane_1_rect.top = \
  38.                                  (bg_size[0] - self.myplane_1_rect.width) // 2 ,\
  39.                                   bg_size[1] - self.myplane_1_rect.height
  40.         self.screen_width,self.screen_height = bg_size[0],bg_size[1]
  41.         self.speed = [0,0]

  42.     def move(self):
  43.         #如果按下W,A,S,D,表示飞机分别向上下左右移动
  44.         press_can_K_w = True
  45.         press_can_K_s = True
  46.         press_can_K_a = True
  47.         press_can_K_d = True
  48.         
  49.         if self.myplane_1_rect.left  < 0  :  #超过左边界,键盘a按下无效
  50.             press_can_K_a = False
  51.             
  52.         elif self.myplane_1_rect.left >  self.screen_width - self.myplane_1_rect.width :
  53.             press_can_K_d = False

  54.         elif  self.myplane_1_rect.top  < 0 :
  55.             press_can_K_w = False
  56.             
  57.         elif self.myplane_1_rect.top>  self.screen_height - self.myplane_1_rect.height:
  58.             press_can_K_d = False

  59.         else:
  60.             press_can_K_w = True
  61.             press_can_K_s = True
  62.             press_can_K_a = True
  63.             press_can_K_d = True


  64.    

  65. def main():
  66.     pygame.init()
  67.     myplane_1_image = "image/me1.png"
  68.     bg_size = width,height = 480,700
  69.     screen = pygame.display.set_mode(bg_size)
  70.     pygame.display.set_caption("飞机大战")
  71.     background = pygame.image.load("image/background.png").convert()

  72.     BLACK = (0,0,0)
  73.     GREEN = (0,255,0)
  74.     RED = (255,0,0)

  75.    
  76.     pygame.mixer.music.play(-1)   #无限循环背景
  77.     clock = pygame.time.Clock()
  78.     pygame.key.set_repeat(100,100)
  79.     myplane = Myplane(myplane_1_image,bg_size)   
  80.     running = True
  81.     while running:
  82.         for event in pygame.event.get():
  83.             if event.type == QUIT:
  84.                 pygame.quit()
  85.                 sys.exit()

  86.         if event.type == KEYDOWN:
  87.                 myplane.move()
  88.                 if event.type == K_w and press_can_K_w == True :
  89.                    myplane.rect.center[1] -= 1
  90.                   
  91.                 if event.type == K_s and press_can_K_s == True :
  92.                    myplane.rect.center[1] -= 1   

  93.                 if event.type == K_a and press_can_K_a == True :
  94.                    myplane.rect.center[0] -= 1
  95.                   
  96.                 if event.type == K_d and press_can_K_d == True :
  97.                    myplane.rect.center[0] += 1
  98.                   
  99.         screen.blit(background,(0,0))
  100.         screen.blit(myplane.myplane_1_image,myplane.myplane_1_rect)

  101.         
  102.         pygame.display.flip()
  103.         clock.tick(60)
  104.         
  105.         
  106. if __name__ =="__main__":
  107.     #在双击打开文件时,如果出现异常,将异常显示出来
  108.     try:
  109.         main()
  110.     except SystemExit:
  111.         pass
  112.     except:
  113.         traceback.print_exc()
  114.         pygame.quit()
  115.         input()









  116.         

  117.         
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-30 14:33:45 | 显示全部楼层
还是把问题弄得太复杂了,边界按键失效比超过边界时把坐标锁在边界要多一些事件,而且主函数事件的缩进有点问题,如果坚持要用这个方法的话得重新设计一下,要不然给后面其他功能增加麻烦的。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-30 14:38:58 | 显示全部楼层
Hoiste 发表于 2020-4-30 14:33
还是把问题弄得太复杂了,边界按键失效比超过边界时把坐标锁在边界要多一些事件,而且主函数事件的缩进有点 ...

而且在类里面设置的press_can_K_a等变量在外面访问要带带上对象,问题累积起来会让这个程序变得更加累赘。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-30 14:53:44 | 显示全部楼层    本楼为最佳答案   
按你的想法改好了,问题主要有几个:
一、你对边界的限制的几个变量应该作为属性放在__init__下面,并且和self绑定,下面对应的方法下的同名变量也改为同样的名字。
二、主函数main中的rect在Myplane类中叫做myplane_1_rect,所以没有rect这个属性报错。
三、rect.center是一个坐标元组,无法被修改,所以你应该改成按键改变left,top这些可修改的量。
四、运行的时候你应该就发现了,飞机的移动非常不流畅,这就是事件中操作移动带来的问题了,所以建议把整个代码改成小甲鱼课程里用的
key_pressed = pygame.key.get_pressed()
if key_pressed[K_w] or key_pressed[K_UP]:
    pass
if key_pressed[K_s] or key_pressed[K_DOWN]:
    pass
if key_pressed[K_a] or key_pressed[K_LEFT]:
    pass
if key_pressed[K_d] or key_pressed[K_RIGHT]:
    pass
这种类型,具体的代码自己思考一下吧。
            
  1. import sys
  2. import traceback
  3. from pygame.locals import *
  4. import pygame

  5. pygame.init()
  6. pygame.mixer.init()


  7. #载入游戏音乐
  8. pygame.mixer.music.load("sound/game_music.ogg")
  9. pygame.mixer.music.set_volume(0.2)
  10. bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
  11. bullet_sound.set_volume(0.2)
  12. bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
  13. bomb_sound.set_volume(0.2)
  14. supply_sound = pygame.mixer.Sound("sound/supply.wav")
  15. supply_sound.set_volume(0.2)
  16. get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
  17. get_bomb_sound.set_volume(0.2)
  18. get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
  19. get_bullet_sound.set_volume(0.2)
  20. upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
  21. upgrade_sound.set_volume(0.2)
  22. enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
  23. enemy3_fly_sound.set_volume(0.2)
  24. enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
  25. enemy1_down_sound.set_volume(0.2)
  26. enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
  27. enemy2_down_sound.set_volume(0.2)
  28. enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
  29. enemy3_down_sound.set_volume(0.2)
  30. me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
  31. me_down_sound.set_volume(0.2)


  32. class Myplane(pygame.sprite.Sprite):
  33.     def __init__(self,myplane_1_image,bg_size):
  34.         pygame.sprite.Sprite.__init__(self)  #继承类

  35.         self.myplane_1_image = pygame.image.load(myplane_1_image).convert_alpha()
  36.         self.myplane_1_rect = self.myplane_1_image.get_rect()
  37.         self.myplane_1_rect.left,self.myplane_1_rect.top = \
  38.                                  (bg_size[0] - self.myplane_1_rect.width) // 2 ,\
  39.                                   bg_size[1] - self.myplane_1_rect.height
  40.         self.screen_width,self.screen_height = bg_size[0],bg_size[1]
  41.         self.speed = [0,0]

  42.         #如果按下W,A,S,D,表示飞机分别向上下左右移动
  43.         self.press_can_K_w = True
  44.         self.press_can_K_s = True
  45.         self.press_can_K_a = True
  46.         self.press_can_K_d = True

  47.     def move(self):        
  48.         if self.myplane_1_rect.left  < 0  :  #超过左边界,键盘a按下无效
  49.             self.press_can_K_a = False
  50.             
  51.         elif self.myplane_1_rect.left >  self.screen_width - self.myplane_1_rect.width :
  52.             self.press_can_K_d = False

  53.         elif  self.myplane_1_rect.top  < 0 :
  54.             self.press_can_K_w = False
  55.             
  56.         elif self.myplane_1_rect.top>  self.screen_height - self.myplane_1_rect.height:
  57.             self.press_can_K_d = False
  58.         else:
  59.             self.press_can_K_w = True
  60.             self.press_can_K_s = True
  61.             self.press_can_K_a = True
  62.             self.press_can_K_d = True


  63.    

  64. def main():
  65.     pygame.init()
  66.     myplane_1_image = "images/me1.png"
  67.     bg_size = width,height = 480,700
  68.     screen = pygame.display.set_mode(bg_size)
  69.     pygame.display.set_caption("飞机大战")
  70.     background = pygame.image.load("images/background.png").convert()

  71.     BLACK = (0,0,0)
  72.     GREEN = (0,255,0)
  73.     RED = (255,0,0)

  74.    
  75.     pygame.mixer.music.play(-1)   #无限循环背景
  76.     clock = pygame.time.Clock()
  77.     pygame.key.set_repeat(100,100)
  78.     myplane = Myplane(myplane_1_image,bg_size)   
  79.     running = True
  80.     while running:
  81.         for event in pygame.event.get():
  82.             if event.type == QUIT:
  83.                 pygame.quit()
  84.                 sys.exit()

  85.             elif event.type == KEYDOWN:
  86.                 myplane.move()
  87.                 if event.key == K_w and myplane.press_can_K_w == True :
  88.                     myplane.myplane_1_rect.top -= 10
  89.                        
  90.                 if event.key == K_s and myplane.press_can_K_s == True :
  91.                     myplane.myplane_1_rect.top += 10   

  92.                 if event.key == K_a and myplane.press_can_K_a == True :
  93.                     myplane.myplane_1_rect.left -= 10
  94.                        
  95.                 if event.key == K_d and myplane.press_can_K_d == True :
  96.                     myplane.myplane_1_rect.left += 10
  97.                        
  98.         screen.blit(background,(0,0))
  99.         screen.blit(myplane.myplane_1_image,myplane.myplane_1_rect)

  100.         
  101.         pygame.display.flip()
  102.         clock.tick(60)
  103.         
  104.         
  105. if __name__ =="__main__":
  106.     #在双击打开文件时,如果出现异常,将异常显示出来
  107.     try:
  108.         main()
  109.     except SystemExit:
  110.         pass
  111.     except:
  112.         traceback.print_exc()
  113.         pygame.quit()
  114.         input()









  115.         

  116.         
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-30 14:56:41 | 显示全部楼层
Hoiste 发表于 2020-4-30 14:38
而且在类里面设置的press_can_K_a等变量在外面访问要带带上对象,问题累积起来会让这个程序变得更加累赘 ...

myplane.press_can_K_w == True  这样?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-30 15:07:09 | 显示全部楼层
Hoiste 发表于 2020-4-30 14:53
按你的想法改好了,问题主要有几个:
一、你对边界的限制的几个变量应该作为属性放在__init__下面,并且和 ...

谢谢,万分感谢,这是自己的思路,确实乱
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-30 15:13:15 | 显示全部楼层
猪猪虾 发表于 2020-4-30 15:07
谢谢,万分感谢,这是自己的思路,确实乱

我发现把这改小一些,就不卡了
pygame.key.set_repeat(10,10)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 17:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表