鱼C论坛

 找回密码
 立即注册
查看: 972|回复: 6

[作品展示] 飞机大战(破烂版)

[复制链接]
发表于 2019-9-13 18:03:43 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 juhugufudu 于 2019-9-13 21:38 编辑
  1. import pygame
  2. from pygame.locals import *
  3. import time
  4. import random

  5. count = 0
  6. y = 0

  7. class HeroPlane(object):
  8.     """docstring for HeroPlane"""
  9.     def __init__(self,screen):
  10.         self.x = 210
  11.         self.y = 700
  12.         self.image = pygame.image.load("./feiji/hero1.png")
  13.         self.screen = screen
  14.         #存子弹
  15.         self.bullet_list = []

  16.     def display(self):
  17.         self.screen.blit(self.image,(self.x,self.y))

  18.         for bullet in self.bullet_list:
  19.             '''bullet 是一个对象'''
  20.             if bullet.judge():
  21.                 self.bullet_list.remove(bullet)
  22.             else:
  23.                 bullet.checkHit()
  24.                 bullet.display()
  25.                 bullet.move()
  26.                
  27.     def move_left(self):
  28.         self.x -=7

  29.     def move_right(self):
  30.         self.x +=7

  31.     def fire(self):
  32.         self.bullet_list.append(Bullet(self.x,self.y,self.screen))

  33. class EnemyPlane(object):
  34.     """docstring for EnemyPlane"""
  35.     def __init__(self,screen):
  36.         self.x = 0
  37.         self.y = 0
  38.         self.image = pygame.image.load("./feiji/enemy0.png")
  39.         self.screen = screen
  40.         self.bullet_list = []
  41.         #判断
  42.         self.direction = "right"

  43.     def display(self):
  44.         self.screen.blit(self.image,(self.x,self.y))

  45.         for bullet in self.bullet_list:
  46.             '''bullet 是一个对象'''
  47.             if bullet.judge():
  48.                 self.bullet_list.remove(bullet)
  49.             else:
  50.                 bullet.display()
  51.                 bullet.move()
  52.         
  53.     def move(self):
  54.         global y
  55.         if self.direction == "right":
  56.             self.x +=5
  57.             y = self.x
  58.         elif self.direction == "left":
  59.             self.x -=5
  60.             y = self.x

  61.         if self.x>480-50:
  62.             self.direction = "left"
  63.         elif self.x <0:
  64.             self.direction = "right"

  65.     def fire(self):
  66.         self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y))

  67. class EnemyBullet(object):
  68.     def __init__(self,screen,x,y):
  69.         self.screen = screen
  70.         self.x = x+25
  71.         self.y = y+40
  72.         self.image = pygame.image.load("./feiji/bullet1.png")

  73.     def display(self):
  74.         self.screen.blit(self.image,(self.x,self.y))

  75.     def move(self):
  76.         self.y += 5

  77.     def judge(self):
  78.         '''判断是否越界'''
  79.         if self.y >852:
  80.             return True
  81.         else:
  82.             return False

  83. class Bullet(object):
  84.     """docstring for Bullet"""
  85.     def __init__(self,x,y,screen_temp):
  86.         self.x = x+40
  87.         self.y = y-20
  88.         self.screen = screen_temp
  89.         self.image = pygame.image.load("./feiji/bullet.png")
  90.    
  91.     def display(self):
  92.         self.screen.blit(self.image,(self.x,self.y))

  93.     def move(self):
  94.         self.y-=20

  95.     def checkHit(self):
  96.         global count
  97.         global y
  98.         #print(self.x)
  99.         if y <= self.x+10 and y >= self.x-10 and self.y ==0:
  100.             count+=1
  101.             print(count)

  102.     def judge(self):
  103.         '''判断是否越界'''
  104.         if self.y <0:
  105.             return True
  106.         else:
  107.             return False

  108. def key_control(hero):
  109.     for event in pygame.event.get():
  110.         if event.type == QUIT:
  111.             exit()
  112.         elif event.type == KEYDOWN:
  113.             if event.key == K_LEFT or event.key == K_a:
  114.                 hero.move_left()
  115.             elif event.key == K_RIGHT or event.key == K_d:
  116.                 hero.move_right()
  117.             elif event.key == K_SPACE:
  118.                 hero.fire()

  119. def main():
  120.     #显示窗口
  121.     screen = pygame.display.set_mode((480,852),0,32)
  122.     #显示背景
  123.     background = pygame.image.load("./feiji/background.png")
  124.     #显示飞机
  125.     hero = HeroPlane(screen)
  126.     enemy = EnemyPlane(screen)
  127.     #while循环一直显示
  128.     while True:
  129.         screen.blit(background,(0,0))
  130.         hero.display()
  131.         #显示敌机
  132.         enemy.display()
  133.         enemy.move()
  134.         num = random.randint(1,30)
  135.         if num == 9:
  136.             enemy.fire()
  137.         #更新
  138.         pygame.display.update()
  139.         #键盘输入
  140.         key_control(hero)
  141.         #休息
  142.         time.sleep(0.01)

  143. if __name__ == '__main__':
  144.     main()

  145. '''
  146. 误差方法

  147. y = 0

  148. x 误差 10px
  149. '''
复制代码
效果简单到包...(大神·勿喷)

feiji.zip

1.49 MB, 下载次数: 27

资源

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-9-13 21:08:19 | 显示全部楼层
附件资源呢,百度云盘上传:https://pan.baidu.com/disk/home
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-13 21:38:59 | 显示全部楼层
一个账号 发表于 2019-9-13 21:08
附件资源呢,百度云盘上传:https://pan.baidu.com/disk/home

资源已经发到了..
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-13 21:41:36 | 显示全部楼层
对不起,没看到
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-13 21:48:39 | 显示全部楼层
还行吧~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-15 16:49:25 | 显示全部楼层
缺点 1:飞机移动慢
缺点 2:我方飞机没有子弹
缺点 3:……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-6 22:43:33 | 显示全部楼层
zltzlt 发表于 2019-9-15 16:49
缺点 1:飞机移动慢
缺点 2:我方飞机没有子弹
缺点 3:……

是呀,真的是破烂版
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 00:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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