RaptoRR
RaptoRR

Reputation: 55

How to make my tile system work with pygame in python

Im making a custom tile system for my game. Its my first time and didnt expect to go that far without a tutorial. So i did my best and made something that works when i render in a tile with the blit method but when i use pygame.sprite.Sprite sprites it doesnt work. I need it to be with pygame's sprite system because it makes it easier doing other stuff and im positive its a rookie mistake i just cant find it.

Code:

# Importing libraries
try: 
    import pygame
    from pygame.locals import *
    from time import *
    from os import *
    from random import *
    import sys
    print("All Libraries imported successfully \n")
except :
    print("Error while importing modules and libraries")
    sys.exit()
    
# Creating Screen

screen_width = 640
screen_height = 640
tile_size = int(screen_width / 10)
screen_res = (screen_width, screen_height)
clock = pygame.time.Clock()
running_fps = 144

screen = pygame.display.set_mode(screen_res)
pygame.display.set_caption(f"GTA Circuit Hack FPS:{running_fps}, {screen_width} x {screen_height}, Tile Size: {tile_size}")

# Loading images

#    Background image:
backgroundImg = pygame.image.load("Background.png")
backgroundImg = pygame.transform.scale(backgroundImg, screen_res)

#    Not walkable tile image:
noWalkTileImg = pygame.image.load("unwalkTile.png")
noWalkTileImg = pygame.transform.scale(noWalkTileImg, (tile_size, tile_size))

# making tiles

tiles = []
tile_y = 0
tile_x = 0
for i in range(10):
    
    if i > 0:
        tile_y += tile_size
        
    
    for i in range(10):
        if i > 0:
            tile_x += tile_size
            
            
        tiles.append((tile_x, tile_y))
    tile_x = 0
    
tile_x = 0
tile_y = 0
        
# creating object classes

class BadTile (pygame.sprite.Sprite):
    def __init__(self, tile):
        pygame.sprite.Sprite.__init__(self)
        self.image = noWalkTileImg
        self.tile = tile
        self.rect = self.image.get_rect()
        self.x = self.tile[0]
        self.y = self.tile[1]  
        
    def update(self):
        self.x = self.tile[0]
        self.y = self.tile[1]  

# creating objects

tile1 = BadTile(tiles[99])
game_tiles = pygame.sprite.Group()
game_tiles.add(tile1)

run = True
while run:
    clock.tick(running_fps)
    screen.blit(backgroundImg, (0,0))

    # rendering
    
    game_tiles.update()
    game_tiles.draw(screen)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            
    pygame.display.update()

pygame.quit()
print ("Exited with no errors")
sys.exit()

Images:

Upvotes: 1

Views: 1156

Answers (1)

Rabbid76
Rabbid76

Reputation: 211277

pygame.sprite.Group.draw() and pygame.sprite.Group.update() are methods which are provided by pygame.sprite.Group.

The latter delegates to the update method of the contained pygame.sprite.Sprites — you have to implement the method. See pygame.sprite.Group.update():

Calls the update() method on all Sprites in the Group. [...]

The former uses the image and rect attributes of the contained pygame.sprite.Sprites to draw the objects — you have to ensure that the pygame.sprite.Sprites have the required attributes. See pygame.sprite.Group.draw():

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect. [...]

So you need to set self.rect.x and self.rect.y. Instead of self.x and self.y. You don't need self.x and self.y at all:

class BadTile (pygame.sprite.Sprite):
    def __init__(self, tile):
        pygame.sprite.Sprite.__init__(self)
        self.image = noWalkTileImg
        self.tile = tile
        self.rect = self.image.get_rect()
        self.rect.x = self.tile[0]
        self.rect.y = self.tile[1]  
        
    def update(self):
        self.rect.x = self.tile[0]
        self.rect.y = self.tile[1] 

Upvotes: 1

Related Questions