pygame 学习check_event,game_function,alien_invasion等程序

这篇文章介绍了使用Python的pygame库开发的一款简易游戏,包括主程序的运行流程、设置类定义屏幕尺寸和标题,以及飞船类的创建与屏幕绘制。通过链接下载ship.bmp资源,展示了如何初始化游戏并更新和显示屏幕内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#game.py
#主程序
import pygame
from settings import Settings
from game_function import update_screen
from game_function import check_events
from ship import Ship
def run_game():
    pygame.init()
    my_set = Settings()
    screen = pygame.display.set_mode((my_set.width,my_set.height))
    ship = Ship(screen)
    pygame.display.set_caption(my_set.caption)
    while True:
        check_events()
        update_screen(my_set,screen,ship)
        
run_game()
#settings.py
import pygame
import sys
class Settings():
    def __init__(self):
        self.color = (0,0,255)
        self.width = 1000
        self.height = 800
        self.caption = 'alien invasion'
#ship.py
import pygame
class Ship():
    def __init__(self,screen):
        self.screen = screen
        self.screen_rect = self.screen.get_rect()

        self.image = pygame.image.load('ship.bmp')
        self.rect = self.image.get_rect()
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom  = self.screen_rect.bottom

    def blitme(self):
        self.screen.blit(self.image,self.rect)
#game_function.py
import pygame
import sys
from settings import Settings
def check_events():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            
def update_screen(settings,screen,ship):
    """circle the ship in screen"""
    """fill the color """
    """flip the screen"""
    screen.fill(settings.color)
    ship.blitme()
    pygame.display.flip()

本文所用到的ship.bmp和alien.bmp的下载   链接: https://pan.baidu.com/s/1Dn5yaQGZLIsN0BohM8PzoA  密码: 38ie

满足上述4个源代码文件以及保证文件夹下放置ship.bmp情况下,运行代码:

ubuntu运行命令:

python3 game.py

win系统运行命令:

python game.py

运行结果:

 

要让飞船可以移动,你需要在 `Ship` 类中添加移动的方法,并在 `game_functions` 模块中添加检测按键事件的函数。具体步骤如下: 1. 在 `Ship` 类中添加 `move_right()` 和 `move_left()` 方法,用于将飞船向右或向左移动一定的距离。 ```python class Ship(): def __init__(self, screen, ai_setting): ... def move_right(self): self.rect.centerx += self.ai_setting.ship_speed_factor def move_left(self): self.rect.centerx -= self.ai_setting.ship_speed_factor ``` 2. 在 `game_functions` 模块中添加 `check_keydown_events()` 和 `check_keyup_events()` 函数,用于检测按键事件,并调用飞船的移动方法。 ```python def check_keydown_events(event, ship): if event.key == pygame.K_RIGHT: ship.move_right() elif event.key == pygame.K_LEFT: ship.move_left() def check_keyup_events(event, ship): if event.key == pygame.K_RIGHT: # 停止向右移动 elif event.key == pygame.K_LEFT: # 停止向左移动 ``` 3. 修改 `check_event()` 函数,调用检测按键事件的函数。 ```python def check_event(ship): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: check_keydown_events(event, ship) elif event.type == pygame.KEYUP: check_keyup_events(event, ship) ``` 4. 在 `run_game()` 函数中调用 `check_event()` 函数。 ```python def run_game(): pygame.init() ai_setting=Settings() screen=pygame.display.set_mode((ai_setting.screen_width,ai_setting.screen_height)) pygame.display.set_caption("alien invasion") ship=Ship(screen,ai_setting) while True: gf.check_event(ship) ship.update(screen) gf.update_screen(ai_setting,screen,ship) pygame.display.flip() run_game() ``` 这样就可以让飞船可以通过方向键进行左右移动了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发狂的蜗牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值