Fight Aliens 飞船出现在游戏窗口底部中间位置和移动

一、飞船显示,最终效果如下:

kim image

在根目录AlienGame 下新建ship模块来管理飞船
代码如下: 
# -*- coding: utf-8 -*-
# 作者: guos
# 日期: 2023/3/29
import pygame


# 保存 飞船类的模块
class Ship:
    """
        表示飞船【玩家】
            image 飞船的样子
            rect 飞船的大小和位置
    """
    def __init__(self, screen):
        # 加载飞船的图片
        self.image = pygame.image.load("./data/ship.bmp")   # 加载飞船的图片
        self.rect = self.image.get_rect()  # 获取 飞船的默认的矩形
        self.screen = screen  # 拿到screen游戏窗口对象
        self.sc_rect = screen.get_rect()  # 获取窗口矩形


        # 设置飞船的位置 窗口底部 居中
        # 这里可以直接和窗口底部保持一致,就可以
        self.rect.bottom = self.sc_rect.bottom
        self.rect.centerx = self.sc_rect.centerx


    def blit_ship(self):
        # 飞船显示,需要飞船本身来做,即定义一个方法,然后调用后就会显示
        self.screen.blit(self.image, self.rect)

main模块即main.py文件代码更新如下:

# -*- coding: utf-8 -*-
# 作者: guos
# 日期: 2023/3/29


# 游戏的主文件,使用该文件来启动游戏
import pygame
import sys
import time


from game_config import GameConfig
from ship import Ship


# 创建配置对象
gc = GameConfig()


# 初始化
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode(gc.size)
# 设置窗口的标题
pygame.display.set_caption(gc.title)


# 创建一个飞船实例对象
ship = Ship(screen)


# 创建主循环
while True:
    # 处理事件
    for event in pygame.event.get():
        # 处理退出的事件
        if event.type == pygame.QUIT:
            sys.exit()
    # 设置窗口背景颜色, rgb值
    screen.fill(gc.bg_color)


    # 飞船显示
    ship.blit_ship()


    # 对窗口进行重绘
    pygame.display.flip()

二、飞船在我们按下左右键可以实现移动

ship模块【ship.py文件】代码更新如下:

# -*- coding: utf-8 -*-
# 作者: guos
# 日期: 2023/3/29
import pygame




# 保存 飞船类的模块
class Ship:
    """
        表示飞船【玩家】
            image 飞船的样子
            rect 飞船的大小和位置
    """
    def __init__(self, screen, gc):
        # 加载飞船的图片
        self.image = pygame.image.load("./data/ship.bmp")   # 加载飞船的图片
        self.rect = self.image.get_rect()  # 获取 飞船的默认的矩形
        self.screen = screen  # 拿到screen游戏窗口对象
        self.sc_rect = screen.get_rect()  # 获取窗口矩形


        # 拿到配置对象,主要用来获取移动方向
        self.gc = gc


        # 设置飞船的位置 窗口底部 居中
        # 这里可以直接和窗口底部保持一致,就可以
        self.rect.bottom = self.sc_rect.bottom
        self.rect.centerx = self.sc_rect.centerx


    def blit_ship(self):
        """
            飞船显示
        :return:
        """
        # 飞船显示,需要飞船本身来做,即定义一个方法,然后调用就会显示
        self.screen.blit(self.image, self.rect)


    def update(self):
        """
            更新飞船的位置
        :return:
        """
        self.rect.x += self.gc.ship_speed * self.gc.ship_dir
        # 控制不要出到游戏窗口
        if self.rect.x < 0:
            self.rect.x = 0
        elif self.rect.right > self.sc_rect.right:
            self.rect.right = self.sc_rect.right

main模块【main.py文件】的代码更新如下:

# -*- coding: utf-8 -*-
# 作者: guos
# 日期: 2023/3/29


# 游戏的主文件,使用该文件来启动游戏
import pygame
import sys
import time
from game_config import GameConfig
from ship import Ship

# 创建配置对象
gc = GameConfig()

# 初始化
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode(gc.size)
# 设置窗口的标题
pygame.display.set_caption(gc.title)


# 创建一个飞船实例对象
ship = Ship(screen, gc)


# 创建主循环
while True:
    # 处理事件
    for event in pygame.event.get():
        # 处理退出的事件
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            # 键盘按下
            if event.key == pygame.K_LEFT:
                # 向左移动,设置飞船的移动方向,而不是控制飞船移动 -1
                # 这里把这个方向 放入到 配置对象里
                gc.ship_dir = -1
            elif event.key == pygame.K_RIGHT:
                # 向右移动 1
                gc.ship_dir = 1
        elif event.type == pygame.KEYUP:
            # 何时将 ship_dir 设置为0
            # 当键盘松开,如果此时为向左移动或者向右移动时
            if gc.ship_dir == 1 and event.key == pygame.K_RIGHT or gc.ship_dir == -1 and event.key == pygame.K_LEFT:
                gc.ship_dir = 0

    # 设置窗口背景颜色, rgb值
    screen.fill(gc.bg_color)

    # 更新飞船的位置 必须在显示之前
    ship.update()
    # 飞船显示
    ship.blit_ship()

    # 对窗口进行重绘
    pygame.display.flip()

 

posted @ 2023-03-29 16:56  以赛亚  阅读(14)  评论(0编辑  收藏  举报