1

My idea was to safe the rectangles in world[] to then move them every game tick to the left by using: velocity_map. The class World is only for creating the pattern of rectangles, that are supposed to move. I want this pattern of rectangles to move to the left, but instead of moving them, a lot of new ones, will be created wihle moving to the left, but the old ones are not beeing removed by the pygame.display.update() function. How do I move the them correctly?

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, 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, 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)
                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, 0), (0, line * tile_size), (1280, line * tile_size))
        pygame.draw.line(screen, (0, 0, 0), (line * tile_size, 0), (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), 0, 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, 0, 0, 1],
    [0, 0, 0, 0],
    [0, 1, 1, 0],
    [0, 0, 0, 0],
    [0, 1, 1, 0],
    [0, 0, 0, 0],
    [0, 1, 1, 0],
    [0, 0, 2, 0],
]

# 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)
Silasso
  • 13
  • 2

2 Answers2

1

Before you draw new information to the screen you want to clear it.

to do this you want to do

screen.fill((0,0,0))

another problem i see is that you are drawing to the screen in a function that you never passed screen too, what you might want to do is have a new variable in the draw function for screen so

def draw(screen,draw_list):
  #draw code

draw(screen, world)
hbblue
  • 305
  • 3
  • 14
1

You have to clear the display in every frame with 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)

The typical PyGame application loop has to:

Rabbid76
  • 202,892
  • 27
  • 131
  • 174