[Python] Pygame method not working in a simple OOP

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to create a simple Car class in Python using pygame, but my draw() method doesn't seem to work. The rectangle is not appearing on the screen.

    import pygame

    pygame.init()

    class Car:
    def __init__(self, x, y, color=(255, 0, 0), width=50, height=30):
    self.x = x
    self.y = y
    self.color = color
    self.width = width
    self.height = height

    def draw(self, screen):
    pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))

    # Initialize pygame screen
    screen = pygame.display.set_mode((500, 500))
    clock = pygame.time.Clock()

    car = Car(200, 200) # Create a car object

    running = True

    Continue reading...

Compartilhe esta Página