python外星⼈颜⾊答案_Python-外星⼈此项⽬在学习Python的过程中,对照着书本所写,已成功运⾏。
所⽤的编辑器为Spyder,安装插件Pygame.  外星⼈及飞船图⽚下载链接
以下为⼩部分代码,完整版已上传资源。
主程序:alien_invasion.py
import sys
import pygame
from pygame.sprite import Group
from settings import Settings
from game_stats import GameStats
from scoreboard import Scoreboard
from button import Button
from ship import Ship
from alien import Alien
import game_functions as gf
def run_game():
# 初始化游戏并创建⼀个屏幕对象
pygame.init()
ai_settings=Settings()
screen=pygame.display.set_mode(
(ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
# 创建play按钮
play_button=Button(ai_settings,screen,"Play")
# 创建⼀个⽤于存储游戏统计信息的实例,并创建记分牌
stats=GameStats(ai_settings)
sb=Scoreboard(ai_settings,screen,stats)
# 创建⼀艘飞船,创建⼀个⽤于存储⼦弹的编组,创建⼀个外星⼈编组
ship=Ship(ai_settings,screen)
bullets=Group()
aliens=Group()
# 创建外星⼈
#开始游戏主循环
while True:
# 从gf中调⽤函数⿏标键盘,
gf.check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets) # 更新飞船位置
if stats.game_active:
ship.update()
# 更新屏幕
gf.update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets)
gf.update_aliens(ai_settings,screen,stats,sb,ship,aliens,bullets)
gf.update_screen(ai_settings,screen,stats,sb,ship,aliens,bullets,play_button) run_game()
储存⼤量游戏运⾏的函数 :game_functions.py
import sys
from time import sleep
import pygame
from bullet import Bullet
from alien import Alien
spyder python下载
def check_keydown_events(event,ai_settings,screen,ship,bullets):
"""响应按键"""
if event.key==pygame.K_RIGHT:
# 向右移动飞船
elif event.key==pygame.K_LEFT:
elif event.key==pygame.K_SPACE:
fire_bullet(ai_settings,screen,ship,bullets)
# 快捷键结束游戏
elif event.key == pygame.K_q:
def fire_bullet(ai_settings,screen,ship,bullets):
"""如果还没有到达限制,就发射⼀颗⼦弹"""
# 创建⼀颗⼦弹并将其加⼊到编组bullets中,屏幕上最多有三颗⼦弹
if len(bullets) < ai_settings.bullets_allowed:
new_bullet=Bullet(ai_settings,screen,ship)
bullets.add(new_bullet)
def check_keyup_events(event,ship):
"""响应松开"""
if event.key==pygame.K_RIGHT:
elif event.key==pygame.K_LEFT:
def check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets): """响应按键和⿏标事件"""
for event in ():
pe==pygame.QUIT:
# 按下⿏标
pe==pygame.KEYDOWN:
check_keydown_events(event,ai_settings,screen,ship,bullets)
# 松开⿏标
pe==pygame.KEYUP:
check_keyup_events(event,ship)
# 点击开始Play
pe==pygame.MOUSEBUTTONDOWN:
mouse_x,mouse__pos()
check_play_button(ai_settings,screen,stats,sb,play_button,ship,
aliens,bullets,mouse_x,mouse_y)
def check_play_button(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets, mouse_x,mouse_y):
"""在玩家单击play时开始新游戏"""
button_clicked=ollidepoint(mouse_x,mouse_y)
if button_clicked and not stats.game_active:
# 重置游戏设置
ai_settings.initialize_dynamic_settings()
# 隐藏光标
# 重置游戏统计信息
stats.game_active=True
# 重置记分牌图像
sb.prep_score()
sb.prep_high_score()
sb.prep_level()
sb.prep_ships()
# 清空外星⼈和⼦弹列表
# 创建⼀新的外星⼈,并让飞船居中
create_fleet(ai_settings,screen,ship,aliens)
<_ship()
def update_screen(ai_settings,screen,stats,sb,ship,aliens,bullets,play_button): """更新屏幕上的图像并更新"""
# 每次循环时都重绘屏幕
screen.fill(ai_settings.bg_color)
# 在飞船和外星⼈后⾯重绘⼦弹
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
aliens.draw(screen)
# 显⽰得分
sb.show_score()
# 如果游戏处于⾮活动状态,就绘制play按钮
if not stats.game_active:
play_button.draw_button()
# 让最近绘画的屏幕可见
pygame.display.flip()
def update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets):
"""更新⼦弹的位置,并删除已消失的⼦弹"""
# 更新⼦弹的位置
bullets.update()
# 删除已消失的⼦弹
for bullet py():
bottom <= 0:
check_bullet_alien_collisions(ai_settings,screen,stats,sb,
ship,aliens,bullets)
def check_bullet_alien_collisions(ai_settings,screen,stats,sb,
ship,aliens,bullets):
"""响应⼦弹和外星⼈的碰撞"""
# 删除发⽣碰撞的⼦弹和外星⼈
# ⽅法groupcollide将⼦弹和外星⼈的rect相⽐较
collisions=upcollide(bullets,aliens,True,True)
if collisions:
for aliens in collisions.values():
stats.score += ai_settings.alien_points * len(aliens)
sb.prep_score()
check_high_score(stats,sb)
if len(aliens) == 0:
# 删除现有的⼦弹加快游戏节奏,并新建⼀外星⼈,整外星⼈被消灭,提⾼⼀个等级pty()
ai_settings.increase_speed()
# 提⾼等级
stats.level += 1
sb.prep_level()
create_fleet(ai_settings,screen,ship,aliens)
def get_number_aliens_x(ai_settings,alien_width):
"""计算每⾏可容纳多少个外星⼈"""
available_space_x=ai_settings.screen_width - 2 * alien_width
number_alien_x=int(available_space_x / (2 * alien_width))
return number_alien_x
def get_number_rows(ai_settings,ship_height,alien_height):
"""计算屏幕可容纳多少⾏外星⼈"""
available_space_y=(ai_settings.screen_height -
(3 * alien_height) - ship_height)
number_rows=int(available_space_y / (2 * alien_height))
return number_rows

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。