1

My kid is learning python via pygame he saw in online tutorial about sprites and wrote this code, but we dont see anything

import pygame
import random

WIDTH = 360
HEIGHT = 480
FPS = 30

# colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)


class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill(WHITE)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH / 2, HEIGHT / 2)


pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("my game")
clock = pygame.time.Clock()

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

# game loop
running = True
while running:
    # keep the loop running at the right speed
    clock.tick(FPS)
    # events
    for event in pygame.event.get():
        # check for closing the window
        if event.type == pygame.QUIT:
            running = False

    # update
    all_sprites.update()
    # draw/render
    screen.fill(BLACK)
    all_sprites.draw(screen)
    pygame.display.flip()

pygame.quit()
li-raz
  • 1,678
  • 2
  • 29
  • 57
  • There isn't an obvious bug in this code, it seems to be working fine. Except: Where is `import pygame`? – Rabbid76 Oct 14 '20 at 12:54
  • forgot to paste it – li-raz Oct 14 '20 at 12:56
  • 2
    I guess you use macOS? Pygame works notoriously bad on macOS. – sloth Oct 14 '20 at 13:02
  • yes , I am working on mac. need to move to ubuntu? – li-raz Oct 14 '20 at 13:21
  • 1
    If you want to work with pygame, I would sugegst you do. Or you search stackoverflow for similiar questions. Maybe [this](https://stackoverflow.com/questions/54263575/pygame-on-mac-mojave) or [this](https://stackoverflow.com/questions/52718921/problems-getting-pygame-to-show-anything-but-a-blank-screen-on-macos-mojave) helps? – sloth Oct 14 '20 at 13:28

0 Answers0