Pygame Scrolling

元气小坏坏 提交于 2019-12-02 07:42:13

So here we go. Here you are updating the platforms if the robot reached the limit:

for plat in platforms:
    plat.rect.x = plat.rect.x - abs(Robot.vel.x)

But when you draw the platforms you draw from the original PLATFORM_LIST list:

def draw():
    for plat in PLATFORM_LIST:
        p = Platform(*plat)
        pg.draw.rect(gameDisplay, blue, (p))

So what ends up happening is even though you are updating the platforms properly, you are drawing from the original list, so you are not drawing the updated the list. You should draw from the platform list you are updating:

def draw():
    for plat in platforms:
        pg.draw.rect(gameDisplay, blue, plat)

Second, I discovered once you hit the left scroll limit, the movement back to the right direction moved the robot the wrong way. replace this:

if Robot.rect.left < display_width/4:
    Robot.RobotPos.x = Robot.RobotPos.x - abs(Robot.vel.x)

with this (the minus sign switched over to the plus sign):

if Robot.rect.left < display_width/4:
    Robot.RobotPos.x = Robot.RobotPos.x + abs(Robot.vel.x)

Just a couple things I found while playing around with the game.

Update

There is also an issue with rounding. Pygame rectangles take integers. Your calculation of velocity yields a float and you are trying to add that to the x of the rectangle here:

for plat in platforms:
    plat.rect.x = plat.rect.x - abs(Robot.vel.x)

This causes issues with rounding that show up in the display (platforms) moving in peculiar ways. You can make them ints:

for plat in platforms:
    plat.rect.x = plat.rect.x - int(abs(Robot.vel.x))

Otherwise you will have to make the platforms like you do the Robot and deal in vec()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!