Pygame学习笔记之一:飞船左右移动射击

参数 set.py

class Settings():
    """存储《外星人入侵》的基本参数"""
    def __init__(self):
        """初始化游戏的设置"""
        #屏幕参数,背景颜色,移动速度
        self.screen_width = 1200
        self.screen_height = 700
        self.bg_color = (230,230,230)
        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

飞船ship.py

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()
        self.screen_rect = screen.get_rect()
        #飞船放在屏幕底部中央
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
        #浮点化
        self.center = float(self.rect.centerx)
        #移动标志
        self.moving_right = False
        self.moving_left = False

    def update(self):
		#左右移动对应的坐标变化,不能出界
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.center += self.ai_settings.ship_speed_factor
        if self.moving_left and self.rect.left > 0:
            self.center -= self.ai_settings.ship_speed_factor
        self.rect.centerx = self.center

    def blitme(self):
        """ 指定位置绘制飞船,两个参数,绘制的图片Surface、画的位置dest"""
        self.screen.blit(self.image,self.rect)

子弹bullet.py

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):

    def __init__(self, ai_settings, screen, ship):
        """创建一个管理子弹的类"""
        super().__init__()
        self.screen = screen

        #在(0,0)位置画矩形子弹,然后设置子弹位置
        self.rect = pygame.Rect(0, 0, ai_settings.bullet_width,
                                ai_settings.bullet_height)
        #子弹位置,X坐标等于飞船X均值,Y坐标等于飞船top即Y坐标
        self.rect.centerx = ship.rect.centerx
        self.rect.top = ship.rect.top

        #小数化Y坐标,便于移动运算
        self.y = float(self.rect.y)
        #子弹颜色、速度
        self.color = ai_settings.bullet_color
        self.speed_factor = ai_settings.bullet_speed_factor

    def update(self):
        """向上移动子弹"""
        # 更新子弹Y坐标.
        self.y -= self.speed_factor
        self.rect.y = self.y

    def draw_bullet(self):
        """将子弹画在屏幕上"""
        pygame.draw.rect(self.screen, self.color, self.rect)

飞船移动键盘控制的实现game_functions.py

import sys
import pygame
from bullet import Bullet
#判断键盘是按下、松开、退出
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 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 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 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)

主函数alien.py

import pygame
from set 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")

    #设置背景颜色
    bg_color = (230,230,230)
    #画飞船
    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()

效果图在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值