Thanks to 这边因为公司停电了 哈哈哈哈哈哈哈哈哈哈,赶紧抓紧时间学习一下,完善一下消灭外星人,目前完成了子弹Bullet类以及可以正常的发射子弹如下,然后我准备发完这篇文章后,继续学习到Mac没电了,就回家充电去 嘿嘿嘿~
(完善子弹及子弹发射等代码,对于其他几个类都有改动,改动的类如下:大家接着上一篇直接覆盖即可:bullet.py、settings.py、alien_invasion.py、game_function.py)
1.bullet.py:
# coding = utf8
import os
os.path.abspath(".")
from pygame.sprite import Sprite
import pygame
class Bullet(Sprite):
"""
Image 代表图片本身,只提供图片的数据和基本操作,
Sprite代表一个由多帧组成的闪花,只负责只帧数、位置上操作。传入Image后,Sprite把Image分成多帧操作。
Sprite仅限只游戏框架范围内使用
Image是基本图片封装类型
Sprite可以认为是一个个小图片,一种可以在屏幕上移动的图形对象,并且可以与其他图形对象交互。精灵图像可以是使用pygame
绘制函数绘制的图像,也可以是原来就有的图像文件
当前类中使用Sprite类,通过使用精灵,可将游戏中相关元素编组,进而同时操作编组中的所有元素。
"""
def __init__(self, screen, settings_game, ship):
"""Create a bullet object and contained theirs attributions
Args:
screen (Screen): Deliver into a screen object
settings_game (settings_game): Deliver into a settings_game object
ship (ship_obj): Deliver into a ship object
"""
# 在飞船位置处创建一个子弹对象
super(Bullet, self).__init__()
self.screen = screen
self.settings_game = settings_game
self.ship = ship
# 在(0, 0)位置绘制一个子弹的矩形,再设置后续位置
self.rect = pygame.Rect(0, 0, settings_game.bullet_width, settings_game.bullet_height)
self.rect.centerx = ship.ship_rect.centerx
self.rect.top = ship.ship_rect.top
# 存储小数子弹纵坐标表示其位置,后续位置移动使用
self.y = float(self.rect.y) # rect.y == rect.top
self.color = settings_game.bullet_color
self.speed_factor = settings_game.bullet_speed_factor
def update(self):
"""
更新子弹位置
"""
self.y -= self.speed_factor
self.rect.y = self.y
def draw_bullet(self):
"""
屏幕上绘制子弹
"""
pygame.draw.rect(self.screen, self.color, self.rect)
# 子弹需要不断更新位置,当绘制完成后需要更新子弹位置
self.update()
2.settings.py:
# coding = utf8
import os
os.path.abspath(".")
class settings_game():
def __init__(self):
# 屏幕属性
self.width = 800
self.height = 600
self.bg_color = (230, 230, 230)
self.caption = "Alien Invasion"
# 飞船属性
# 设置飞船移动速度
self.ship_speed_factor = 10
# 子弹属性
self.bullet_speed_factor = 3
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = (60, 60, 60)
self.bullet_allowed = 5
if __name__ == "__main__":
pass
3.alien_invasion.py:
# coding = utf8
import os
import sys
import pygame
from settings import settings_game
from ship import ship_game
from game_function import game_function_game as gf
from pygame.sprite import Group
os.path.abspath(".")
def run_game():
# 初始化游戏并创建一个屏幕对象
pygame.init()
settings_obj = settings_game()
screen = pygame.display.set_mode((settings_obj.width, settings_obj.height))
bg_color = settings_obj.bg_color
pygame.display.set_caption(settings_obj.caption)
# 创建一艘飞船
ship_obj = ship_game(screen)
# 创建一个用于存储子弹的编组
bullets = Group()
# 开始游戏主循环
while True:
# 监听键盘和鼠标事件
gf.check_events(ship_obj, settings_obj, screen, bullets)
# 更新子弹
gf.update_bullets(bullets)
# 让最近绘制的屏幕可见
gf.update_screen(settings_obj, screen, ship_obj, bullets)
run_game()
4.game_function.py:
# coding = utf8
import os
os.path.abspath(".")
import sys
import pygame
from bullet import Bullet
class game_function_game():
def check_keydown_events(event, ship_obj, settings_obj, screen, bullets):
"""This is a function to control keydown events
Args:
event (Events): All event to accept for judgement from keyboard
ship_obj (ship_game): A ship_game object
settings_obj (settings_game): Deliver into a settings_game object
screen (Screen): Deliver into a screen object
bullets (Group) : Deliver into a bullet group
"""
if event.key == pygame.K_RIGHT:
# 向右移动飞船
ship_obj.moving_right = True
elif event.key == pygame.K_LEFT:
# 向左移动飞船
ship_obj.moving_left = True
elif event.key == pygame.K_UP:
# 向上移动飞船
ship_obj.moving_top = True
elif event.key == pygame.K_DOWN:
# 向下移动飞船
ship_obj.moving_bottom = True
elif event.key == pygame.K_SPACE:
# 创建一颗子弹并添加到编组bullets中, 同时限制在屏幕中出现的子弹是数量
game_function_game.fire_bullet(settings_obj, ship_obj, screen, bullets)
def check_keyup_events(event, ship_obj):
"""This is a function to control keyup events
Args:
event (Events): All event to accept for judgement from keyboard
ship_obj (ship_game): A ship_game object
"""
if event.key == pygame.K_LEFT:
ship_obj.moving_left = False
elif event.key == pygame.K_RIGHT:
ship_obj.moving_right = False
elif event.key == pygame.K_UP:
ship_obj.moving_top = False
elif event.key == pygame.K_DOWN:
ship_obj.moving_bottom = False
def check_events(ship_obj, settings_obj, screen, bullets):
"""Main events distribution
Args:
ship_obj (ship_game): A ship_game object to response keyboard or other action
settings_obj (settings_game): Deliver into a settings_game object
screen (Screen): Deliver into a screen object
bullets (Group) : Deliver into a bullet group
"""
# 响应按键和鼠标事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
game_function_game.check_keydown_events(event, ship_obj, settings_obj, screen, bullets)
elif event.type == pygame.KEYUP:
game_function_game.check_keyup_events(event, ship_obj)
def update_screen(settings_obj, screen, ship_obj, bullets):
"""A function to update screen, refresh the ship or other object's behaviour
Args:
settings_obj ([settings_game]): A settings_game object to gain some attributions
screen (Screen): Current screen's object as the game frame
ship_obj (ship_game): A ship_game object for ship update
bullets (Group) : Deliver into a bullet group
"""
# 更新屏幕图像,并切换到新屏幕
# 每次循环时重绘屏幕
# 更新飞船位置
screen.fill(settings_obj.bg_color)
# 重绘所有子弹
for bullet in bullets.sprites():
bullet.draw_bullet()
ship_obj.blitme()
ship_obj.update(settings_obj)
# 更新整个待显示的Surface对象到屏幕上
pygame.display.flip()
def update_bullets(bullets):
# 删除已消失的子弹
bullets.update()
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
def fire_bullet(settings_obj, ship_obj, screen, bullets):
"""Describe a function to fire bullet
Args:
settings_obj (settings_game): Deliver into a settings_game object
ship_obj (ship_game): A ship_game object for ship update
screen (Screen): Deliver into a screen object
bullets (Group) : Deliver into a bullet group
"""
# 发射子弹函数
if len(bullets) < settings_obj.bullet_allowed:
new_bullet = Bullet(screen, settings_obj, ship_obj)
bullets.add(new_bullet)
好了,那么我们来看看效果图:
虽然有点Low,好吧,的确是Low,下一篇文章我们就来创建外星人了,很快,这个项目就要完成, 大家拭目以待把~
关注 + 收藏 + 点赞哦,谢谢啦~