python - 如何让我的角色移动更顺畅/使背景与我的角色同步?
问题描述
我现在正在制作游戏,我希望猫角色移动更顺畅,而不是随机爆发。我还希望背景根据角色所在的位置而不是单独移动。例如,如果角色向右移动超过某个点,则背景将移动以适应这一点。如果没有,背景将保持静止。
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()
解决方案
推荐阅读
- python - 如何根据唯一列组合 Pandas 数据框
- javascript - JavaScript - 如何在显示为正方形/矩阵的元素的线性数组中按键查找元素的 x,y 位置?
- python - 将数据帧的部分行从数字转换为日期时间,但得到奇怪的数字
- android - 从一部手机到另一部手机的远距离Android屏幕共享
- java - 即使导入了类也找不到符号
- php - 无法将 WordPress 备份从实时安装到本地
- javascript - Firefox 没有在 Service Worker 中为推送消息打开窗口
- sql - 如果列不包含另一列的 ID,则创建行
- java - 我怎样才能得到正确的平均分和成绩?
- ios - 辅助功能将焦点设置到导航栏标题项