问题描述
我想实现水平和垂直运动的帧率独立性,但每次我这样做时,精灵都会断断续续。
视频下载中的代码:https://dafluffypotato.com/static/platformer_project_9.zip
使用 https://www.youtube.com/watch?v=abH2MSBdnWc 中的示例重新创建问题,并进行了一些修改。
import time
import pygame,sys
clock = pygame.time.Clock()
from pygame.locals import *
pygame.init() # initiates pygame
pygame.display.set_caption('Pygame Platformer')
WINDOW_SIZE = (300*4,200*4)
screen = pygame.display.set_mode(WINDOW_SIZE,32) # initiate the window
display = pygame.Surface((300,200)) # used as the surface for rendering,which is scaled
moving_right = False
moving_left = False
vertical_momentum = 0
air_timer = 0
true_scroll = [0,0]
def load_map(path):
f = open(path + '.txt','r')
data = f.read()
f.close()
data = data.split('\n')
game_map = []
for row in data:
game_map.append(list(row))
return game_map
game_map = load_map('map')
grass_img = pygame.image.load('grass.png')
dirt_img = pygame.image.load('dirt.png')
player_img = pygame.image.load('player.png').convert()
player_img.set_colorkey((255,255,255))
player_rect = pygame.Rect(100,100,5,13)
background_objects = [[0.25,[120,10,70,400]],[0.25,[280,30,40,[0.5,[30,[130,90,[300,80,120,400]]]
def collision_test(rect,tiles):
hit_list = []
for tile in tiles:
if rect.colliderect(tile):
hit_list.append(tile)
return hit_list
vx = 0
def move(rect,movement,tiles):
global vx
collision_types = {'top':False,'bottom':False,'right':False,'left':False}
vx += movement[0]
rect.x = vx
hit_list = collision_test(rect,tiles)
for tile in hit_list:
if movement[0] > 0:
rect.right = tile.left
collision_types['right'] = True
elif movement[0] < 0:
rect.left = tile.right
collision_types['left'] = True
rect.y += movement[1]
hit_list = collision_test(rect,tiles)
for tile in hit_list:
if movement[1] > 0:
rect.bottom = tile.top
collision_types['bottom'] = True
elif movement[1] < 0:
rect.top = tile.bottom
collision_types['top'] = True
return rect,collision_types
last_time = time.time()
while True: # game loop
dt = time.time() - last_time
last_time = time.time()
display.fill((146,244,255)) # clear screen by filling it with blue
true_scroll[0] += (player_rect.x-true_scroll[0]-152)/20
true_scroll[1] += (player_rect.y-true_scroll[1]-106)/20
scroll = true_scroll.copy()
scroll[0] = int(scroll[0])
scroll[1] = int(scroll[1])
pygame.draw.rect(display,(7,75),pygame.Rect(0,300,80))
for background_object in background_objects:
obj_rect = pygame.Rect(background_object[1][0]-scroll[0]*background_object[0],background_object[1][1]-scroll[1]*background_object[0],background_object[1][2],background_object[1][3])
if background_object[0] == 0.5:
pygame.draw.rect(display,(14,222,150),obj_rect)
else:
pygame.draw.rect(display,(9,91,85),obj_rect)
tile_rects = []
y = 0
for layer in game_map:
x = 0
for tile in layer:
if tile == '1':
display.blit(dirt_img,(x*16-scroll[0],y*16-scroll[1]))
if tile == '2':
display.blit(grass_img,y*16-scroll[1]))
if tile != '0':
tile_rects.append(pygame.Rect(x*16,y*16,16,16))
x += 1
y += 1
player_movement = [0,0]
if moving_right == True:
player_movement[0] += 2 * dt * 60
if moving_left == True:
player_movement[0] -= 2 * dt * 60
player_movement[1] += vertical_momentum
vertical_momentum += 0.2# * dt * 60
if vertical_momentum > 3:
vertical_momentum = 3
print(player_movement,dt)
player_rect,collisions = move(player_rect,player_movement,tile_rects)
if collisions['bottom'] == True:
air_timer = 0
vertical_momentum = 0
else:
air_timer += 1
display.blit(player_img,(player_rect.x-scroll[0],player_rect.y-scroll[1]))
for event in pygame.event.get(): # event loop
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
moving_right = True
if event.key == K_LEFT:
moving_left = True
if event.key == K_UP:
if air_timer < 6:
vertical_momentum = -5
if event.type == KEYUP:
if event.key == K_RIGHT:
moving_right = False
if event.key == K_LEFT:
moving_left = False
screen.blit(pygame.transform.scale(display,WINDOW_SIZE),(0,0))
pygame.display.update()
clock.tick(60)
如果添加增量时间,精灵在移动时会卡顿。
if moving_right == True:
player_movement[0] += 2 * dt * 60
if moving_left == True:
player_movement[0] -= 2 * dt * 60
我用于项目的碰撞和绘图代码。
def drawing(self,screen):
screen.blit(self.covece,(self.hitBox.x - scroll[0],self.hitBox.y - scroll[1]))
def movement(self,listofWalls,dt):
#.speed.x,self.speed.y = 0,0
if self.left:
self.speed.x = -self.vel * dt * 60
if self.right:
self.speed.x = self.vel * dt * 60
if self.up:
self.speed.y = -self.vel * dt * 60
if self.down:
self.speed.y = self.vel * dt * 60
if self.speed.x != 0 and self.speed.y != 0:
self.speed.x *= .7071
self.speed.y *= .7071
print(self.speed)
self.korY += self.speed.y
self.korX += self.speed.x
self.hitBox.x = self.korX
if r := self.hitBox.collidelistall(listofWalls):
for n in r:
if self.speed.x > 0:
self.korX = listofWalls[n].left - self.hitBox.width
if self.speed.x < 0:
self.korX = listofWalls[n].right
self.speed.x = 0
self.hitBox.x = self.korX
self.hitBox.y = self.korY
if r := self.hitBox.collidelistall(listofWalls):
for n in r:
if self.speed.y > 0:
self.korY = listofWalls[n].top - self.hitBox.height
if self.speed.y < 0:
self.korY = listofWalls[n].bottom
self.speed.y = 0
self.hitBox.y = self.korY
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)