第一次改写游戏代码,按键左右移动相对比较容易查找学习,更在意的是上下移动的问题。
下面举个例子,放代码
settings. py
class Settings():
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
#火箭的速度
self.rocket_speed_factor = 1.5
rocket. py
import pygame
class Rocket():
def __init__(self,lr_settings,screen):
self.screen = screen
self.lr_settings =lr_settings
#加载游戏图像和初始化规格属性
self.image = pygame.image.load('images/rocket.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
#设置初始位置
self.rect.centerx = self.screen_rect.centerx
self.rect.centery = self.screen_rect.centery
#在火箭的属性center中存储小数值
self.centerx = float(self.rect.centerx)
self.centery = float(self.rect.centery)
#移动标志
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving_down = False
def update(self):
"""根据移动标志调整火箭的位置"""
#更新火箭的center值,而不是rect
#注意self.rect.right和self.screen_rect.right的标点符号
if self.moving_right and self.rect.right < self.screen_rect.right:
self.centerx += self.lr_settings.rocket_speed_factor
if self.moving_left and self.rect.left > 0:
self.centerx -= self.lr_settings.rocket_speed_factor
if self.moving_down and self.rect.bottom< self.screen_rect.bottom:
self.centery += self.lr_settings.rocket_speed_factor
if self.moving_up and self.rect.top > 0:
self.centery -= self.lr_settings.rocket_speed_factor
#根据self.centerx/y更新rect对象
self.rect.centerx = self.centerx
self.rect.centery = self.centery
def blitme(self):
self.screen.blit(self.image,self.rect)
game_functions. py
import sys,pygame
def check_keydown_events(event, rocket):
"""响应按键"""
if event.key == pygame.K_RIGHT:
rocket.moving_right = True
elif event.key == pygame.K_LEFT:
rocket.moving_left = True
elif event.key == pygame.K_UP:
rocket.moving_up = True
elif 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
elif event.key == pygame.K_LEFT:
rocket.moving_left = False
elif event.key == pygame.K_UP:
rocket.moving_up = False
elif 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(lr_settings,screen,rocket):
screen.fill(lr_settings.bg_color)
rocket.blitme()
pygame.display.flip()
主文件launch_rocket. py
import pygame
from settings import Settings
from rocket import Rocket
import game_functions as gf
def run_game():
"""初始化pygame!!!"""
pygame.init()
lr_settings = Settings()
screen = pygame.display.set_mode(
(lr_settings.screen_width,lr_settings .screen_height)) #设置屏幕窗口大小
pygame.display.set_caption('Rocket') #给窗口命名
rocket = Rocket(lr_settings,screen)
while True:
#隶属于方法run_game()中,需要缩进
gf.check_events(rocket)
rocket.update()
gf.update_screen(lr_settings,screen,rocket)
run_game()
代码部分标注,为个人学习笔记,仅供参考
注重讲解点
pygame方向键如下使用:
K_RIGHT
K_LEFT
K_DOWN
K_UP
而rect没有属性up 和down,应该是top和bottom
具体参考rocket.py/class rocket()/def update(self)
其他介绍:应当把所要应用的图像,rocket. bmp存储在我们当前游戏代码的主项目文件夹里,新建的子文件images中。