Pygame 运动加速问题,平台游戏 [英] Problem with Pygame movement acceleration, platformer game
问题描述
当我使用右键向右移动时,我加速到最大速度.当我释放它时,我确实减速到停止,所以这很好.但是,当使用左键向左移动时,释放它后,我继续以固定速度移动,然后在一小会儿后突然停止.知道我的代码有什么问题吗?
原始代码来自http://programarcadegames.com/python_examples/show_file.php?file=platform_jumper.py
导入pygame# 全局常量# 颜色黑色 = (0, 0, 0)白色 = (255, 255, 255)绿色 = (0, 255, 0)红色 = (255, 0, 0)蓝色 = (0, 0, 255)# 屏幕尺寸屏幕宽度 = 800屏幕高度 = 600类玩家(pygame.sprite.Sprite):""" 这个类代表播放器底部的栏控件."""# - 方法def __init__(self):"""构造函数"""# 调用父类的构造函数super().__init__()# 创建块的图像,并用颜色填充它.# 这也可以是从磁盘加载的图像.宽度 = 40高度 = 60self.image = pygame.Surface([宽度,高度])self.image.fill(RED)# 设置对图像矩形的引用.self.rect = self.image.get_rect()# 设置玩家的速度向量self.xVel = 0self.yVel = 0# 我们可以碰到的精灵列表self.level = 无定义更新(自我):"""移动播放器."""# 重力self.calc_grav()# 向左/向右移动# 看看我们有没有碰到什么block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)对于 block_hit_list 中的块:# 如果我们向右移动,# 将我们的右侧设置为我们命中的项目的左侧如果 self.xVel >0:self.rect.right = block.rect.leftelif self.xVel <0:# 否则,如果我们向左移动,则执行相反的操作.self.rect.left = block.rect.right# 向上/向下移动self.rect.y += self.yVel# 检查我们是否击中任何东西block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)对于 block_hit_list 中的块:# 根据对象的顶部/底部重置我们的位置.如果 self.yVel >0:self.rect.bottom = block.rect.topelif self.yVel <0:self.rect.top = block.rect.bottom# 停止我们的垂直运动self.yVel = 0def calc_grav(self):"""计算重力效应."""如果 self.yVel == 0:self.yVel = 1别的:self.yVel += .35# 看看我们是否在地面上.如果 self.rect.y >= SCREEN_HEIGHT - self.rect.height 和 self.yVel >= 0:self.yVel = 0self.rect.y = SCREEN_HEIGHT - self.rect.heightdef跳转(自己):""" 当用户点击跳转"按钮时调用."""# 向下移动一点,看看我们下面有没有平台.# 向下移动 2 个像素,因为如果我们只向下移动效果不佳#1 当平台向下移动时.self.rect.y += 2platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)self.rect.y -= 2# 如果可以跳跃,将我们的速度调高如果 len(platform_hit_list) >0 或 self.rect.bottom >= SCREEN_HEIGHT:self.yVel = -10类平台(pygame.sprite.Sprite):""" 用户可以跳转的平台 """def __init__(self, width, height):""" 平台构造函数.假设使用用户传入构造一个由 5 个数字组成的数组,就像上面定义的那样代码."""super().__init__()self.image = pygame.Surface([宽度,高度])self.image.fill(绿色)self.rect = self.image.get_rect()类级别(对象):""" 这是用于定义级别的通用超类.为每个级别创建一个特定级别的子类信息."""def __init__(self, player):""" 构造函数.将句柄传递给玩家.移动平台时需要与玩家碰撞."""self.platform_list = pygame.sprite.Group()self.enemy_list = pygame.sprite.Group()self.player = 玩家# 背景图片self.background = 无# 更新此级别的所有内容定义更新(自我):"""更新此关卡中的所有内容."""self.platform_list.update()self.enemy_list.update()def draw(self, screen):"""在这一层绘制所有内容."""# 绘制背景屏幕填充(蓝色)# 绘制我们拥有的所有精灵列表self.platform_list.draw(屏幕)self.enemy_list.draw(屏幕)# 为关卡创建平台等级Level_01(等级):""" 级别 1 的定义."""def __init__(self, player):"""创建级别 1."""# 调用父构造函数Level.__init__(self, player)# 包含平台宽度、高度、x 和 y 的数组级别 = [[210, 70, 500, 500],[210, 70, 200, 400],[210, 70, 600, 300],]# 遍历上面的数组并添加平台对于水平平台:块 = 平台(平台 [0],平台 [1])block.rect.x = 平台[2]block.rect.y = 平台[3]block.player = self.playerself.platform_list.add(block)定义主():"""主程序"""pygame.init()# 设置屏幕的高度和宽度大小 = [屏幕宽度,屏幕高度]屏幕 = pygame.display.set_mode(size)pygame.display.set_caption("平台跳线")# 创建播放器玩家 = 玩家()# 创建所有关卡level_list = []level_list.append(Level_01(玩家))# 设置当前级别current_level_no = 0current_level = level_list[current_level_no]active_sprite_list = pygame.sprite.Group()player.level = current_levelplayer.rect.x = 340player.rect.y = SCREEN_HEIGHT - player.rect.heightactive_sprite_list.add(玩家)加速度_x = 0最大速度 = 6# 循环直到用户点击关闭按钮.完成 = 错误# 用于管理屏幕更新的速度时钟 = pygame.time.Clock()# -------- 主程序循环 -----------虽然没有完成:player_running = 假对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:完成 = 真elif event.type == pygame.KEYDOWN:如果 event.key == pygame.K_LEFT:accel_x = -0.5如果 event.key == pygame.K_RIGHT:加速度_x = 0.5如果 event.key == pygame.K_SPACE:player.jump()elif event.type == pygame.KEYUP:如果 event.key 在 (pygame.K_LEFT, pygame.K_RIGHT):加速度_x = 0player.xVel += accel_x # 加速.if abs(player.xVel) >= max_speed: # 如果超过 max_speed.# 标准化 x_change 并将其与 max_speed 相乘.player.xVel = player.xVel/abs(player.xVel) * max_speed# 如果没有按下任何键,则减速.如果 accel_x == 0:player.xVel *= 0.5player.rect.x += player.xVel# 更新播放器.active_sprite_list.update()# 更新关卡中的物品current_level.update()# 如果玩家靠近右侧,则将世界向左移动 (-x)如果 player.rect.right >屏幕宽度:player.rect.right = SCREEN_WIDTH# 如果玩家靠近左侧,将世界向右移动 (+x)如果 player.rect.left <0:player.rect.left = 0# 所有要绘制的代码都应该在此注释下方current_level.draw(屏幕)active_sprite_list.draw(屏幕)# 所有要绘制的代码都应该高于此注释# 限制为每秒 60 帧时钟滴答(60)# 继续使用我们绘制的内容更新屏幕.pygame.display.flip()# 对空闲友好.如果您忘记这一行,程序将挂起"# 退出时.pygame.quit()如果 __name__ == "__main__":主要的()
问题是因为pygame.Rect
操作整数数据:
Rect 对象的坐标都是整数.[...]
当你这样做
<块引用>player.rect.x += player.xVel
和你做的一样:
player.rect.x = int(player.rect.x + player.xVel)
player.xVel
的小数部分丢失了.加法运算的结果被截断,玩家趋向于值较低的坐标(左).
向Player
类添加一个浮点x坐标(self.px
),并用它来计算玩家的位置.使用 round
设置积分矩形self.px
中的位置:
class Player(pygame.sprite.Sprite):def __init__(self):# [...]# 设置对图像矩形的引用.self.rect = self.image.get_rect()self.px = self.rect.x# [...]定义更新(自我):# [...]block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)对于 block_hit_list 中的块:# 如果我们向右移动,# 将我们的右侧设置为我们命中的项目的左侧如果 self.xVel >0:self.rect.right = block.rect.leftelif self.xVel <0:# 否则,如果我们向左移动,则执行相反的操作.self.rect.left = block.rect.rightself.px = self.rect.x
def main():# [...]player.rect.x = 340player.px = player.rect.x# [...]虽然没有完成:# [...]player.px += player.xVelplayer.rect.x = 回合(玩家.px)# [...]
When i move right using the right key, i accelerate to a max speed. When i release it, i do decelerate to a stop so that is fine. However, when moving left using the left key, and after releasing it, i continue moving at a fixed speed and then come to an abrupt stop after a short while. Any idea what could be wrong with my code?
The original code is from http://programarcadegames.com/python_examples/show_file.php?file=platform_jumper.py
import pygame
# Global constants
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class Player(pygame.sprite.Sprite):
""" This class represents the bar at the bottom that the player
controls. """
# -- Methods
def __init__(self):
""" Constructor function """
# Call the parent's constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
width = 40
height = 60
self.image = pygame.Surface([width, height])
self.image.fill(RED)
# Set a referance to the image rect.
self.rect = self.image.get_rect()
# Set speed vector of player
self.xVel = 0
self.yVel = 0
# List of sprites we can bump against
self.level = None
def update(self):
""" Move the player. """
# Gravity
self.calc_grav()
# Move left/right
# See if we hit anything
block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
for block in block_hit_list:
# If we are moving right,
# set our right side to the left side of the item we hit
if self.xVel > 0:
self.rect.right = block.rect.left
elif self.xVel < 0:
# Otherwise if we are moving left, do the opposite.
self.rect.left = block.rect.right
# Move up/down
self.rect.y += self.yVel
# Check and see if we hit anything
block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
for block in block_hit_list:
# Reset our position based on the top/bottom of the object.
if self.yVel > 0:
self.rect.bottom = block.rect.top
elif self.yVel < 0:
self.rect.top = block.rect.bottom
# Stop our vertical movement
self.yVel = 0
def calc_grav(self):
""" Calculate effect of gravity. """
if self.yVel == 0:
self.yVel = 1
else:
self.yVel += .35
# See if we are on the ground.
if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.yVel >= 0:
self.yVel = 0
self.rect.y = SCREEN_HEIGHT - self.rect.height
def jump(self):
""" Called when user hits 'jump' button. """
# move down a bit and see if there is a platform below us.
# Move down 2 pixels because it doesn't work well if we only move down
# 1 when working with a platform moving down.
self.rect.y += 2
platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
self.rect.y -= 2
# If it is ok to jump, set our speed upwards
if len(platform_hit_list) > 0 or self.rect.bottom >= SCREEN_HEIGHT:
self.yVel = -10
class Platform(pygame.sprite.Sprite):
""" Platform the user can jump on """
def __init__(self, width, height):
""" Platform constructor. Assumes constructed with user passing in
an array of 5 numbers like what's defined at the top of this
code. """
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(GREEN)
self.rect = self.image.get_rect()
class Level(object):
""" This is a generic super-class used to define a level.
Create a child class for each level with level-specific
info. """
def __init__(self, player):
""" Constructor. Pass in a handle to player. Needed for when moving platforms
collide with the player. """
self.platform_list = pygame.sprite.Group()
self.enemy_list = pygame.sprite.Group()
self.player = player
# Background image
self.background = None
# Update everythign on this level
def update(self):
""" Update everything in this level."""
self.platform_list.update()
self.enemy_list.update()
def draw(self, screen):
""" Draw everything on this level. """
# Draw the background
screen.fill(BLUE)
# Draw all the sprite lists that we have
self.platform_list.draw(screen)
self.enemy_list.draw(screen)
# Create platforms for the level
class Level_01(Level):
""" Definition for level 1. """
def __init__(self, player):
""" Create level 1. """
# Call the parent constructor
Level.__init__(self, player)
# Array with width, height, x, and y of platform
level = [[210, 70, 500, 500],
[210, 70, 200, 400],
[210, 70, 600, 300],
]
# Go through the array above and add platforms
for platform in level:
block = Platform(platform[0], platform[1])
block.rect.x = platform[2]
block.rect.y = platform[3]
block.player = self.player
self.platform_list.add(block)
def main():
""" Main Program """
pygame.init()
# Set the height and width of the screen
size = [SCREEN_WIDTH, SCREEN_HEIGHT]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Platformer Jumper")
# Create the player
player = Player()
# Create all the levels
level_list = []
level_list.append(Level_01(player))
# Set the current level
current_level_no = 0
current_level = level_list[current_level_no]
active_sprite_list = pygame.sprite.Group()
player.level = current_level
player.rect.x = 340
player.rect.y = SCREEN_HEIGHT - player.rect.height
active_sprite_list.add(player)
accel_x = 0
max_speed = 6
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
player_running = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
accel_x = -0.5
if event.key == pygame.K_RIGHT:
accel_x = 0.5
if event.key == pygame.K_SPACE:
player.jump()
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
accel_x = 0
player.xVel += accel_x # Accelerate.
if abs(player.xVel) >= max_speed: # If max_speed is exceeded.
# Normalize the x_change and multiply it with the max_speed.
player.xVel = player.xVel / abs(player.xVel) * max_speed
# Decelerate if no key is pressed.
if accel_x == 0:
player.xVel *= 0.5
player.rect.x += player.xVel
# Update the player.
active_sprite_list.update()
# Update items in the level
current_level.update()
# If the player gets near the right side, shift the world left (-x)
if player.rect.right > SCREEN_WIDTH:
player.rect.right = SCREEN_WIDTH
# If the player gets near the left side, shift the world right (+x)
if player.rect.left < 0:
player.rect.left = 0
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
current_level.draw(screen)
active_sprite_list.draw(screen)
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Limit to 60 frames per second
clock.tick(60)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()
if __name__ == "__main__":
main()
The isssue is caused, because the pygame.Rect
operates with integral data:
The coordinates for Rect objects are all integers. [...]
When you do
player.rect.x += player.xVel
it is the same as you would do:
player.rect.x = int(player.rect.x + player.xVel)
The fraction part of player.xVel
gets lost. The result of the addition operation is truncated and the player tends to the coordinate with the lower value (left).
Add a floating point x coordinate (self.px
) to the class Player
and use it to calculate the position of the player. Use round
to set the integral rectangle position from self.px
:
class Player(pygame.sprite.Sprite):
def __init__(self):
# [...]
# Set a referance to the image rect.
self.rect = self.image.get_rect()
self.px = self.rect.x
# [...]
def update(self):
# [...]
block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
for block in block_hit_list:
# If we are moving right,
# set our right side to the left side of the item we hit
if self.xVel > 0:
self.rect.right = block.rect.left
elif self.xVel < 0:
# Otherwise if we are moving left, do the opposite.
self.rect.left = block.rect.right
self.px = self.rect.x
def main():
# [...]
player.rect.x = 340
player.px = player.rect.x
# [...]
while not done:
# [...]
player.px += player.xVel
player.rect.x = round(player.px)
# [...]
这篇关于Pygame 运动加速问题,平台游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!