gpt4 book ai didi

python - 我的 Pygame Flappy Bird 克隆中的管道出现滞后和摇晃,而不是流畅移动

转载 作者:太空宇宙 更新时间:2023-11-03 21:25:20 25 4
gpt4 key购买 nike

下面是创建和管理游戏管道的基本代码:

import pygame as pg
import sys,os,math,time,random

# colours
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)

# general stuff
WIDTH = 1024
HEIGHT = 576
FPS = 60

# other
all_events = [pg.QUIT, pg.ACTIVEEVENT, pg.KEYDOWN, pg.KEYUP, pg.MOUSEMOTION,
pg.MOUSEBUTTONUP, pg.MOUSEBUTTONDOWN, pg.VIDEORESIZE,
pg.VIDEOEXPOSE, pg.USEREVENT]

pg.init()
screen = pg.display.set_mode((WIDTH, HEIGHT))
clock = pg.time.Clock()

# Class to manage Pipes
class Pipe_Manager:

def __init__(self):
self.pipe_width = 50
self.pipes = []
self.pipe_speed = 5
self.max_tick = 75
self.spawn_tick = self.max_tick

def manage_pipes(self):
self.spawner()
self.manage()
self.display()

def make_pipe(self):

height = random.randint(100,326)
gap = random.randint(100,250)
surf1 = pg.Surface((self.pipe_width, height))
surf1.fill(green)
surf2 = pg.Surface((self.pipe_width, HEIGHT - (height + gap)))
surf2.fill(green)

# surface, (x,y) and vertical height
pipe = [surf1, [WIDTH, 0], height]
pipe2 = [surf2, [WIDTH, height + gap], HEIGHT - (height + gap)]
self.pipes.append(pipe)
self.pipes.append(pipe2)

def spawner(self):

if self.spawn_tick == self.max_tick:
self.make_pipe()
self.spawn_tick = 0

self.spawn_tick += 1

def manage(self):

for pipe in self.pipes:

# move the pipe
pipe[1][0] -= self.pipe_speed

# check if it's off screen
if pipe[1][0] + self.pipe_width < 0:
self.pipes.remove(pipe)

def display(self):
for pipe in self.pipes:
screen.blit(pipe[0], (pipe[1][0], pipe[1][1]))

################################################################################

pg.event.set_blocked(all_events)
pg.event.set_allowed([pg.QUIT, pg.KEYDOWN])

pipe_manager = Pipe_Manager()
loop = True
while loop:
screen.fill(white)
pipe_manager.manage_pipes()
pg.display.update()
clock.tick(FPS)

管道在水平移动时似乎会摇晃,有时顶部管道与底部管道未对齐。

我希望这不是我的计算机特有的问题,因为我已经抽象出了大量的 flappy-bird-clone 代码,并且这个管道滞后问题的根源一定就在这里。

最佳答案

问题是这段代码:

    for pipe in self.pipes: 

# move the pipe
pipe[1][0] -= self.pipe_speed

# check if it's off screen
if pipe[1][0] + self.pipe_width < 0:
self.pipes.remove(pipe)

您可以在此处更改当前正在迭代的列表。一旦管道从列表中删除,下一个管道就会错过一个移动步骤。

看一下下面的示例,您可以在其中自己发现问题(看看输出中如何缺少 5,因为我们删除了 4):

>>> l = [1,2,3,4,5,6,7,8,9,10]
>>> for x in l:
... if x == 4:
... l.remove(x)
... print(x)
...
1
2
3
4
6
7
8
9
10
>>>

(其他语言禁止更改您当前迭代的序列是有原因的)。

一个简单的修复方法是首先复制列表:

    for pipe in self.pipes[:]: 
<小时/>

为了使运动更顺畅,请尝试提高帧速率并使用时间步长。这是一种可能的方法:

import pygame as pg
import sys,os,math,time,random

# colours
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)

# general stuff
WIDTH = 1024
HEIGHT = 576
FPS = 120

# other
all_events = [pg.QUIT, pg.ACTIVEEVENT, pg.KEYDOWN, pg.KEYUP, pg.MOUSEMOTION,
pg.MOUSEBUTTONUP, pg.MOUSEBUTTONDOWN, pg.VIDEORESIZE,
pg.VIDEOEXPOSE, pg.USEREVENT]

pg.init()
screen = pg.display.set_mode((WIDTH, HEIGHT))
clock = pg.time.Clock()


class Pipe:
def __init__(self, img, pos):
self.img = img
self.pos = pos

# Class to manage Pipes
class Pipe_Manager:

def __init__(self):
self.pipe_width = 50
self.pipes = []
self.pipe_speed = 0.3
self.max_tick = 1500
self.spawn_tick = self.max_tick

def manage_pipes(self, dt):
self.spawner(dt)
self.manage(dt)
self.display()

def make_pipe(self):

height = random.randint(100,326)
gap = random.randint(100,250)
surf1 = pg.Surface((self.pipe_width, height))
surf1.fill(green)
surf2 = pg.Surface((self.pipe_width, HEIGHT - (height + gap)))
surf2.fill(green)

pipe = Pipe(surf1, pg.Vector2(WIDTH, 0))
pipe2 = Pipe(surf2, pg.Vector2(WIDTH, height + gap))
self.pipes.append(pipe)
self.pipes.append(pipe2)

def spawner(self, dt):

if self.spawn_tick >= self.max_tick:
self.make_pipe()
self.spawn_tick = 0

self.spawn_tick += dt

def manage(self, dt):

for pipe in self.pipes[:]:

# move the pipe
pipe.pos.x -= self.pipe_speed * dt

# check if it's off screen
if pipe.pos.x + self.pipe_width < 0:
self.pipes.remove(pipe)

def display(self):
for pipe in self.pipes:
screen.blit(pipe.img, pipe.pos)

################################################################################

pg.event.set_blocked(all_events)
pg.event.set_allowed([pg.QUIT, pg.KEYDOWN])

pipe_manager = Pipe_Manager()
loop = True
dt=0
while loop:
for e in pg.event.get():
if e.type == pg.QUIT:
loop = False
screen.fill(white)
pipe_manager.manage_pipes(dt)
pg.display.update()
dt=clock.tick(FPS)

关于python - 我的 Pygame Flappy Bird 克隆中的管道出现滞后和摇晃,而不是流畅移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53878334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com