《Python编程从入门到实践》day20

#尝试在python3.11文件夹和pycharm中site-packages文件夹中安装,最终在scripts文件夹中新建py文件成功导入pygame运行程序

#今日知识点学习

python 复制代码
import sys

import pygame


class AlienInvasion:
    """管理游戏资源和行为的类"""

    def __init__(self):
        """初始化游戏并创建游戏资源"""
        pygame.init()

        self.screen = pygame.display.set_mode((1200, 800))
        # 实参元组(1200,800)指定游戏窗口尺寸宽1200像素、高800像素
        pygame.display.set_caption("Alien Invasion")

    def run_game(self):
        """开始游戏的主循环"""
        while True:
            # 监视键盘和鼠标事件
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            # 让最近绘制的屏幕可见
            pygame.display.flip()


if __name__ == '__main__':
    # 创建游戏实例并运行游戏
    ai = AlienInvasion()
    ai.run_game()

运行结果:

12.3.2 设置背景色

python 复制代码
import sys

import pygame


class AlienInvasion:
    """管理游戏资源和行为的类"""

    def __init__(self):
        """初始化游戏并创建游戏资源"""
        pygame.init()

        self.screen = pygame.display.set_mode((1200, 800))
        # 实参元组(1200,800)指定游戏窗口尺寸宽1200像素、高800像素
        pygame.display.set_caption("Alien Invasion")

    def run_game(self):
        """开始游戏的主循环"""
        while True:
            # 监视键盘和鼠标事件
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            # 设置背景色
            bg_color = (230, 230, 230)
           
            # 每次循环是都重绘屏幕
            # screen.fill(bg_color)
            # screen = pygame.display.set_mode([1200, 800])
            self.screen.fill(bg_color)

            # 让最近绘制的屏幕可见
            pygame.display.flip()


if __name__ == '__main__':
    # 创建游戏实例并运行游戏
    ai = AlienInvasion()
    ai.run_game()

运行结果:

12.3.3 创建设置类

python 复制代码
#Settings.py
class Settings():
    """存储《外星人入侵》的所有设置的类"""

    def __init__(self):
        """初始化游戏的设置"""
        # 屏幕设置
        self.screen_width = int(1200)
        self.screen_height = int(800)
        self.bg_color = (230, 230, 230)


# 主程序
import sys

import pygame
from Settings import Settings


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")

    # 开始游戏主循环
    while True:
        # 监视键盘和鼠标事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        # 每次循环是都重绘屏幕
        # screen.fill(bg_color)
        # screen = pygame.display.set_mode([1200, 800])
        screen.fill(ai_settings.bg_color)

        # 让最近绘制的屏幕可见
        pygame.display.flip()


run_game()

12.4 添加飞船图像

python 复制代码
#Ship.py
import pygame


class Ship:
    """管理飞船的类"""

    def __init__(self, ai_game):
        """初始化飞船并设置其初始位置"""
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()

        # 加载飞船图像并获取其外接矩形
        self.image = pygame.image.load('images/ship1.bmp')
        self.rect = self.image.get_rect()

        # 将每艘飞船放在屏幕底部中央
        self.rect.midbottom = self.screen_rect.midbottom

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

#Settings.py
class Settings():
    """存储《外星人入侵》的所有设置的类"""

    def __init__(self):
        """初始化游戏的设置"""
        # 屏幕设置
        self.screen_width = int(1200)
        self.screen_height = int(800)
        self.bg_color = (230, 230, 230)


#主程序
import sys
import pygame
from Settings import Settings
from Ship import Ship


class AlienInvasion:
    """管理游戏资源和行为的类"""
    def __init__(self):
        """初始化游戏并创建游戏资源"""
        pygame.init()

        self.settings = Settings()

        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")
        self.ship = Ship(self)

    def run_game(self):
        """开始游戏的主循环"""
        while True:
            # 监视键盘和鼠标事件
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            # 每次循环是都重绘屏幕
            # screen.fill(bg_color)
            # screen = pygame.display.set_mode([1200, 800])
            self.screen.fill(self.settings.bg_color)
            self.ship.blitme()

            # 让最近绘制的屏幕可见
            pygame.display.flip()


if __name__ == '__main__':
    # 创建游戏实例并运行游戏
    ai = AlienInvasion()
    ai.run_game()

运行结果:

相关推荐
冷琴19964 分钟前
基于java+springboot的酒店预定网站、酒店客房管理系统
java·开发语言·spring boot
缘友一世12 分钟前
macOS .bash_profile配置文件优化记录
开发语言·macos·bash
tekin15 分钟前
macos 中使用macport安装,配置,切换多版本php,使用port 安装php扩展方法总结
开发语言·macos·php·port·mac多版本php安装管理·port-select
巽星石24 分钟前
【Blender Python】7.一些运算、三角函数以及随机
python·blender·三角函数·随机·环形阵列
CSXB9925 分钟前
一、Python(介绍、环境搭建)
开发语言·python·测试工具·集成测试
Mopes__28 分钟前
Python | Leetcode Python题解之第461题汉明距离
python·leetcode·题解
火红的小辣椒34 分钟前
PHP反序列化7(字符串逃逸)
开发语言·web安全·php
三玖诶41 分钟前
第一弹:C++ 的基本知识概述
开发语言·c++
EterNity_TiMe_1 小时前
【机器学习】智驭未来:探索机器学习在食品生产中的革新之路
人工智能·python·机器学习·性能优化·学习方法
wjs20241 小时前
Chrome 浏览器:现代网络浏览的先锋
开发语言