Home > OS >  I don't think it's getting all the info from other files
I don't think it's getting all the info from other files

Time:10-12

I'm working on a pygame project but when I run it, it's not working. There are a lot of messages in my code saying the files imported may be "undefined or defined" which is confusing. I can't tell if that's the reason or not. (I'm using repl.it, and just started trying to learn pygame a week ago)

main.py CODE:

from settings import *
from tiles import tiles
#pygame setup
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
test_tile = pygame.sprite.Group(Tile(100, 100), 200)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

screen.fill('black')
test.tile.draw(screen)

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

settings.py CODE:

  '                              ',
  '                              ',
  '                              ',
  ' XX    XXX              XX    ',
  ' XX                           ',
  ' XXXX          XX          XX ',
  ' XXXX        XX               ',
  ' XX    X   XXXX    XX    XX   ',
  '       X   XXXX    XX    XXX  ',
  '    XXXX   XXXXXX  XX    XXXX ',
  'XXXXXXXX   XXXXXX  XX    XXXX ',]

tile_size = 64
screen_width = 1200
screen_height = len(level_map) * tile_size

tiles.py CODE:


class Tile(pygame.sprite.Sprite):
  def __init__(self,pos,size):
    super().__init__()
    self.image = pygame.Surface((size,size))
    self.image.fill('grey')
    self.rect = self.image.get_rect(topleft = pos)

CodePudding user response:

You're code is almost there. For the moment I suggest simply putting everything in a single file. Obviously in the long-term this gets messy, but for now it will be easier.

There's a couple of bugs around the Sprite Group creation, and adding the Sprite to it.

The code needs to first create a Sprite Group, then add Sprites to it. It's possible to create the tiles as they're added to the group, all in single line of code, but this will be unworkable later.

map_tiles = pygame.sprite.Group()            # Sprite Group
test_tile = Tile( (100, 100), TILE_SIZE )    # Sprite

map_tiles.add( test_tile )                   # Put Sprite into Sprite Group

With that change, and a few tweaks, it's working:

import pygame

### Settings
level_map = [ 
  '                              ',
  '                              ',
  '                              ',
  ' XX    XXX              XX    ',
  ' XX                           ',
  ' XXXX          XX          XX ',
  ' XXXX        XX               ',
  ' XX    X   XXXX    XX    XX   ',
  '       X   XXXX    XX    XXX  ',
  '    XXXX   XXXXXX  XX    XXXX ',
  'XXXXXXXX   XXXXXX  XX    XXXX ',]

GREY  = ( 128, 128, 128 )
BLACK = (   0,   0,   0 )

TILE_SIZE = 64
SCREEN_WIDTH  = len( level_map[0] ) * TILE_SIZE
SCREEN_HEIGHT = len( level_map ) * TILE_SIZE


### Tile Sprite class
class Tile(pygame.sprite.Sprite):
  def __init__(self,pos,size):
    super().__init__()
    self.image = pygame.Surface((size,size), pygame.SRCALPHA)
    self.image.fill( GREY )
    self.rect = self.image.get_rect(topleft = pos)


#pygame setup
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
map_tiles = pygame.sprite.Group()           # Group of all map tiles
test_tile = Tile( (100, 100), TILE_SIZE )   # A single map Tile

map_tiles.add( test_tile )                  # Put tile-sprite into group


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

    # paint the screen
    screen.fill( BLACK )
    map_tiles.draw(screen)
    pygame.display.update()

    # clamp refresh-rate to 60 FPS
    clock.tick(60)

# exiting    
pygame.quit()
#sys.exit()

Typically all constants, for example "tile-size" are written in all-capitals. This allows the person reading the code to immediately know what it represents when reading it.

  • Related