赞
踩
python编程基础项目( 一)
import sys import pygame from settings import Settings from ship import Ship import game_functions as gf from pygame.sprite import Group 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") """创建一艘飞船,必须在主循环前创建,以免以后每次循环都创建""" ship = Ship(ai_settings,screen) """创建一个用于存储子弹的编组""" bullets = Group() """开始游戏的主循环""" while True: """主循环检查玩家的输入""" gf.check_events(ai_settings,screen,ship,bullets) """更新飞船的位置""" ship.update() """未消失子弹的位置""" gf.update_bullets(bullets) """更新后的位置绘制新屏幕""" gf.update_screen(ai_settings,screen,ship,bullets) run_game()
import sys import pygame from bullet import Bullet """函数check_events()检测相关事件。辅助函数check_keydown_events()和 check_keyup_events()来处理这些事件。函数update_screen(),每次执行主循环时都重绘屏幕""" def check_keydown_events(event,ai_settings,screen,ship,bullets): """响应案件""" if event.key == pygame.K_RIGHT: """按下按键时,将其设为True,实现左右移动""" 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) """创建一个子弹,并将加入到编组bullets中""" """如果子弹的数量大于3,俺空格时什么都不发生""" def fire_bullet(ai_settings, screen, ship,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): """响应松开""" """松开右键时,将其设为False,实现左右移动""" 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(): """for循环用来监听事件""" 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_bullets(bullets): """更新子弹的位置,并删除已消失的子弹""" bullets.update() """删除已消失的子弹""" for bullet in bullets.copy(): if bullet.rect.bottom <= 0: bullets.remove(bullet) print(len(bullets)) def update_screen(ai_settings,screen,ship,bullets): """使用screen.fill()填充屏幕""" screen.fill(ai_settings.bg_color) """在飞船和外星人后面重绘所有子弹,方法sprites()返回一个列表""" for bullet in bullets.sprites(): bullet.draw_bullet() """调用ship.blitme()确保飞船出现在屏幕前""" ship.blitme() """让最近绘制的屏幕可见""" pygame.display.flip()
import pygame class Ship(): def __init__(self,ai_settings,screen): """初始化飞船并设置其初始位置""" self.ai_settings = ai_settings self.screen = screen self.image = pygame.image.load('images/ship.bmp') """加载飞船图像并获取其外接矩形""" self.rect = self.image.get_rect() """返回一个表示飞船的surface,并存储在self.image""" self.screen_rect = screen.get_rect() """将表示屏幕的矩形存储在self.screen_rect中""" self.rect.centerx = self.screen_rect.centerx """飞船中心的x坐标设置为表示矩形属性的centerx""" self.rect.bottom = self.screen_rect.bottom """将每艘新飞船放在底部中央""" self.center = float(self.rect.centerx) """飞船的属性center中存储小数值""" self.moving_right = False self.moving_left = False """移动标志位,左右移动""" def update(self): """根据移动标志调整飞船的位置(更新飞船的center值,而不是rect)""" """右边缘的坐标小于屏幕右边缘""" if self.moving_right and self.rect.right < self.screen_rect.right: self.center += self.ai_settings.ship_speed_factor """左边缘大于0说明未触及屏幕左边缘""" if self.moving_left and self.rect.left > 0: self.center -= self.ai_settings.ship_speed_factor self.rect.centerx = self.center """根据self.center更新rect对象""" def blitme(self): """在指定位置绘制飞船""" self.screen.blit(self.image,self.rect)
class Settings(): """存储外星人所有设置的类""" def __init__(self): """初始化游戏外观设置和速度""" #屏幕设置 self.screen_width = 1000 self.screen_height = 800 self.bg_color = (230,230,230) """飞船的速度设置,每次移动1.5像素""" self.ship_speed_factor = 1.5 """子弹设置""" self.bullet_speed_factor = 1 self.bullet_width = 3 self.bullet_height = 15 self.bullet_color = 60,60,60 """默认保留三颗子弹""" self.bullets_allowed = 3
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。