Python编程:从⼊门到实践项⽬《外星⼈⼊侵》完整代码
blog:
github:
学习《Python编程:从⼊门到实践》有段时间了,跟着书本把所有代码都敲了⼀遍,感悟很深,现在完成了《外星⼈⼊侵》项⽬,对于库、类、函数、⽅法都有⼀定的理解,现在将该项⽬完整代码分享出来,以供学习。
1.⾸先当然是主运⾏⽂件alien_invasion.py
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
import game_functions as gf
def run_game():
# 初始化pygame、设置和屏幕对象
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.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,stats,screen,sb,ship,aliens,bullets)
gf.update_screen(ai_settings,screen,stats,sb,ship,aliens,
bullets,play_button)
run_game()
class Settings():
"""存储《外星⼈⼊侵》的所有设置的类"""
def __init__(self):
"""初始化游戏的静态设置"""
# 屏幕设置
self.screen_width = 800
self.screen_height = 600
self.bg_color = (230, 230, 230)
# 飞船的设置
self.ship_speed_factor = 0.3
#⼦弹设置
self.bullet_speed_factor = 1
self.ship_limit = 3
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = 60,60,60
self.bullets_allowed = 3
# 外星⼈设置
self.alien_speed_factor = 0.1
self.fleet_drop_speed = 10
# 以什么样的速度加快游戏节奏
self.speedup_scale = 1.01
# 外星⼈点数的提⾼速度
self.score_scale = 1.1
self.initialize_dynamic_settings()
# fleet_direction为1表⽰向右移,为-1表⽰向左移
self.fleet_direction = 1
def initialize_dynamic_settings(self):
"""初始化随游戏进⾏⽽变化的设置"""
怎样区分常量元素和微量元素
self.ship_speed_factor = 0.3
self.bullet_speed_factor = 1
self.alien_speed_factor = 0.1
# fleet_direction为1表⽰向右;为-1表⽰向左
self.fleet_direction = 1索尼xml文件可以恢复视频吗
# 记分
self.alien_points = 50
def increase_speed(self):
"""提⾼速度设置和外星⼈点数"""
self.ship_speed_factor *= self.speedup_scale
self.bullet_speed_factor *= self.speedup_scale
self.alien_speed_factor *= self.speedup_scale
self.alien_points = int(self.alien_points * self.score_scale)
3.按键及事件逻辑game_functions.py
import sys
from time import sleep
import pygame
from bullet import Bullet
from alien import Alien
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_UP:
elif event.key == pygame.K_DOWN:
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:
elif event.key == pygame.K_UP:
elif event.key == pygame.K_DOWN:
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)
pe == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = _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()
winform应用在那些领域可以做什么sb.prep_ships()
# 清空外星⼈列表和⼦弹列表
# 创建⼀新的外星⼈,并让飞船居中
create_fleet(ai_settings, screen, ship, aliens)
<_ship()
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):
"""响应⼦弹和外星⼈的碰撞"""
# 删除发⽣碰撞的⼦弹和外星⼈
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:
# 如果整外星⼈都被消灭,就提⾼⼀个等级
ai_settings.increase_speed()
# 提⾼等级
stats.level += 1
sb.prep_level()
create_fleet(ai_settings, screen, ship, aliens)
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 get_number_aliens_x(ai_settings, alien_width):
"""计算每⾏可容纳多少个外星⼈"""
available_space_x = ai_settings.screen_width - 2 * alien_width
number_aliens_x = int(available_space_x / (2 * alien_width))
return number_aliens_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 / (3 * alien_height))
return number_rows
def create_alien(ai_settings, screen, aliens, alien_number, row_number):  """创建⼀个外星⼈并将其放在当前⾏"""
alien = Alien(ai_settings, screen)
alien_width = width
alien.x = alien_width + 2 * alien_width * alien_number
< = height + 2 * height * row_number
< = alien.x
aliens.add(alien)
def create_fleet(ai_settings, screen, ship, aliens):
"""创建外星⼈"""
安卓手机flash播放器最新# 创建⼀个外星⼈,并计算每⾏可容纳多少个外星⼈
qq for linux是什么
alien = Alien(ai_settings, screen)
number_aliens_x = get_number_aliens_x(ai_settings, width)
number_rows = get_number_rows(ai_settings, height,
# 创建外星⼈
for row_number in range(number_rows):
for alien_number in range(number_aliens_x):
create_alien(ai_settings, screen, aliens, alien_number,
row_number)
def check_fleet_edges(ai_settings, aliens):
"""有外星⼈到达边缘时采取相应的措施"""
for alien in aliens.sprites():
python基础代码大全黑客if alien.check_edges():
change_fleet_direction(ai_settings, aliens)
break
def change_fleet_direction(ai_settings, aliens):
"""将整外星⼈下移,并改变它们的⽅向"""
for alien in aliens.sprites():
< += ai_settings.fleet_drop_speed
ai_settings.fleet_direction *= -1
def ship_hit(ai_settings, stats, screen, sb, ship, aliens, bullets):
"""响应被外星⼈撞到的飞船"""
if stats.ships_left > 0:
# 将ships_left减1
stats.ships_left -= 1
# 更新记分牌
sb.prep_ships()
# 清空外星⼈列表和⼦弹列表

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