Python案例:通过方向键移动屏幕上的图像

Python案例:通过方向键移动屏幕上的图像

1、安装PyGame
(1)下载PyGame
http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame

Python案例:通过方向键移动屏幕上的图像_第1张图片

Python案例:通过方向键移动屏幕上的图像_第2张图片

(2)安装PyGame
D:\Program Files\Python\Python36> python -m pip install --user pygame-1.9.3-cp36-cp36m-win_amd64.whl

2、GameDemo项目
(1)项目结构图
Python案例:通过方向键移动屏幕上的图像_第3张图片

(2)游戏配置文件:settings.py
class Settings:
    def __init__(self):
        self.screen_width = 1000
        self.screen_height = 600
        self.bg_color = (250, 250, 250)
        self.rocket_speed_factor = 1.5
(3)火箭类文件:rocket.py
Rocket类包含三个方法:__init__()、update()、blitme()
import pygame

class Rocket:
    def __init__(self, settings, screen):
        self.screen = screen
        self.settings = settings

        self.image = pygame.image.load("images/rocket.png")
        self.rect = self.image.get_rect()
        self.screen_rect = self.screen.get_rect()

        self.moving_up = False
        self.moving_down = False
        self.moving_left = False
        self.moving_right = False

        self.rect.centerx = self.screen_rect.centerx
        self.rect.centery = self.screen_rect.centery

        self.centerx = float(self.rect.centerx)
        self.centery = float(self.rect.centery)

    def update(self):
        if self.moving_left and self.rect.left > 0:
            self.centerx -= self.settings.rocket_speed_factor
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.centerx += self.settings.rocket_speed_factor
        if self.moving_up and self.rect.top > 0:
            self.centery -= self.settings.rocket_speed_factor
        if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
            self.centery += self.settings.rocket_speed_factor

        self.rect.centerx = self.centerx
        self.rect.centery = self.centery

    def blitme(self):
        self.screen.blit(self.image, self.rect)
(4)游戏功能文件:functions.py
import sys
import pygame


def check_keydown_events(event, rocket):
    if event.key == pygame.K_RIGHT:
        rocket.moving_right = True
    if event.key == pygame.K_LEFT:
        rocket.moving_left = True
    if event.key == pygame.K_UP:
        rocket.moving_up = True
    if event.key == pygame.K_DOWN:
        rocket.moving_down = True


def check_keyup_events(event, rocket):
    if event.key == pygame.K_RIGHT:
        rocket.moving_right = False
    if event.key == pygame.K_LEFT:
        rocket.moving_left = False
    if event.key == pygame.K_UP:
        rocket.moving_up = False
    if event.key == pygame.K_DOWN:
        rocket.moving_down = False


def check_events(rocket):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, rocket)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, rocket)


def update_screen(settings, screen, rocket):
    screen.fill(settings.bg_color)
    rocket.blitme()
    pygame.display.flip()
(5)游戏主程序main_game.py
import pygame

import functions as func
from rocket import Rocket
from settings import Settings


def run_game():
    pygame.init()
    settings = Settings()
    screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
    pygame.display.set_caption("火箭大战")

    rocket = Rocket(settings, screen)

    while True:
        func.check_events(rocket)
        rocket.update()
        func.update_screen(settings, screen, rocket)

run_game()
运行程序,结果如下:
Python案例:通过方向键移动屏幕上的图像_第4张图片

此时,可以通过方向键,上下左右移动火箭,而且火箭不能越出边界。



你可能感兴趣的:(Python编程)