[Python] Python sprite visible

Discussão em 'Python' iniciado por Stack, Julho 25, 2025.

  1. Stack

    Stack Membro Participativo

    Apparently, I am making a game which you have to roll the blue block to the "Touch me" text, when I run the code, the Blue block is not visible and there are no problems found in VS code. I have no idea what's happening and I'm really new to coding. Any ideas how I can solve this problem and make the blue block visible?

    import pygame, sys
    WIDTH, HEIGHT = 800, 600
    BLUE = (0, 0, 255)
    WHITE = WHITE = (255, 255, 255)
    GREEN = (0,255,0)
    # money = 100

    screen = pygame.display.set_mode((WIDTH,HEIGHT))
    pygame.display.set_caption("I wanna make a game")
    square_size = 50
    x, y = WIDTH // 2, HEIGHT // 2
    speed = 5



    class Sprite(pygame.sprite.Sprite):
    def __init__(self, color, height, width):
    super().__init__()
    self.image = pygame.Surface([square_size,square_size])
    self.image.fill(BLUE)
    self.image.set_colorkey(BLUE)
    pygame.draw.rect(self.image, color, pygame.Rect(0, 0, width, height))
    self.rect = self.image.get_rect()



    def moveRight(self, pixels):
    self.rect.x += pixels
    def moveLeft(self, pixels):
    self.rect.x -= pixels
    def moveUp(self, speed):
    self.rect.y += speed * speed/10
    def moveDown(self, speed):
    self.rect.y -= speed *speed/10



    pygame.init()
    pygame.font.init()

    player = Sprite(BLUE, 30, 30)
    all_sprites_list = pygame.sprite.Group()
    all_sprites_list.add(player)
    player.rect.x = 50
    player.rect.y = 50

    running = True
    while running:
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    running = False
    font2 = pygame.font.SysFont('arial.ttf', 40)
    touchme = font2.render('Touch me', True, WHITE)



    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
    player.moveUp(10)
    if keys[pygame.K_s]:
    player.moveDown(10)
    if keys[pygame.K_a]:
    player.moveLeft(10)
    if keys[pygame.K_d]:
    player.moveRight(10)

    x = max(0, min(WIDTH - square_size, x))
    y = max(0, min(HEIGHT - square_size, y))
    screen.fill((0,0,0)
    screen.blit(touchme, (10, 300))

    all_sprites_list.update()

    all_sprites_list.draw(screen)
    pygame.display.flip()
    pygame.time.Clock().tick(60)

    pygame.quit()
    sys.exit()

    Continue reading...

Compartilhe esta Página