微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pygame 移动矩形 |不更新

如何解决pygame 移动矩形 |不更新

我的想法是保护 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 应用程序循环必须:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。