编写一个创建空屏幕的Pygame文件。在事件循环中,每当检测到pygame.KEYDOWN事件时
都打印属性event.key。运行这个程序并按下不同的键,看看控制台窗口的输出,以便了解
Pygame会如何响应。
Rocket_Game.py
import pygame
import sys
from settings import Settings
from rocket import Rocketclass Rocket_Game:def __init__(self):pygame.init()self.clock = pygame.time.Clock()self.settings = Settings()self.window = pygame.display.set_mode((self.settings.window_width, self.settings.window_height))pygame.display.set_caption('火箭移动')self.bg_color = (245, 245, 245)self.rocket = Rocket(self)def run_game(self):while True:self.clock.tick(60)for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.KEYDOWN:print('按键被按下', event.key)self.window.fill(self.bg_color)self.rocket.blitme()pygame.display.flip()if __name__ == '__main__':# 创建游戏实例并运行游戏RG = Rocket_Game()RG.run_game()
rocket.py
import pygameclass Rocket:def __init__(self, rocket_game):self.window = rocket_game.windowself.window_rect = self.window.get_rect()self.image = pygame.image.load('images/Rocket.bmp')self.rect = self.image.get_rect()self.rect.center = self.window_rect.centerdef blitme(self):self.window.blit(self.image, self.rect)
settings.py
import pygameclass Settings():def __init__(self):self.window_width = 600self.window_height = 800pygame.display.set_caption('坦克移动')self.bg_color = (245, 245, 245)