首页 > 解决方案 > 如何让我的角色移动更顺畅/使背景与我的角色同步?

问题描述

我现在正在制作游戏,我希望猫角色移动更顺畅,而不是随机爆发。我还希望背景根据角色所在的位置而不是单独移动。例如,如果角色向右移动超过某个点,则背景将移动以适应这一点。如果没有,背景将保持静止。

import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(20, 20)
from pygame import * 
init()
size = width, height = 1000, 600
screen = display.set_mode(size)
button = 0
myFont = font.SysFont("Times New Roman",30)

PRESS_RIGHT = False
PRESS_LEFT = False
PRESS_UP = False
PRESS_DOWN = False

dung = image.load("dung.jpg")
dung = transform.scale(dung, (width, height))
cat = image.load("catchar.png")
cat = transform.scale(cat,(200,200))
cat = transform.flip(cat,(True),False)

#Icon
display.set_icon(cat)





def drawScene(backx):
    screen.blit(dung, Rect(backx,0,width,height))    #draw the picture
    screen.blit(dung, Rect(backx + width, 0, width, height))

def cats(spritex):
    screen.blit(cat, (spritex,315))

running = True
myClock = time.Clock()
backgroundX = 0
catx = 200
# Game Loop
while running:

    # Caption
    display.set_caption("Joey's Adventure " + str(mouse.get_pos()[0]) + ", " + str(mouse.get_pos()[1]))

    for e in event.get():             # checks all events that happen
        if e.type == QUIT:
            running =False
        elif e.type == KEYDOWN:
            if e.key == K_RIGHT:
                PRESS_RIGHT = True
                print ("right")
            if e.key == K_LEFT:
                PRESS_LEFT = True 
                print ("left")
            if e.key == K_UP:
                PRESS_UP = True

        keys_pressed = key.get_pressed()

        if keys_pressed[K_LEFT]:
            catx -= 5

        if keys_pressed[K_RIGHT]:
            catx += 5    


    drawScene(backgroundX)
    cats(catx)
    myClock.tick(60)                     # waits long enough to have 60 fps
    backgroundX -= 1
    if backgroundX < -1*width:
        backgroundX = 0
    display.flip()

quit()

标签: pythonpygame

解决方案


推荐阅读