pythonpython-3.xpygamesprite-sheet

Sprite sheet creating a window of the image surface


I'm trying to recreate the chrome no internet dino game inside pygame/python. But whenever I run it it instead of creating the main file window it creates the image window from the sprite sheet file. Don't know what I'm doing wrong here.

main file:

# importing the other file where the spritesheet class is
from SpriteSheet import SpriteSheet

import pygame

pygame.init()

SW = 500
SH = 500
win = pygame.display.set_mode((SW, SH))
SPRITE_SHEET = pygame.image.load('sheet.png')

# mainloop
run = True
while run:

# checking for clicking the x button
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

# cropping the image from the spritesheet
        spritesheet = SpriteSheet(SPRITE_SHEET)
        image = spritesheet.get_image(
            848, 2, 44, 47, (255, 255, 255))
        win.blit(image, (0, 0))

sprite sheet file:

import pygame

# creating the class
class SpriteSheet(object):
    SpriteSheet = None

# getting the spritesheet
    def __init__(self, sheet):
        self.SpriteSheet = sheet

# cropping the image
    def get_image(self, x, y, width, height, color):
        image = pygame.display.set_mode([width, height])

        image.blit(self.SpriteSheet, (0, 0), (x, y, width, height))

        image.set_colorkey(color)

        return image

Solution

  • instead of creating the main file window it creates the image window

    No. There is nothing like a image window.

    The content of the pygame.Surface object which is associated to the display is what you see in the window.

    win = pygame.display.set_mode((SW, SH))
    

    First you blit SPRITESHEET on image (in SpriteSheet.get_image())

    image.blit(self.SpriteSheet, (0, 0), (x, y, width, height)) 
    

    Now the content of image is the SPRITESHEET. Then you blit image on win

    win.blit(image, (0, 0))
    

    Hence the content of image (which is the same as SPRITESHEET) is displayed.

    When you don't want to see the content of SPRITESHEET in the window, then you have to remove the line

    >win.blit(image, (0, 0))


    By the way, pygame wiki provides a nice and complete Spritesheet example.