Python Pygame相对于角色位置的滚动背景非常糟糕
我对编程和pygame都很陌生,这是我第一次尝试制作平台游戏。我遵循了一个关于如何制作滚动背景的教程,并尝试在我自己的代码中实现它。但它几乎不会运行,大多数时候角色会被随机卡住,我不知道如何和为什么 以下是该漏洞的视频: 这就是全部代码Python Pygame相对于角色位置的滚动背景非常糟糕,python,python-3.x,scroll,pygame,background,Python,Python 3.x,Scroll,Pygame,Background,我对编程和pygame都很陌生,这是我第一次尝试制作平台游戏。我遵循了一个关于如何制作滚动背景的教程,并尝试在我自己的代码中实现它。但它几乎不会运行,大多数时候角色会被随机卡住,我不知道如何和为什么 以下是该漏洞的视频: 这就是全部代码 import pygame, os #from pygame import Vector2 #Images BG = pygame.image.load(os.path.join("assets\Background.png")) BG2
import pygame, os
#from pygame import Vector2
#Images
BG = pygame.image.load(os.path.join("assets\Background.png"))
BG2 = pygame.image.load(os.path.join("assets\F38196.jpg"))
idle_mario = pygame.image.load(os.path.join('assets\player_animations\Bigidle\Bigidle_0.png'))
mario_run_0 = pygame.image.load(os.path.join('assets\player_animations\Bigrun\Bigrun_0.png'))
mario_run_1 = pygame.image.load(os.path.join('assets\player_animations\Bigrun\Bigrun_1.png'))
mario_run_2 = pygame.image.load(os.path.join('assets\player_animations\Bigrun\Bigrun_2.png'))
mario_jump = pygame.image.load(os.path.join('assets\player_animations\Bigjump\Bigjump_0.png'))
bgWidth, bgHeight = BG2.get_rect().size
#Classes
class Character:
def __init__(self):
self.x = 200
self.x2 = 960
self.y = 810
self.ground = 810
self.vel = 0
#Idle image
self.mario_idle = pygame.transform.scale(idle_mario, (70,140))
#Running animation:
self.mario_run_0 = pygame.transform.scale(mario_run_0, (70,140))
self.mario_run_1 = pygame.transform.scale(mario_run_1, (70, 140))
self.mario_run_2 = pygame.transform.scale(mario_run_2, (70, 140))
self.mario_run_set = [self.mario_run_0, self.mario_run_0, self.mario_run_1, self.mario_run_1, self.mario_run_2,self.mario_run_2]
self.i = 0
#Jumping image:
self.mario_jump = pygame.transform.scale(mario_jump, (70,140))
def draw_character(self, idle, moving_right, moving_left, jumping):
if jumping and moving_left is False:
screen.blit(self.mario_jump, (self.x2, self.y))
elif jumping and moving_left:
screen.blit(pygame.transform.flip(self.mario_jump, True, False), (self.x2,self.y))
elif idle:
screen.blit(self.mario_idle, (self.x2,self.y))
elif moving_right:
screen.blit(self.mario_run_set[self.i], (self.x2,self.y))
self.i += 1
if self.i == 6:
self.i = 0
elif moving_left:
screen.blit(pygame.transform.flip(self.mario_run_set[self.i], True, False), (self.x2,self.y))
self.i += 1
if self.i == 6:
self.i = 0
class Stage:
def __init__(self):
self.width = bgWidth
self.x = 0
self.start_scroll = W / 2
self.stop_scroll = self.width - self.start_scroll
class Main:
def __init__(self):
self.player = Character()
self.stage = Stage()
self.background = BG
self.background2 = BG2
self.bg_width = self.background2.get_rect().width
self.ground = pygame.Rect(0,990,1920,20)
self.move_right = False
self.move_left = False
self.idle = True
self.is_jumping = False
self.i = 1
def draw(self):
#pygame.draw.rect(screen, BROWN, self.ground)
self.player.draw_character(self.idle, self.move_right, self.move_left, self.is_jumping)
#Config
W = 1920
H = 1020
pygame.init()
screen = pygame.display.set_mode((W,H))
pygame.display.set_caption("Lidl Adventures")
clock = pygame.time.Clock()
#Constants
BLACK = (0,0,0)
WHITE = (255,255,255)
BROWN = (137,94,39)
FPS = 60
#Functions
def main():
true = True
main = Main()
while true:
for event in pygame.event.get():
if event.type == pygame.QUIT:
true = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
main.move_right = True
main.move_left = False
main.idle = False
main.player.vel = 12
if event.key == pygame.K_a:
main.move_left = True
main.move_right = False
main.idle = False
main.player.vel = -12
if event.key == pygame.K_w or event.key == pygame.K_SPACE: #https://stackoverflow.com/questions/20940852/calculate-gravity-jump-in-pygame
main.is_jumping = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d:
main.move_right = False
main.player.vel = 0
if event.key == pygame.K_a:
main.move_left = False
main.player.vel = 0
if main.move_left is False and main.move_right is False:
main.idle = True
if main.is_jumping:
v = 40-4*main.i
main.player.y -= v
main.i += 1
if main.player.y == main.player.ground: #or main.player.y = main.player.object:
main.is_jumping = False
main.i = 1
main.player.x += main.player.vel
print(main.player.x)
if main.player.x > main.stage.width - 70: main.player.x = main.stage.width - 70
if main.player.x < 10: main.player.x = 10
if main.player.x < main.stage.start_scroll: main.player.x2 = main.player.x
elif main.player.x > main.stage.stop_scroll: main.stage.x2 = main.player.x - main.stage.width + W
else:
main.player.x2 = main.stage.start_scroll
main.stage.x += -main.player.vel
rel_x = main.stage.x % main.bg_width
screen.blit(main.background2, (rel_x - main.bg_width, 0))
if rel_x < W:
screen.blit(main.background2, (rel_x, 0))
main.draw()
pygame.display.update()
clock.tick(FPS)
if __name__ == "__main__":
main()
导入pygame,操作系统
#从pygame导入向量2
#图像
BG=pygame.image.load(os.path.join(“assets\Background.png”))
BG2=pygame.image.load(os.path.join(“assets\F38196.jpg”))
idle\u mario=pygame.image.load(os.path.join('assets\player\u animations\Bigidle\Bigidle\u 0.png'))
mario\u run\u 0=pygame.image.load(os.path.join('assets\player\u animations\Bigrun\Bigrun\u 0.png'))
mario\u run\u 1=pygame.image.load(os.path.join('assets\player\u animations\Bigrun\Bigrun\u 1.png'))
mario\u run\u 2=pygame.image.load(os.path.join('assets\player\u animations\Bigrun\Bigrun\u 2.png'))
mario\u jump=pygame.image.load(os.path.join('assets\player\u animations\Bigjump\Bigjump\u 0.png'))
bgWidth,bgHeight=BG2.get_rect().size
#班级
类别字符:
定义初始化(自):
self.x=200
self.x2=960
self.y=810
self.ground=810
self.vel=0
#空闲映像
self.mario_idle=pygame.transform.scale(idle_mario,(70140))
#运行动画:
self.mario_run_0=pygame.transform.scale(mario_run_0,(70140))
self.mario_run_1=pygame.transform.scale(mario_run_1,(70140))
self.mario_run_2=pygame.transform.scale(mario_run_2,(70140))
self.mario_run_set=[self.mario_run_0,self.mario_run_0,self.mario_run_1,self.mario_run_2,self.mario_run_2]
self.i=0
#跳转图像:
self.mario_jump=pygame.transform.scale(mario_jump,(70140))
def draw_字符(自身、空闲、向右移动、向左移动、跳跃):
如果跳跃和向左移动为False:
screen.blit(self.mario_jump,(self.x2,self.y))
elif跳跃并向左移动:
screen.blit(pygame.transform.flip(self.mario_jump,True,False),(self.x2,self.y))
elif空闲:
screen.blit(self.mario_idle,(self.x2,self.y))
elif向右移动:
blit(self.mario\u run\u set[self.i],(self.x2,self.y))
self.i+=1
如果self.i==6:
self.i=0
elif向左移动:
blit(pygame.transform.flip(self.mario\u run\u set[self.i],True,False),(self.x2,self.y))
self.i+=1
如果self.i==6:
self.i=0
班级阶段:
定义初始化(自):
self.width=bgWidth
self.x=0
self.start_scroll=W/2
self.stop\u scroll=self.width-self.start\u scroll
班长:
定义初始化(自):
self.player=Character()
self.stage=stage()
self.background=BG
self.background2=BG2
self.bg_width=self.background2.get_rect().width
self.ground=pygame.Rect(09901920,20)
self.move_right=False
self.move_left=False
self.idle=True
self.is_jumping=False
self.i=1
def牵引(自):
#pygame.draw.rect(屏幕、棕色、自我、地面)
self.player.draw_角色(self.idle,self.move_right,self.move_left,self.is_jumping)
#配置
W=1920
H=1020
pygame.init()
screen=pygame.display.set_模式((W,H))
pygame.display.set_标题(“Lidl冒险”)
clock=pygame.time.clock()
#常数
黑色=(0,0,0)
白色=(255255)
布朗=(137,94,39)
FPS=60
#功能
def main():
真的
main=main()
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
真=假
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K\u d:
main.move_right=True
main.move_left=False
main.idle=False
main.player.vel=12
如果event.key==pygame.K_a:
main.move_left=True
main.move_right=False
main.idle=False
main.player.vel=-12
如果event.key==pygame.K_w或event.key==pygame.K_空间:#https://stackoverflow.com/questions/20940852/calculate-gravity-jump-in-pygame
main.is_jumping=True
elif event.type==pygame.KEYUP:
如果event.key==pygame.K\u d:
main.move_right=False
main.player.vel=0
如果event.key==pygame.K_a:
main.move_left=False
main.player.vel=0
如果main.move_left为False,main.move_right为False:
main.idle=True
如果main.u正在跳跃:
v=40-4*main.i
main.player.y-=v
main.i+=1
如果main.player.y==main.player.ground:#或main.player.y=main.player.object:
main.is_jumping=False
main.i=1
main.player.x+=main.player.vel
打印(main.player.x)
如果main.player.x>main.stage.width-70:main.player.x=main.stage.width-70
如果main.player.x<10:main.player.x=10
如果main.player.xmain.stage.stop\u滚动:main.stage.x2=main.player.x-main.stage.width+W
其他:
main.player.x2=main.stage.start\u滚动
main.stage.x+=-main.player.vel
rel_x=main.stage.x%main.bg_宽度
屏幕.blit(main.background2,(rel_x-main.bg_宽度,0))
如果rel_x if main.player.x > main.stage.width - 70: main.player.x = main.stage.width - 70
if main.player.x < 10: main.player.x = 10
if main.player.x < main.stage.start_scroll: main.player.x2 = main.player.x
elif main.player.x > main.stage.stop_scroll: main.stage.x2 = main.player.x - main.stage.width + W
else:
main.player.x2 = main.stage.start_scroll
main.stage.x += -main.player.vel
rel_x = main.stage.x % main.bg_width
screen.blit(main.background2, (rel_x - main.bg_width, 0))
if rel_x < W:
screen.blit(main.background2, (rel_x, 0))
def main():
true = True
main = Main()
while true:
for event in pygame.event.get():
if event.type == pygame.QUIT:
true = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
main.move_right = True
main.move_left = False
main.idle = False
main.player.vel = 12
if event.key == pygame.K_a:
main.move_left = True
main.move_right = False
main.idle = False
main.player.vel = -12
if event.key == pygame.K_w or event.key == pygame.K_SPACE: # https://stackoverflow.com/questions/20940852/calculate-gravity-jump-in-pygame
main.is_jumping = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d:
main.move_right = False
if main.move_left:
pass
else:
main.player.vel = 0
if event.key == pygame.K_a:
main.move_left = False
if main.move_right:
pass
else:
main.player.vel = 0
if main.move_left is False and main.move_right is False:
main.idle = True
main.player.vel = 0
if main.is_jumping:
v = 40 - 4 * main.i
main.player.y -= v
main.i += 1
print(v)
if main.player.y == main.player.ground: # or main.player.y = main.player.object:
main.is_jumping = False
main.i = 1
if main.move_right or main.move_left:
main.player.x += main.player.vel
print(main.player.x)
if main.player.x > main.stage.width - 70: main.player.x = main.stage.width - 70
if main.player.x < 10: main.player.x = 10
if main.player.x < main.stage.start_scroll: main.player.x2 = main.player.x
elif main.player.x > main.stage.stop_scroll: main.player.x2 = main.player.x - main.stage.width + W
else:
main.player.x2 = main.stage.start_scroll
main.stage.x += -main.player.vel
rel_x = main.stage.x % main.bg_width
screen.blit(main.background, (rel_x - main.bg_width, 0))
if rel_x < W:
screen.blit(main.background, (rel_x, 0))
main.draw()
pygame.display.update()
clock.tick(FPS)
if __name__ == "__main__":
main()