问题描述
我的想法是保护 world[]
中的矩形,然后使用:veLocity_map 将它们在每个游戏刻度向左移动。类 World 仅用于创建应该移动的矩形图案。
我希望这种矩形模式向左移动,但不是移动它们,而是在向左移动时创建许多新的矩形,但旧的不会被 pygame.display.update()
函数删除。
如何正确移动它们?
import sys
import pygame
class World():
def setupMap(data):
tile_list = []
row_count = 0
for row in data:
col_count = 0
for tile in row:
if tile == 1:
barrier_surface = pygame.Surface([80,80])
barrier_surface.fill([0,0])
barrier_rect = barrier_surface.get_rect()
barrier_rect.x = col_count * tile_size
barrier_rect.y = row_count * tile_size
tile = barrier_surface,barrier_rect
tile_list.append(tile)
if tile == 2:
barrier_surface = pygame.Surface([80,80])
barrier_surface.fill([255,barrier_rect
tile_list.append(tile)
col_count += 1
row_count += 1
return tile_list
def draw(tile_list):
for tile in tile_list:
screen.blit(tile[0],tile[1])
def move(tile_list):
for h in tile_list:
h[1].centerx -= veLocity_map
return tile_list
def draw_grid():
for line in range(0,20):
pygame.draw.line(screen,(0,0),line * tile_size),(1280,line * tile_size))
pygame.draw.line(screen,(line * tile_size,640))
def drawBlocks(tile):
for h in tile:
screen.blit(h[0],h[1])
# Window settings
pygame.init()
screen = pygame.display.set_mode((1280,640),32)
screen.fill([255,255,255])
pygame.display.set_caption("Geometrydash")
# Game Variables
tile_size = 80
veLocity_map = 1
# Map 1= Block 2= Spike
world_data = [
[0,1],[0,0],1,2,]
# Welt klasse laden
world = World.setupMap(world_data)
# Clock
clock = pygame.time.Clock()
Run = True
while Run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
world = move(world)
draw(world)
draw_grid()
clock.tick(60)
解决方法
在将新信息绘制到屏幕之前,您要清除它。
做你想做的事
screen.fill((0,0))
我看到的另一个问题是,您正在一个从未通过 screen 的函数中绘制到屏幕上,您可能想要做的是在 screen 的绘制函数中添加一个新变量
def draw(screen,draw_list):
#draw code
draw(screen,world)
,
您必须使用 pygame.Surface.fill
清除每一帧中的显示:
Run = True
while Run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
world = move(world)
screen.fill([255,255,255])
draw(world)
draw_grid()
pygame.display.update()
clock.tick(60)
典型的 PyGame 应用程序循环必须:
- 通过
pygame.event.pump()
或pygame.event.get()
处理事件。 - 根据输入事件和时间(分别为帧)更新游戏状态和对象位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象) - 通过
pygame.display.update()
或pygame.display.flip()
更新显示 - 限制每秒帧数以限制 CPU 使用率