Python小游戏——外星人入侵(保姆级教程)第一章 06让飞船移动,我们将编写代码,在用


系列文章目录

第一章:武装飞船

06:让飞船移动

一、驾驶飞船

下面来让玩家能够左右移动飞船。我们将编写代码,在用户按左或右箭头键时做出响应。我们将首先专注于向右移动,再使用同样的原理来控制向左移动。通过这样做,你将学会如何控制屏幕图像的移动。

二、让飞船移动

1.代码及注释(笔记)

(笔记在代码的注释中!!!)

A:修改文件:game_functions.py

点击查看代码
#渗透小红帽python的学习之路
#外星人入侵小游戏
#game_functions.py
#存储让游戏运行的函数

import sys

import pygame

def check_events(ship): # 在玩家按右箭头时需要将飞船向右移动,
    # 所以给函数加上了形参ship
    for event in pygame.event.get():  # 监听键盘和鼠标事件
        if event.type == pygame.QUIT:
            sys.exit()
    #修改alien_invasion.py,使其导入game_functions
    #并将事件循环替换为对函数check_events()的调用

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                #移动飞船
                ship.moving_right = True
                #玩家按下右箭头键时标志设为true
            elif event.key == pygame.K_LEFT:
                ship.moving_left = True
                # 玩家按下左箭头键时标志设为true


            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    ship.moving_right = False
                    # 玩家松开右箭头键时标志设为false
                elif event.key == pygame.K_LEFT:
                    ship.moving_left = False
                    # 玩家松开左箭头键时标志设为false

def update_screen(ai_settings,screen,ship):
    # 将更新屏幕的代码移到此处
    screen.fill(ai_settings.bg_color)  # 每次循环都会重绘屏幕
    ship.blitme()  # 每次循环时重新绘制飞船

    pygame.display.flip()  # 让最近绘制的屏幕可见
    # 然后修改alien_invasion.py

B:修改文件:alien_invasion.py

点击查看代码
#渗透小红帽python的学习之路
#外星人入侵小游戏
#创建pygame窗口以及响应用户输入

import sys
#模块sys用来退出游戏
import pygame

from settings import Setting
#导入刚创建的设置类
from ship import Ship
#导入管理飞船行为的类
import game_functions as gf

def run_game():
    pygame.init() #初始化屏屏幕对象
    ai_settings = Setting()
    # 创建Setting类的实例,并存储在ai_settings变量中

    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) #创建显示窗口
    pygame.display.set_caption("Alien Invasion")
    ship = Ship(screen) #绘制一艘飞船

    #开始游戏主循环
    while True:
        gf.check_events(ship)
        ship.update()
        # 飞船移动已经设置完成

        gf.update_screen(ai_settings,screen,ship)

run_game()
# 代码重构完成

C:修改文件:ship.py

点击查看代码
#渗透小红帽python的学习之路
#外星人入侵小游戏
#ship.py
#管理飞船行为的类

import pygame

class Ship():
    def __init__(self,screen):
        #参数screen用来指定将飞船绘制到什么地方
        self.screen = screen
        #加载飞船图片并获取其外接矩形
        self.image = pygame.image.load('F:/PythonProject/pythongame/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

        # 移动标志,玩家按下右箭头键时,将标志设为true,松开时重新设置为false
        self.moving_right = False
        self.moving_left = False

    # 方法update()检查标志状态,标志为true时调整飞船位置
    def update(self):
        if self.moving_right:
            self.rect.centerx += 1 #实现了飞船向右移动
        if self.moving_left:
            self.rect.centerx -= 1 #实现了飞船向左移动

    def blitme(self):
        # 在指定位置绘制飞船
        self.screen.blit(self.image, self.rect)

2.运行效果

A.按下右箭头键后向右移动,再按下左箭头键时停止

B.按下左箭头键后向右移动,再按下右箭头键时停止

有什么不懂的地方在评论区留言哦!希望我的文章能对你有所帮助,如果喜欢我的文章,请点赞收藏并关注!你的认可是对我创作最大的鼓励!

2022-08-22 20:59:23 星期一

评论关闭