From f8c9abec03d69b779b1b07ce25f03c817e073bac Mon Sep 17 00:00:00 2001 From: 206530309 <932780660@qq.com> Date: Mon, 14 Jun 2021 14:57:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | Bin 0 -> 1024 bytes alien_invasion.py | 31 +++++++++++++++++++++ game_functions.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++ l.py | 30 ++++++++++++++++++++ main.py | 1 + 5 files changed, 131 insertions(+) create mode 100644 alien_invasion.py create mode 100644 game_functions.py create mode 100644 l.py create mode 100644 main.py diff --git a/README.md b/README.md index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..06d7405020018ddf3cacee90fd4af10487da3d20 100644 GIT binary patch literal 1024 ScmZQz7zLvtFd70QH3R?z00031 literal 0 HcmV?d00001 diff --git a/alien_invasion.py b/alien_invasion.py new file mode 100644 index 0000000..92d2a00 --- /dev/null +++ b/alien_invasion.py @@ -0,0 +1,31 @@ +import pygame +from pygame.sprite import Group + +from settings import Settings +from ship import Ship +import game_functions as gf + + +def run_game(): + """初始化游戏并创建一个屏幕对象""" + 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(screen, ai_settings) + # 创建一个用于存储子弹的编组 + 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() \ No newline at end of file diff --git a/game_functions.py b/game_functions.py new file mode 100644 index 0000000..de8679d --- /dev/null +++ b/game_functions.py @@ -0,0 +1,69 @@ +import sys + +import pygame + +from bullet import Bullet + + +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) + + +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 fire_bullet(ai_settings, screen, ship, bullets): + """如果还没有达到限制,就发射一颗子弹""" + if len(bullets) < ai_settings.bullets_allowed: + # 创建一个子弹,并将其加入编组bullets + new_bullet = Bullet(ai_settings, screen, ship) + bullets.add(new_bullet) + + +def update_screen(ai_settings, screen, ship, bullets): + """更新屏幕上的图像,并切换到新屏幕""" + # 用背景色填充屏幕 + screen.fill(ai_settings.bg_color) + # 在飞船和外星人后面重新绘制所有的子弹 + for bullet in bullets.sprites(): + bullet.draw_bullet() + + # 绘制飞船图形 + ship.blitme() + + # 让最近绘制的屏幕可见(刷新屏幕) + pygame.display.flip() + + +def update_bullets(bullets): + """更新子弹位置,并删除已经消失的子弹""" + bullets.update() + + # 删除子弹 + for bullet in bullets.copy(): + if bullet.rect.bottom <= 0: + bullets.remove(bullet) \ No newline at end of file diff --git a/l.py b/l.py new file mode 100644 index 0000000..5361e3b --- /dev/null +++ b/l.py @@ -0,0 +1,30 @@ +import pygame #导入pygame模块 +from pygame.locals import * #导入pygame.locals模块 +import time #导入time模块 + +#子弹类 +class Bullet(object): + #构造方法,用于初始化子弹对象的属性 + def __init__(self, screen_temp, x, y): + self.x = x+40 #子弹起始x坐标 + self.y = y-20 #子弹起始y坐标 + self.screen = screen_temp #窗口 + self.image = pygame.image.load("C:/work/youxi/zidan.png") #创建一个子弹图片 + #显示子弹图片的方法 + def display(self): + self.screen.blit(self.image, (self.x, self.y)) #显示子弹图片 + #子弹移动方法 + def move(self): + self.y-=10 #子弹y坐标自减10 + #判断子弹是否越界的方法 + def judge(self): + if self.y<0: #如果子弹的y坐标小于0 + return True #返回true正确 + else: + return False #返回false错误 + + + + + + diff --git a/main.py b/main.py new file mode 100644 index 0000000..ba09ab1 --- /dev/null +++ b/main.py @@ -0,0 +1 @@ +print("hellow me web") \ No newline at end of file