加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

[python] 小游戏 - play_plane

发布时间:2020-12-17 00:22:21 所属栏目:Python 来源:网络整理
导读:GitHub: 目前只做了第一部分:一个界面,有个飞机,可以左右移动,放子弹。 暂无计划做第二部分。 alien_invasion.py span style="color: #0000ff;"import span style="color: #000000;" pygame span style="color: #0000ff;"from settings span style="col

  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)

<span style="color: #0000ff;">def<span style="color: #000000;"> fire_bullet(ai_settings,bullets):
<span style="color: #0000ff;">if len(bullets) <<span style="color: #000000;"> ai_settings.bullets_allowed:
new_bullet =<span style="color: #000000;"> Bullet(ai_settings,ship)
bullets.add(new_bullet)

  settings.py

</span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__init__</span><span style="color: #000000;"&gt;(self): </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 屏幕设置</span> self.screen_width = 1200<span style="color: #000000;"&gt; self.screnn_height </span>= 800<span style="color: #000000;"&gt; self.bg_color </span>= (230,230<span style="color: #000000;"&gt;) self.ship_speed_factor </span>= 1.5 <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 子弹设置</span> self.bullet_speed_factor = 1<span style="color: #000000;"&gt; self.bullet_width </span>= 3<span style="color: #000000;"&gt; self.bullet_height </span>= 15<span style="color: #000000;"&gt; self.bullet_color </span>= 60,60,60<span style="color: #000000;"&gt; self.bullets_allowed </span>= 5</pre>

  ship.py

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

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

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 加载飞船图像并获取其外接矩形</span>
    self.image = pygame.image.load(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;images/ship.bmp</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;)
    self.rect </span>=<span style="color: #000000;"&gt; self.image.get_rect()
    self.screen_rect </span>=<span style="color: #000000;"&gt; screen.get_rect()

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 将每艘新飞船放在屏幕底部中央</span>
    self.rect.centerx =<span style="color: #000000;"&gt; self.screen_rect.centerx
    self.rect.bottom </span>=<span style="color: #000000;"&gt; self.screen_rect.bottom

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 在飞船的属性 center 中存储小数值</span>
    self.center =<span style="color: #000000;"&gt; float(self.rect.centerx)

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 移动标识</span>
    self.moving_right =<span style="color: #000000;"&gt; False
    self.moving_left </span>=<span style="color: #000000;"&gt; False

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; blitme(self):
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 屏幕指定位置绘制飞船</span>

<span style="color: #000000;"> self.screen.blit(self.image,self.rect)

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; update(self):
    </span><span style="color: #0000ff;"&gt;if</span> self.moving_right <span style="color: #0000ff;"&gt;and</span> self.rect.right <<span style="color: #000000;"&gt; self.screen_rect.right:
        self.center </span>+=<span style="color: #000000;"&gt; self.ai_settings.ship_speed_factor
    </span><span style="color: #0000ff;"&gt;elif</span> self.moving_left <span style="color: #0000ff;"&gt;and</span> self.rect.left ><span style="color: #000000;"&gt; 0:
        self.center </span>-=<span style="color: #000000;"&gt; self.ai_settings.ship_speed_factor

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 更新 rect 对象</span>
    self.rect.centerx = self.center</pre>

(编辑:李大同)

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

    推荐文章
      热点阅读