pythonpygame

Sprites are not showing on screen in pygame?


I am writing a game in pygame and trying create 2 sprites, 1 as a polygon and the other a rectangle. I have done a lot of research into this but cannot find how to resolve it. I am creating 2 classes that inherit from pygame.sprite.Sprite and then instantiating 2 objects. The problem is all i get is a black screen. ATM im trying to just get the sprites on the screen. Here is the code.


import pygame

# Initialize Pygame
pygame.init()

# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLACK = (0, 0, 0)


class Terrain(pygame.sprite.Sprite):
    points = [(100, 100), (200, 200), (300, 100), (100, 100)]

    def __init__(self, size, pos):
        pygame.sprite.Sprite.__init__(self)

        # create transparent background image
        self.image = pygame.Surface(size, pygame.SRCALPHA, 32)
        self.rect = self.image.get_rect()
        self.rect.topleft = pos

        pygame.draw.polygon(self.image, RED, self.points)

        # Create the collision mask (anything not transparent)
        self.mask = pygame.mask.from_surface(self.image)

    def update(self):
        pygame.draw.polygon(self.image, RED, self.points)


class Star(pygame.sprite.Sprite):
    def __init__(self, size, pos):
        pygame.sprite.Sprite.__init__(self)

        # create transparent background image
        self.image = pygame.Surface(size, pygame.SRCALPHA, 32)
        self.rect = self.image.get_rect()
        self.rect.topleft = pos

        pygame.draw.rect(self.image, WHITE, self.rect)

        # Create the collision mask (anything not transparent)
        self.mask = pygame.mask.from_surface(self.image)

    def update(self):
        pygame.draw.rect(self.image, WHITE, self.rect)


# Screen size
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
screen.fill((0, 0, 0))

pygame.display.set_caption("Collision Detection")

clock = pygame.time.Clock()

terrain = Terrain((200, 100), (100, 100))
star = Star((10, 10), (120, 111))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    collision = terrain.mask.overlap(star.mask, (120, 111))

    if collision:
        screen.fill((255, 255, 255))

    screen.blit(terrain.image, terrain.rect)
    screen.blit(star.image, star.rect)

    pygame.display.update()
    clock.tick(60)

pygame.quit()

I would appreciate any help. Thanks in advance. Malcolm


Solution

  • You are drawing outside the area of the pygame.Surface objects. You have to draw the polygon and rectangles in the coordinates system of the image but not in the coordinate system of the screen:

    points = [(100, 100), (200, 200), (300, 100), (100, 100)]

    points = [(0, 0), (100, 0), (100, 100), (0, 100)]
    

    pygame.draw.rect(self.image, WHITE, self.rect)

    pygame.draw.rect(self.image, WHITE, self.image.get_rect())
    

    The coordinates of the pygame.draw functions are always relative to the surface being drawn on, but not relative to the coordinates where the surface is later placed on the screen.

    In addition, you are not using pygame.sprite.Sprite as intended. I recommend reading How to use sprites in PyGame?, What does pygame.sprite.Group() do and Can't figure out how to check mask collision between two sprites