python外星人入侵代码提示has no attri_Python外星人入侵完整代码和注释(四)
四、重构:模块game_function,创建game_function.py 文件名
1、创建game_function.py(用来存储项目的大部分函数)
2、函数check_events() :响应按键和鼠标事件
3、函数update_screen() :更新屏幕上的图像并切换到新屏幕
4、函数check_play_button() :在玩家单击Play按钮时开始新游戏
5、函数change_fleet_direction() :将整群外星人下移,并改变它们的方向
6、函数update_aliens() : 检查是否有外星人位于屏幕边缘,并更新外星人群中所有外星人的位置
等等一些函数,代码有详细注释,就不一个个列出来了
每当用户按键时,都在pygam中注册一个KEYDOWN事件,事件都是通过方法pygame.event.get()获取的。
(1)响应按键(如果按下右箭头键则增大飞船的 rect.centerx 值)
(2)允许飞船不断移动(设置标志 self.moving_right,按下为true,松开为false)
(3)左右移动 :类似飞船右移动
(4)调整飞船速度
settings.py中添加飞船的速度属性ship_speed_factor
__init__()中初始化一个ai_settings的属性 在飞船的属性center中存储小数值, 更新飞船的center值,而不是rect。 根据self.center更新rect对象。
(5)限制飞船的活动范围
ship.py: 对移动标志进行if判断时添加self.rect.right和self.rect.left,即要保证外接矩形的右边缘的x坐标小于屏幕右边缘,左边缘的x坐标要保证大于0。
(6)重构check_events()
将check_events()的代码放在两个函数中,一个处理KEYDOWN事件,一个处理KEYUP事件。
等等,代码有详细注释,就不一个个列出来了
代码如下
import sys
import pygame
from time import sleep
from bullet import Bullet
from alien import Alien
def check_keydown_events(event,ai_settings,screen,ship,bullets):
#响应按键
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.key ==pygame.K_SPACE:
fire_bullet(ai_settings,screen,ship,bullets)
#输入q键,退出游戏
elif event.key == pygame.K_q:
sys.exit()
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:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
def check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets):
#响应按键和鼠标事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# 返回一个元祖,包含鼠标的x和y坐标
mouse_x , mouse_y = pygame.mouse.get_pos()
check_play_button(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets,mouse_x,mouse_y)
elif event.type == pygame.KEYDOWN:
check_keydown_events(event,ai_settings,screen,ship,bullets)
#向右移动飞船
#ship.rect.centerx += 1
elif event.type == pygame.KEYUP:
check_keyup_events(event,ship)
def check_play_button(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets,mouse_x,mouse_y):
#在玩家单击Play按钮时开始新游戏
#collidepoint()检查鼠标单击位置是否在Play按钮的rect内
button_clicked = play_button.rect.collidepoint(mouse_x,mouse_y)
if button_clicked and not stats.game_active:
#重置游戏设置
ai_settings.initialize_dynamic_settings()
#隐藏光标
pygame.mouse.set_visible(False)
#重置游戏统计信息
stats.reset_stats()
stats.game_active = True
#重置记分牌图像
sb.prep_score()
sb.prep_high_score()
sb.prep_level()
sb.prep_ships()
#清空外星人列表和子弹列表
aliens.empty()
bullets.empty()
#创建一群新的外星人,并让飞船居中
create_fleet(ai_settings,screen,ship,aliens)
ship.center_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()
#显示分数
sb.show_score()
#在屏幕上绘制编组中的每个外星人
#aliens.blitme()
aliens.draw(screen)
#如果游戏处于非活跃状态,就绘制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 in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
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 = pygame.sprite.groupcollide(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:
#删除现有的子弹,加快游戏节奏,并新建一群外星人,就提高一个等级
bullets.empty()
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_aliens_x = int(available_space_x / (2 * alien_width))
return number_aliens_x
def create_alien(ai_settings,screen,aliens,alien_number,row_number):
# 创建一个外星人并把其放入当前行
alien = Alien(ai_settings, screen)
alien_width = alien.rect.width
alien.x = alien_width + 2 * alien_width * alien_number
alien.rect.x = alien.x
alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
aliens.add(alien)
def create_fleet(ai_settings,screen,ship,aliens):
#创建外星人群
#创建一个外星人,并计算一行可容纳多少个外星人
#外星人的间距为外星人的宽度
alien = Alien(ai_settings,screen)
number_aliens_x = get_number_aliens_x(ai_settings,alien.rect.width)
number_rows = get_number_rows(ai_settings,ship.rect.height,alien.rect.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 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
def check_fleet_edges(ai_settings,aliens):
#有外星人到达边缘时,采取相应措施
for alien in aliens.sprites():
if alien.check_edges():
change_fleet_direction(ai_settings,aliens)
break
def change_fleet_direction(ai_settings,aliens):
#将整群外星人下移,并改变它们的方向
for alien in aliens.sprites():
alien.rect.y += ai_settings.fleet_drop_speed
#将ai_settings.fleet_durection的值修改为其当前值与-1的乘积
ai_settings.fleet_direction *= -1
def ship_hit(ai_settings,stats,screen,sb,ship,aliens,bullets):
#响应被外星人撞到的飞船
if stats.ships_left > 0:
#将ship_left减一
stats.ships_left -= 1
#更新记分牌
sb.prep_ships()
#清空外星人和子弹列表
aliens.empty()
bullets.empty()
#创建一群新的外星人,并将飞船放到屏幕低端中央
create_fleet(ai_settings,screen,ship,aliens)
ship.center_ship()
#暂停
sleep(0.5)
else:
stats.game_active = False
pygame.mouse.set_visible(True)
def check_aliens_bottom(ai_settings,stats,screen,sb,ship,aliens,bullets):
#检查是否有外星人到达了屏幕低端
screen_rect = screen.get_rect()
for alien in aliens.sprites():
if alien.rect.bottom >=screen_rect.bottom:
#像飞船被撞到一样进行处理
ship_hit(ai_settings,stats,screen,sb,ship,aliens,bullets)
break
def update_aliens(ai_settings,stats,screen,sb,ship,aliens,bullets):
#检查是否有外星人位于屏幕边缘,并更新外星人群中所有外星人的位置
check_fleet_edges(ai_settings,aliens)
aliens.update()
#检查是否有外星人到达屏幕低端
check_aliens_bottom(ai_settings,stats,screen,sb,ship,aliens,bullets)
#检测外星人和飞船之间的碰撞
if pygame.sprite.spritecollideany(ship,aliens):
ship_hit(ai_settings,stats,screen,sb,ship,aliens,bullets)
#print("Ship hit!!!")
def check_high_score(stats,sb):
#检查是否诞生了新的最高得分
if stats.score > stats.high_score:
stats.high_score = stats.score
sb.prep_high_score()
点击链接 https://blog.csdn.net/Ljt101222/article/details/81253329 进入 Python外星人入侵完整代码和注释(五)
python外星人入侵代码提示has no attri_Python外星人入侵完整代码和注释(四)相关推荐
- 用Python代码画一个足球(附完整代码)
用Python代码画一个足球(附完整代码) C站举办了世界杯征文活动,本文用Python代码画一个足球. 实现方法介绍 本文的绘图工具使用Python的标准库turtle库,无需安装,导入即可使用. ...
- Python 编程实现图像分辨率的修改(完整代码)
Python 编程实现图像分辨率的修改(完整代码) 在日常生活和工作中,我们经常需要对图片进行处理,其中一个常见的需求就是修改图像的分辨率.Python 语言提供了强大的图像处理库 PIL(Pytho ...
- anaconda 代码提示,Anaconda安装OpenCV没有自动代码补全
(1)错误描述 出现这种错误OpenCV3.0 和 4.0 都是一样的,不是版本导致的问题,不要在版本中挣扎啦! 最省心最安全最简洁的方式就是下载Anaconda+OpenCV再安装,别用单独的Pyt ...
- 基于python nlp PyTorch智能对联生成系统 附完整代码 毕业设计
软件标题:智能对联生成系统 b 系统概述 使用项目:智能对联生成系统 软件用途:通过网页端可以获取到根据已有上联只能生成的下联. 开发历史:本项目未曾有前置版本.但在服务器搭建,Tensorflow ...
- 代码提示_PHPStorm 支持 Laravel Facades 的代码提示
好久没用 Laravel 和 PHPStorm 了,最近开发了小项目,用的时候发现 Laravel 的 Facades 是没有代码补全提示的,原因是 Facades 是基于 静态方法重载 __call ...
- chrome动态ip python_用Python爬虫爬取动态网页,附带完整代码,有错误欢迎指出!...
系统环境: 操作系统:Windows8.1专业版 64bit Python:anaconda.Python2.7 Python modules:requests.random.json Backgro ...
- opengl实现三维动画简单代码_使用Python简单实现马赛克拼图!内附完整代码
今天小编带大家使用python简单实现马赛克拼图,内容比以往会稍长一些,各位看官老爷可以慢慢细读,若有不足之处还望请斧正,闲话不多说,请看文章. 先看原图: 效果图: 思路: 拼图的原理其实很简单,就 ...
- Python批量处理大量excel数据(含完整代码)
Python批量处理excel数据(含完整代码) pandas库可以有效的处理excel.csv和txt文件,并能多格式将数据重新保存为excel.csv和txt文件. 一.导入数据 利用pandas ...
- python 用户信息管理系统【各个函数剖析 + 完整代码 零基础适用篇】
这个用户管理系统小白也能轻松掌握,只用了函数,用户信息只有两个:姓名(账号)和密码,采用字典存放数据,字典的键即为姓名,值为密码,功能分为两大部分,第一部分为用户的登录和注册,第二部分为管理员对信息的 ...
最新文章
- python考试编程题
- 服务器安装使用rstudio-server
- Dockerfile命令
- 了解员工工作的四种方法
- 网络设备主备配置系列3:华为防火墙(路由模式)
- Visual Studio Debug 教程 之 入门
- 1506G. Maximize the Remaining String
- 写一个类Army,代表一支军队,这个类有一个属性Weapon数组w(用来存储该军队所拥有的所有武器), 该类还提供一个构造方法,在构造方法里通过传一个int类型的参数来限定该类所能拥有
- C语言第七讲,函数入门.
- 2、数字,字符串,列表,字典,集合
- 微服务面试题及详细答案
- 基于 Retina-GAN 的视网膜图像血管分割
- Python Learn 2 -- 高级特性、函数式编程
- ClientDisconnectionReason(客户端断开原因)_羊豆豆_新浪博客
- 安装maskrcnn-banchmark时遇到的“AT_CHECK“ is undefined错误
- IIC总线协议及应用
- 我的世界java版是免费,我的世界Java版
- 十进制转化为二进制的几种方法
- 日拱一卒——160个crackme之#5
- ----down----
热门文章
- 简述计算机主板的功能是什么,电脑主板的功能是什么?
- 《数字电子技术基础》5.1 触发器 概述
- Leaflet绘制全球地震数据热力图
- 回收站清空后如何恢复,三秒就学会
- 猜数字游戏python程序_Python猜数游戏,程序随机生成一个0-100的数,猜对后退出【实例源码】...
- FlashBuilder4的小技巧 代码自动完成 和 代码自动提示
- codeforces C. Omkar and Baseball
- 微信小程序-图片上传
- C语言课程设计-保安值班系统支持任意输入保安值班时间
- 统计辅助表格——年月日历表的生成