- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
下面是创建和管理游戏管道的基本代码:
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/
我想让我的 UICollectionViewCell 在长时间按下 (UILongPressGestureRecognizer) 后摇晃以便删除一个项目。 我读过这个post和另一个 one . 我按
我有两个 SkSpriteNode,分别是 left-ball 和 right-ball。它们从屏幕的两侧流出。 我为这两个球实现了碰撞检测。但是我注意到,在我添加碰撞检测之前,球在没有任何晃动效果的
我在使用Github的 Action 时遇到了一个奇怪的问题。我有一个Gradle任务,可以将发行说明打印到文件中(在本地工作得很好)。但是使用GitHub操作似乎无法将–pretty=format:
我将 Cannon.js 与 Three.js 结合使用。我设置了一个场景,其中有 5 列,每列 4 个 block 相互堆叠。 我希望它们能够与我计划添加到场景中的其他对象进行交互。然而,列中的 b
我在 tableview 上使用了自动布局,但在 HeightforRowAtIndexPath 中计算了图像框架、标题和副标题以及高度。 & 滚动 tableview 抖动 最佳答案 在 viewD
我是一名优秀的程序员,十分优秀!