I'm trying to implement a simple platformer, but I have a problem with the sprites. They blink and do not move. A platform appears on the left mouse button, a square appears on the right mouse button, a square moves on the left and right arrows. It should output like this
import pygame
class Square(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__(sprites)
self.image = pygame.Surface((width, height), pygame.SRCALPHA, 32)
self.rect = pygame.Rect(x, y, width, height)
pygame.draw.rect(screen, pygame.Color('blue'), pygame.Rect(x, y, width, height))
def new_rect(self, x, y, width, height):
self.rect = pygame.Rect(x, y, width, height)
pygame.draw.rect(screen, pygame.Color('blue'), pygame.Rect(x, y, width, height))
def fall(self):
if not pygame.sprite.spritecollideany(self, platforms):
self.rect = self.rect.move(0, 1)
def move(self, direction):
if direction == 'left':
self.rect = self.rect.move(-10, 0)
elif direction == 'right':
self.rect = self.rect.move(10, 0)
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__(sprites)
self.add(platforms)
self.image = pygame.Surface((width, height), pygame.SRCALPHA, 32)
self.rect = pygame.Rect(x, y, width, height)
pygame.draw.rect(screen, pygame.Color('gray'), pygame.Rect(x, y, width, height))
sprites = pygame.sprite.Group()
platforms = pygame.sprite.Group()
screen = pygame.display.set_mode((500, 500))
CUBEFALL = pygame.USEREVENT + 1
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
Platform(event.pos[0], event.pos[1], 50, 10)
elif event.button == 3:
cube = Square(event.pos[0], event.pos[1], 20, 20)
pygame.time.set_timer(CUBEFALL, 20)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
cube.move('left')
elif event.key == pygame.K_RIGHT:
cube.move('right')
if event.type == CUBEFALL:
screen.fill(pygame.Color('black'))
cube.fall()
sprites.draw(screen)
pygame.display.flip()
pygame.quit()
I tried to fix this problem but I didn't succeed.