python编程从入门到实践练习12-5

类型: 编程技术 作者: grimmettg 时间: 2018-04-27 阅读数: 89 删除 审核 反审核 站点推荐
#main.py import pygame import sys from ship import Ship import game_functions as gf from settings import Settings from pygame.sprite import Group def run_game(): """飞机位于屏幕左边""" pygame.init() ai_settings = Settings() screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) ship = Ship(screen) bullets = Group() pygame.display.set_caption("Alien Invasion") while True: gf.check_events(ship, ai_settings, bullets, screen) ship.update(ship) bullets.update() for bullet in bullets.copy(): if bullet.rect.left > ai_settings.screen_width: bullets.remove(bullet) #print(len(bullets)) gf.update_screen(screen, ai_settings, ship, bullets) run_game() #ship.py """为方便起见,将子弹类和飞船类都写在这里""" import pygame from pygame.sprite import Sprite class Ship(): """飞船类""" def __init__(self, screen): self.screen = screen self.image = pygame.image.load('ship.jpg') self.rect = self.image.get_rect() self.screen_rect = self.screen.get_rect() self.moving_up = False self.moving_down = False #将飞船放在指定位置 self.rect.centery = self.screen_rect.centery def update(self, ship): if ship.moving_up: ship.rect.y -= 1 if ship.moving_down: ship.rect.y += 1 def blitme(self): self.screen.blit(self.image, self.rect) class Bullet(Sprite): """表示子弹的类""" def __init__(self, screen, ai_setting, ship): super(Bullet, self).__init__() self.screen = screen #在(0,0)处创建一个表示子弹的矩形,在设置正确的位置 self.rect = pygame.Rect(0,0,ai_setting.bullet_width,ai_setting.bullet_height) self.rect.centery = ship.rect.centery self.rect.right = ship.rect.right self.x = float(self.rect.x) self.color = ai_setting.bullet_color self.speed_factor = ai_setting.bullet_speed_factor def draw_bullet(self): pygame.draw.rect(self.screen, self.color, self.rect) def update(self): self.x += self.speed_factor self.rect.x = self.x #settingspy class Settings(): def __init__(self): self.bg_color = (230, 230, 230) self.screen_width = 1200 self.screen_height = 600 #设置子弹的属性 self.bullet_width = 15 self.bullet_height = 3 self.bullet_color = (60,60,60) self.bullet_speed_factor = 1 #game_functions.py import pygame import sys from ship import Bullet def check_events(ship, ai_settings, bullets, screen): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: ship.moving_up = True elif event.key == pygame.K_DOWN: ship.moving_down = True elif event.key == pygame.K_SPACE: fire_bullet(ship,ai_settings, bullets, screen) if event.type == pygame.KEYUP: if event.key == pygame.K_UP: ship.moving_up = False elif event.key == pygame.K_DOWN: ship.moving_down = False def fire_bullet(ship, ai_settings, bullets, screen): new_bullet = Bullet(screen, ai_settings, ship) bullets.add(new_bullet) def update_screen(screen, ai_settings, ship, bullets): screen.fill(ai_settings.bg_color) for bullet in bullets.sprites(): bullet.draw_bullet() ship.blitme() pygame.display.flip()

这是python编程从入门到实践练习12-5的答案,鉴于水平有限,没有实现的功能如下:

未能阻挡飞船移出屏幕. 没有重构,重构好麻烦,天啊噜!!!

来自Lujishu-lujishu.net的文章,链接:http://article.lujishu.net/viewarticle/128740501160。本文章由作者发布,不代表本站观点,如有侵权,请联系我们撤下该文章。

在此注入作者简介

注册时间: 2015-05-31

  • 文章量
    3
  • 访问量
    172

站点推荐

地摊真的会影响商业地产吗

管理员

关于Spark集群的启动命令大全

thincladt

在Easyui中怎么设置datagrid来自适应屏幕的宽度 你知道吗

nongzuo

大数据平台定义及技术方案和路线

laurustin

如何用Python 将图片和矩阵进行互相转换

langdang

在matlab中图像如何用imwrite()保存

jinpu

计算各种标准差的代码怎么写

outdoersa

在python中修改Dataframe列名,怎么做

jinghuhui

人工智能-人脸识别之人脸姿态

consuwpso

linux 如何再次获取 dhcp 的ip地址

incestsih