加入收藏 | 设为首页 | 会员中心 | 我要投稿 大庆站长网 (https://www.0459zz.com/)- 科技、智能边缘云、事件网格、云计算、站长网!
当前位置: 首页 > 编程开发 > Python > 正文

[python] 小游戏 - play_plane

发布时间:2020-07-16 00:20:18 所属栏目:Python 来源:互联网
导读:GitHub:https://github.com/liqingwen2015/play_plane目前只做了第一部分:一个界面,有个飞机,可以左右移动,放子弹。暂无计划做第二部分。alien_invasion.pyimport sysimport pygamefrom settings import Settingsfrom ship import Shipimport game_funct

  GitHub:

  目前只做了第一部分:一个界面,有个飞机,可以左右移动,放子弹。

  暂无计划做第二部分。

  alien_invasion.py

<span style="color: #0000ff;">import<span style="color: #000000;"> pygame

<span style="color: #0000ff;">from settings <span style="color: #0000ff;">import<span style="color: #000000;"> Settings
<span style="color: #0000ff;">from ship <span style="color: #0000ff;">import<span style="color: #000000;"> Ship
<span style="color: #0000ff;">import<span style="color: #000000;"> game_functions as gf
<span style="color: #0000ff;">from pygame.sprite <span style="color: #0000ff;">import<span style="color: #000000;"> Group

<span style="color: #0000ff;">def<span style="color: #000000;"> run_game():
<span style="color: #008000;">#<span style="color: #008000;"> 初始化游戏并创建一个屏幕对象
<span style="color: #000000;"> pygame.init()
ai_settings =<span style="color: #000000;"> Settings()
screen =<span style="color: #000000;"> pygame.display.set_mode((ai_settings.screen_width,ai_settings.screnn_height))
pygame.display.set_caption(<span style="color: #800000;">'<span style="color: #800000;">Alien Invasion<span style="color: #800000;">'<span style="color: #000000;">)

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 创建一艘飞船</span>
ship =<span style="color: #000000;"&gt; Ship(ai_settings,screen)
</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 创建一个存储的子弹组</span>
bullets =<span style="color: #000000;"&gt; Group()

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 设置背景色</span>
bg_color = (230,230,230<span style="color: #000000;"&gt;)

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 开始游戏的主循环</span>
<span style="color: #0000ff;"&gt;while</span><span style="color: #000000;"&gt; True:
    gf.check_events(ai_settings,screen,ship,bullets)
    ship.update()
    gf.update_bullets(bullets)
    gf.update_screen(ai_settings,bullets)

run_game()

  bullet.py

<span style="color: #0000ff;">from pygame.sprite <span style="color: #0000ff;">import<span style="color: #000000;"> Sprite

<span style="color: #0000ff;">class<span style="color: #000000;"> Bullet(Sprite):

</span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__init__</span><span style="color: #000000;"&gt;(self,ai_settings,ship):
    super(Bullet,self).</span><span style="color: #800080;"&gt;__init__</span><span style="color: #000000;"&gt;()
    self.screen </span>=<span style="color: #000000;"&gt; screen

    self.rect </span>=<span style="color: #000000;"&gt; pygame.Rect(0,ai_settings.bullet_width,ai_settings.bullet_height)
    self.rect.centerx </span>=<span style="color: #000000;"&gt; ship.rect.centerx
    self.rect.top </span>=<span style="color: #000000;"&gt; ship.rect.top

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 存储用小数表示的子弹位置</span>
    self.y =<span style="color: #000000;"&gt; float(self.rect.y)

    self.color </span>=<span style="color: #000000;"&gt; ai_settings.bullet_color
    self.speed_factor </span>=<span style="color: #000000;"&gt; ai_settings.bullet_speed_factor

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; update(self):
    self.y </span>-=<span style="color: #000000;"&gt; self.speed_factor
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 更新子弹位置</span>
    self.rect.y =<span style="color: #000000;"&gt; self.y

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; draw_bullet(self):
    pygame.draw.rect(self.screen,self.color,self.rect)</span></pre>

  game_functions

<span style="color: #0000ff;">import<span style="color: #000000;"> pygame

<span style="color: #0000ff;">from bullet <span style="color: #0000ff;">import<span style="color: #000000;"> Bullet

<span style="color: #0000ff;">def<span style="color: #000000;"> check_events(ai_settings,bullets):
<span style="color: #008000;">#<span style="color: #008000;"> 监听键盘和鼠标的事件
<span style="color: #0000ff;">for event <span style="color: #0000ff;">in<span style="color: #000000;"> pygame.event.get():
<span style="color: #0000ff;">if event.type ==<span style="color: #000000;"> pygame.QUIT:
sys.exit()
<span style="color: #0000ff;">elif event.type ==<span style="color: #000000;"> pygame.KEYDOWN:
check_keydown_events(event,bullets)
<span style="color: #0000ff;">elif event.type ==<span style="color: #000000;"> pygame.KEYUP:
check_keyup_events(event,ship)

<span style="color: #0000ff;">def<span style="color: #000000;"> update_screen(ai_settings,bullets):
<span style="color: #008000;">#<span style="color: #008000;"> 每次循环时都重绘屏幕
<span style="color: #000000;"> screen.fill(ai_settings.bg_color)

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 在飞船和外星人后面重绘所有子弹</span>
<span style="color: #0000ff;"&gt;for</span> bullet <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; bullets.sprites():
    bullet.draw_bullet()

ship.blitme()

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 让最近绘制的屏幕可见</span>

<span style="color: #000000;"> pygame.display.flip()

<span style="color: #0000ff;">def<span style="color: #000000;"> check_keydown_events(event,bullets):
<span style="color: #0000ff;">if event.key ==<span style="color: #000000;"> pygame.K_RIGHT:
ship.moving_right =<span style="color: #000000;"> True
<span style="color: #0000ff;">elif event.key ==<span style="color: #000000;"> pygame.K_LEFT:
ship.moving_left =<span style="color: #000000;"> True
<span style="color: #0000ff;">elif event.key ==<span style="color: #000000;"> pygame.K_SPACE:
<span style="color: #008000;">#<span style="color: #008000;"> 创建一颗子弹,并将其加入到编组 bullets 中
<span style="color: #000000;"> fire_bullet(ai_settings,bullets)

<span style="color: #0000ff;">def<span style="color: #000000;"> check_keyup_events(event,ship):
<span style="color: #0000ff;">if event.key ==<span style="color: #000000;"> pygame.K_RIGHT:
ship.moving_right =<span style="color: #000000;"> False
<span style="color: #0000ff;">elif event.key ==<span style="color: #000000;"> pygame.K_LEFT:
ship.moving_left =<span style="color: #000000;"> False

<span style="color: #0000ff;">def<span style="color: #000000;"> update_bullets(bullets):
bullets.update()

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 删除已经消失的子弹</span>
<span style="color: #0000ff;"&gt;for</span> bullet <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; bullets.copy():
    </span><span style="color: #0000ff;"&gt;if</span> bullet.rect.bottom <=<span style="color: #000000;"&gt; 0:
        bullets.remove(bullet)

(编辑:大庆站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读