在 pygame 内连续生成钢琴块中的块
Generate tiles in piano tiles consecutively in pygame
我在 pygame 中创建了一个简单的钢琴块游戏克隆。
一切正常,除了我在每隔一定间隔后生成图块的方式,但随着游戏速度的增加,这会在两个图块之间留下间隙。
在游戏的原始版本中,两个传入的图块之间没有滞后(0 距离)。
这是游戏的预览:
目前我正在生成这样的图块:
ADDBLOCK = pygame.USEREVENT + 1
ADDTIME = 650
pygame.time.set_timer(ADDBLOCK, ADDTIME)
if event.type == ADDBLOCK:
x_col = random.randint(0,3)
block = Block(win, (67.5 * x_col, -120))
block_group.add(block)
但是随着时间的推移,这些图块的速度会增加,因此在预览中红线所示的两个图块之间仍然存在差距。有什么方法可以连续生成瓦片吗?
使用变量number
了解自开始以来生成了多少个图块。这个变量将从0开始,然后每生成一个tile你就给这个变量加1。
然后,您可以使用像 scrolling
这样不断增加的变量。您会将此滚动添加到每个图块 y
pos 以呈现它们。
现在您只需添加一个 y
位置类似于 -tile_height - tile_height * number
的图块。
如果这对您来说没有意义,请查看此 MRE:
import pygame
from pygame.locals import *
from random import randint
pygame.init()
screen = pygame.display.set_mode((240, 480))
clock = pygame.time.Clock()
number_of_tiles = 0
tile_height = 150
tile_surf = pygame.Surface((60, tile_height))
tiles = [] # [column, y pos]
scrolling = 0
score = 0
speed = lambda: 200 + 5*score # increase with the score
time_passed = 0
while True:
click = None # used to click on a tile
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == MOUSEBUTTONDOWN:
click = event.pos
screen.fill((150, 200, 255))
if scrolling > number_of_tiles * tile_height:
# new tile
# use "while" instead of "if" if you want to go at really high speed
tiles.append([randint(0, 3), -tile_height - number_of_tiles * tile_height])
number_of_tiles += 1
for x, y in tiles:
screen.blit(tile_surf, (60 * x, y + scrolling))
if y + scrolling > 480: # delete any tile that is no longer visible
tiles.remove([x, y])
if click is not None and Rect((60 * x, y + scrolling), tile_surf.get_size())
.collidepoint(click):
tiles.remove([x, y]) # delete any tile that has been clicked
score += 1 # used to calculate speed
scrolling += speed() * time_passed
pygame.display.flip()
time_passed = clock.tick() / 1000
我在 pygame 中创建了一个简单的钢琴块游戏克隆。
一切正常,除了我在每隔一定间隔后生成图块的方式,但随着游戏速度的增加,这会在两个图块之间留下间隙。
在游戏的原始版本中,两个传入的图块之间没有滞后(0 距离)。
这是游戏的预览:
目前我正在生成这样的图块:
ADDBLOCK = pygame.USEREVENT + 1
ADDTIME = 650
pygame.time.set_timer(ADDBLOCK, ADDTIME)
if event.type == ADDBLOCK:
x_col = random.randint(0,3)
block = Block(win, (67.5 * x_col, -120))
block_group.add(block)
但是随着时间的推移,这些图块的速度会增加,因此在预览中红线所示的两个图块之间仍然存在差距。有什么方法可以连续生成瓦片吗?
使用变量number
了解自开始以来生成了多少个图块。这个变量将从0开始,然后每生成一个tile你就给这个变量加1。
然后,您可以使用像 scrolling
这样不断增加的变量。您会将此滚动添加到每个图块 y
pos 以呈现它们。
现在您只需添加一个 y
位置类似于 -tile_height - tile_height * number
的图块。
如果这对您来说没有意义,请查看此 MRE:
import pygame
from pygame.locals import *
from random import randint
pygame.init()
screen = pygame.display.set_mode((240, 480))
clock = pygame.time.Clock()
number_of_tiles = 0
tile_height = 150
tile_surf = pygame.Surface((60, tile_height))
tiles = [] # [column, y pos]
scrolling = 0
score = 0
speed = lambda: 200 + 5*score # increase with the score
time_passed = 0
while True:
click = None # used to click on a tile
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == MOUSEBUTTONDOWN:
click = event.pos
screen.fill((150, 200, 255))
if scrolling > number_of_tiles * tile_height:
# new tile
# use "while" instead of "if" if you want to go at really high speed
tiles.append([randint(0, 3), -tile_height - number_of_tiles * tile_height])
number_of_tiles += 1
for x, y in tiles:
screen.blit(tile_surf, (60 * x, y + scrolling))
if y + scrolling > 480: # delete any tile that is no longer visible
tiles.remove([x, y])
if click is not None and Rect((60 * x, y + scrolling), tile_surf.get_size())
.collidepoint(click):
tiles.remove([x, y]) # delete any tile that has been clicked
score += 1 # used to calculate speed
scrolling += speed() * time_passed
pygame.display.flip()
time_passed = clock.tick() / 1000