鱼C论坛

 找回密码
 立即注册
查看: 1399|回复: 3

[已解决]pygame event问题

[复制链接]
发表于 2020-7-19 15:04:50 | 显示全部楼层 |阅读模式

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

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

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

  6. def main():
  7.     pygame.init()
  8.     pygame.mixer.init()
  9.     running=True
  10.     clock=pygame.time.Clock()

  11.    
  12.     bg_size=width,height=480,700
  13.     screen=pygame.display.set_mode(bg_size)
  14.     pygame.display.set_caption('The battle of the plane')
  15.     bg_image=pygame.image.load('./images/background.png').convert_alpha()

  16.     #载入音乐
  17.     pygame.mixer.music.load('./sound/game_music.ogg')
  18.     bullet_sound=pygame.mixer.Sound('./sound/bullet.wav')
  19.     bullet_sound.set_volume(0.2)
  20.     button_sound=pygame.mixer.Sound('./sound/button.wav')
  21.     button_sound.set_volume(0.2)
  22.     enemy1_down=pygame.mixer.Sound('./sound/enemy1_down.wav')
  23.     enemy1_down.set_volume(0.2)
  24.     enemy2_down=pygame.mixer.Sound('./sound/enemy2_down.wav')
  25.     enemy2_down.set_volume(0.2)
  26.     enemy3_down=pygame.mixer.Sound('./sound/enemy3_down.wav')
  27.     enemy3_down.set_volume(0.2)
  28.     enemy3_flying=pygame.mixer.Sound('./sound/enemy3_flying.wav')
  29.     enemy3_flying.set_volume(0.2)
  30.     get_bomb=pygame.mixer.Sound('./sound/get_bomb.wav')
  31.     get_bomb.set_volume(0.2)
  32.     get_bullet=pygame.mixer.Sound('./sound/get_bullet.wav')
  33.     get_bullet.set_volume(0.2)
  34.     me_down=pygame.mixer.Sound('./sound/me_down.wav')
  35.     me_down.set_volume(0.2)
  36.     supply=pygame.mixer.Sound('./sound/supply.wav')
  37.     supply.set_volume(0.2)
  38.     upgrade=pygame.mixer.Sound('./sound/upgrade.wav')
  39.     upgrade.set_volume(0.2)
  40.     use_bomb=pygame.mixer.Sound('./sound/use_bomb.wav')
  41.     use_bomb.set_volume(0.2)

  42.     pygame.mixer.music.play(-1)
  43.     pygame.mixer.music.set_volume(0.2)
  44.     pygame.key.set_repeat(100,100)
  45.     #创建自己的飞机
  46.     me=myplane.Myplane(bg_size)
  47.     while running:
  48.         for event in pygame.event.get():
  49.             if event.type==pygame.QUIT:
  50.                 pygame.quit()
  51.                 sys.exit()
  52.             elif event.type==pygame.KEYDOWN:
  53.                 if event.key==K_a:
  54.                     me.move((-10,0))
  55.                 if event.key==K_d:
  56.                     me.move((10,0))
  57.                 if event.key==K_w:
  58.                     me.move((0,-10))
  59.                 if event.key==K_s:
  60.                     me.move((0,10))

  61.         screen.blit(bg_image,(0,0))
  62.         screen.blit(me.image1,me.rect)
  63.         pygame.display.flip()
  64.         clock.tick(60)

  65.    
  66.         
  67. if __name__=='__main__':
  68.     try:
  69.         main()
  70.     except SystemExit:
  71.         pass
  72.     except:
  73.         traceback.print_exc()
  74.         pygame.quit()
  75.         input()

复制代码


  1. import pygame
  2. class Myplane:
  3.     def __init__(self,bg_size):
  4.         self.image1=pygame.image.load('./images/me1.png').convert_alpha()
  5.         self.image2=pygame.image.load('./images/me2.png').convert_alpha()
  6.         self.rect=self.image1.get_rect()
  7.         self.bg_size=bg_size
  8.         self.rect.left,self.rect.top=(self.bg_size[0]-self.rect.width)//2,self.bg_size[1]-self.rect.height
  9.         self.destroy1=pygame.image.load('./images/me_destroy_1.png').convert_alpha()
  10.         self.destroy2=pygame.image.load('./images/me_destroy_2.png').convert_alpha()
  11.         self.destroy3=pygame.image.load('./images/me_destroy_3.png').convert_alpha()
  12.         self.destroy4=pygame.image.load('./images/me_destroy_4.png').convert_alpha()
  13.     def move(self,speed):
  14.         self.rect=self.rect.move(speed)
  15.         if self.rect.left<0:
  16.             self.rect.left=0
  17.         if self.rect.right>self.bg_size[0]:
  18.             self.rect.right=self.bg_size[0]
  19.         if self.rect.top<0:
  20.             self.rect.top=0
  21.         if self.rect.bottom>self.bg_size[1]:
  22.             self.rect.bottom=self.bg_size[1]
复制代码


在跟着小甲鱼学打飞机, ,但是飞机只能朝一个方向飞,有没有办法能让飞机斜着飞的办法啊,求解
最佳答案
2020-7-24 08:38:31
这个,我也不知道。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-24 08:38:31 | 显示全部楼层    本楼为最佳答案   
这个,我也不知道。

评分

参与人数 1荣誉 -1 鱼币 -1 收起 理由
wuqramy -1 -1 (#`皿′)

查看全部评分

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

使用道具 举报

发表于 2020-8-4 16:43:49 | 显示全部楼层
可以!
楼上瞎说!
只要移动的时候位置加在另外一个角度就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-4 17:01:07 | 显示全部楼层
wuqramy 发表于 2020-8-4 16:43
可以!
楼上瞎说!
只要移动的时候位置加在另外一个角度就行了

你这个手有点残啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 19:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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