对象在 pygame 中移动时闪烁
Object is flickering while moving in pygame
我正在尝试使用 pygame 创建一个游戏,当对象移动时它会闪烁 我尝试了 screen.fill () 和更新 () 但没有任何效果。
此问题可能会重复但与此问题相关的答案无效
import pygame, sys,random
def create_block():
global rand_block
rand_block_rect=rand_block.get_rect(topleft = (random.choice(blck_pos),-20))
return rand_block_rect
def move_block(blocks):
for block in blocks:
block.centery+=5
return blocks
def draw_block(blocks):
for block in blocks:
screen.blit(rand_block,block)
pygame.init()
screen = pygame.display.set_mode((500, 600))
screen.fill((255, 255, 255))
clock=pygame.time.Clock()
# blocks
blocks={
1:pygame.image.load('blocks/BLOCK L.png'),
2:pygame.image.load('blocks/BLOCK S.png'),
3:pygame.image.load('blocks/BLOCK M.png'),
}
blck_array=[blocks[1],blocks[2],blocks[3]]
blck_pos=[3,47,190,250,357]
block_list=[]
SPAWNPIPE=pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE,1200)
bal_x=0
loop = True
while loop:
rand_block = random.choice(blck_array)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==SPAWNPIPE:
block_list.append(create_block())
print(block_list)
screen.fill((255,255,255))
block_list=move_block(block_list)
draw_block(block_list)
pygame.display.update()
clock.tick(120)```
问题是你在每一帧都选择了一个新的随机块。创建新块时只需选择一个新块:
def create_block():
global rand_block
rand_block = random.choice(blck_array) # <-- INSERT
rand_block_rect = rand_block.get_rect(topleft = (random.choice(blck_pos),-20))
return rand_block_rect
while loop:
# rand_block = random.choice(blck_array) <-- DELETE
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==SPAWNPIPE:
block_list.append(create_block())
print(block_list)
screen.fill((255,255,255))
block_list=move_block(block_list)
draw_block(block_list)
pygame.display.update()
clock.tick(120)
我正在尝试使用 pygame 创建一个游戏,当对象移动时它会闪烁 我尝试了 screen.fill () 和更新 () 但没有任何效果。 此问题可能会重复但与此问题相关的答案无效
import pygame, sys,random
def create_block():
global rand_block
rand_block_rect=rand_block.get_rect(topleft = (random.choice(blck_pos),-20))
return rand_block_rect
def move_block(blocks):
for block in blocks:
block.centery+=5
return blocks
def draw_block(blocks):
for block in blocks:
screen.blit(rand_block,block)
pygame.init()
screen = pygame.display.set_mode((500, 600))
screen.fill((255, 255, 255))
clock=pygame.time.Clock()
# blocks
blocks={
1:pygame.image.load('blocks/BLOCK L.png'),
2:pygame.image.load('blocks/BLOCK S.png'),
3:pygame.image.load('blocks/BLOCK M.png'),
}
blck_array=[blocks[1],blocks[2],blocks[3]]
blck_pos=[3,47,190,250,357]
block_list=[]
SPAWNPIPE=pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE,1200)
bal_x=0
loop = True
while loop:
rand_block = random.choice(blck_array)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==SPAWNPIPE:
block_list.append(create_block())
print(block_list)
screen.fill((255,255,255))
block_list=move_block(block_list)
draw_block(block_list)
pygame.display.update()
clock.tick(120)```
问题是你在每一帧都选择了一个新的随机块。创建新块时只需选择一个新块:
def create_block():
global rand_block
rand_block = random.choice(blck_array) # <-- INSERT
rand_block_rect = rand_block.get_rect(topleft = (random.choice(blck_pos),-20))
return rand_block_rect
while loop:
# rand_block = random.choice(blck_array) <-- DELETE
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==SPAWNPIPE:
block_list.append(create_block())
print(block_list)
screen.fill((255,255,255))
block_list=move_block(block_list)
draw_block(block_list)
pygame.display.update()
clock.tick(120)