import pygame
import sys
from ship import Ship
from settings import Settings
from bullet import Bullet
from alien import Alien
from time import sleep
def ship_hit(ai_Settings,stats,screen,ship,aliens,bullets):
if stats.ships_left >= 0:
stats.ships_left-=1
aliens.empty()
bullets.empty()
create_fillet(ai_Settings,screen,ship,aliens)
ship.center_ship()
sleep(1)
else:
stats.game_active=False
def check_aliens_bottom(ai_Settings,stats,screen,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,ship,aliens,bullets)
break
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)
elif event.key==pygame.K_Q:
sys.exit()
def fire_bullet(ai_Settings,screen,ship,bullets):
if len(bullets) < ai_Settings.bullet_max_numble:
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,ship,bullets):
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type==pygame.KEYDOWN:
check_KEYDOWN_events(event,ai_Settings,screen,ship,bullets)
elif event.type == pygame.KEYUP:
check_KEYUP_events(event,ship)
def update_screen(ai_Settings,screen,ship,aliens,bullets):
screen.fill(ai_Settings.bg_color)
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
aliens.draw(screen)
pygame.display.flip()
def update_bullets(ai_Settings,screen,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,ship,aliens,bullets)
def check_bullet_alien_collisions(ai_Settings,screen,ship,aliens,bullets):
collisions=pygame.sprite.groupcollide(aliens,bullets,True,False)
if len(aliens)==0:
bullets.empty()
create_fillet(ai_Settings,screen,ship,aliens)
def get_numble_aliens_x(ai_Settings,alien_width):
alien_space_x=ai_Settings.screen_width-2*alien_width
alien_numble_x=int(alien_space_x/(2*alien_width))
return alien_numble_x
def get_number_rows(ai_Settings,ship_height,alien_height):
alien_space_y=ai_Settings.screen_height-(alien_height+ship_height+2*alien_height)
numbler_rows=int(alien_space_y/(2*alien_height))
return numbler_rows
def create_alien(ai_Settings,screen,aliens,alien_number,row_numbler):
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_numbler
aliens.add(alien)
def create_fillet(ai_Settings,screen,ship,aliens):
alien=Alien(ai_Settings,screen)
number_aliens_x=get_numble_aliens_x(ai_Settings,alien.rect.width)
bumbler_rows=get_number_rows(ai_Settings,ship.rect.height,alien.rect.height)
for row_numbler in range(bumbler_rows):
for alien_number in range(number_aliens_x):
create_alien(ai_Settings,screen,aliens,alien_number,row_numbler)
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_direction*=-1
def update_aliens(ai_Settings,stats,screen,ship,aliens,bullets):
check_fleet_edges(ai_Settings,aliens)
aliens.update()
if pygame.sprite.spritecollideany(ship,aliens):
ship_hit(ai_Settings,stats,screen,ship,aliens,bullets)
check_aliens_bottom(ai_Settings,stats,screen,ship,aliens,bullets)
AI技术控
- 粉丝: 654
- 资源: 583
最新资源
- 机器人研究数模实验方案LQR控制,m函数调用simulink中的控制算法,在simscape Multibody中的物理模型中测试 跑通模型花了很多时间,这个能帮你节省下时间 方法适合所有机器人研
- 2023华数杯C题总结PDF
- 国内可用AI工具合集(更新到20241130)
- P2轿车并联模型,完全基于正向开发,先计算整车阻力,然后根据当前车辆模式进行扭矩分配
- 混合储能系统能量管理simulink仿真模型 蓄电池和超级电容构成的混合储能系统能量管理控制策略 采用lpf分配系统功率,放电过程中超级电容soc限值管理策略,soc高时多放电,小时少放电
- 三相整流仿真,电压外环电流内环双闭环pi控制,svpwm,pll锁相环,整流电压稳定在650v 功率等级可修改 当前为10kw 如需其他功率等级,与对应修改系统及控制参数 单相整流仿真,双闭环控制 有
- 无监督分类算法代码.zip
- 基于西门子S7-200 PLC和组态王组态3泵恒压供水系统控制
- xilinx FPGA srio 接口verilog源码程序,顶层接口封装为fifo,使用简单方便,已运用在实际项目上 本源码支持srio NWRITE、NWRITE-R、SWRITE、MAINTE
- 导轨搬运设备程序,包含三菱PLC以及信捷HMI源程序,程序包含注释,可附带电气接线图 PLC型号FX3U,人机型号:TH765 可用于各种搬运码垛设备开发参照套用,也可以作为工控爱者学习参考 不包含硬
- Python基础题库100题及答案PDF
- 通过Matlab Simulink仿真平台,以IEEE10节点配电系统为背景,通过仿真分析风机和光伏接入位置和容量对电压分布和波动的影响 包含word说明文档
- vofa+-1.3.12-x64-installer
- 1200W开关电源原理图及PCB文件
- TMC5160 步进驱动器例程
- HTML5实现好看的儿童特长培训班网页源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈