0

What is the best way to have separate lists of blockades in pygame? Should I have lots of separate classes for each individual block (section I don't want to be able to walk on) or are you able to have a class for each different 'type' of blockade, eg: doors, walls, rocks. Will this become an issue when I am making the game that if you leave the town, you end up in the next path to the next town. (I am making a Pokemon game. My current code is as such:

import pygame
from pygame.locals import*

#initialise pygame
pygame.init()

WHITE = (255,255,255)

#counts which sprite you should be on when running


#create screen
screen_width = 160
screen_height = 144
screen_multiplier = 4
screen = pygame.display.set_mode(((screen_width*screen_multiplier),      (screen_height*screen_multiplier)))
pygame.display.set_caption('Pokemon Blood Red')

#Sprite stuff
sprite = pygame.image.load('player_east_still.png')
#Reform the sprite
sprite = pygame.transform.scale(sprite, (10*screen_multiplier, 14*screen_multiplier))
sprite.set_colorkey(WHITE)

#############################################################################
class Player(pygame.sprite.Sprite):

    def __init__(self):
        super(Player, self).__init__()
        self.player_sprite = pygame.Surface((10*screen_multiplier, 14*screen_multiplier))
        self.player_sprite.fill(WHITE)
        self.rect = self.player_sprite.get_rect()
        self.rect.x = 200
        self.rect.y = 200

class Blocks(pygame.sprite.Sprite):

    def __init__(self):
        super(Blocks, self).__init__()
        self.trees = pygame.Surface((10*screen_multiplier, 14*screen_multiplier))
        self.trees.fill(WHITE)
        self.rect = self.trees.get_rect()
        self.rect.x = 25
        self.rect.y = 25


player = Player()
blocks = Blocks()
pygame.init()

amount_caught = 0
place = 1
catch1 = {'pokemon':'none',
          'hp':0,
          'attack':0,
          'defence':0,
          'sp_attack':0,
          'sp_defence':0,}
background = 1
def area_load():
    global background
    if background == 1:
        background = pygame.image.load('neuory_town.png').convert()
        background = pygame.transform.scale(background, (160*screen_multiplier, 144*screen_multiplier))
area_load()
(x) = 160*0.45
(y) = 144*0.45
def caught():
        if amount_caught == 0:
            pass
            #catch1 values are equal to wild one's

#Mainloop
crashed = False
while not crashed:

    x_change = 0
    y_change = 0

    #Different buttons
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
            pygame.quit()
        elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -3*screen_multiplier
                    player.rect.x += x_change
                    screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
                    hits = pygame.sprite.spritecollide(player, blockades, False)              
                    if hits:
                            print ('Collision!')
                            x_change = 0
                        player.rect.x = 10*screen_multiplier + blocks.rect.x
                    sprite = pygame.image.load('player_west_still.png')
                elif event.key == pygame.K_RIGHT:
                    x_change = 3*screen_multiplier
                    player.rect.x += x_change
                    screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
                    hits = pygame.sprite.spritecollide(player, blockades, False)              
                    if hits:
                            print ('Collision!')
                            x_change = 0
                            player.rect.x = blocks.rect.x - 10*screen_multiplier
                    sprite = pygame.image.load('player_east_still.png')
                elif event.key == pygame.K_UP:      
                    y_change = -3*screen_multiplier
                    player.rect.y += y_change
                    screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
                    hits = pygame.sprite.spritecollide(player, blockades, False)              
                    if hits:
                            print ('Collision!')
                            y_change = 0
                            player.rect.y = 14*screen_multiplier + blocks.rect.x
                    sprite = pygame.image.load('player_north_still.png')
                elif event.key == pygame.K_DOWN:
                    y_change = 3*screen_multiplier
                    player.rect.y += y_change
                    screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
                    hits = pygame.sprite.spritecollide(player, blockades, False)              
                    if hits:
                        print ('Collision!')
                            y_change = 0
                            player.rect.y = blocks.rect.x - 14*screen_multiplier
                    sprite = pygame.image.load('player_south_still.png')
        elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    x_change = 0
                elif event.key == pygame.K_RIGHT:
                    x_change = 0
                elif event.key == pygame.K_UP:
                    y_change = 0
                elif event.key == pygame.K_DOWN:
                    y_change = 0    
    x += x_change
    y += y_change
    player.rect.x += x_change
    player.rect.y += y_change

    blockades = pygame.sprite.Group()
    blockades.add(blocks)
    #Check for collisions
    hits = pygame.sprite.spritecollide(player, blockades, False)


    #Draw everything
    sprite = pygame.transform.scale(sprite, (10*screen_multiplier, 14*screen_multiplier))
    sprite.set_colorkey(WHITE)
    screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
    screen.blit(blocks.trees, (blocks.rect.x, blocks.rect.y))
    screen.blit(background,(0,0))
    screen.blit(sprite,(player.rect.x,player.rect.y))
    pygame.display.flip()
pygame.quit()    
  • In short, what should I do if I want separate lists of boundaries and blockades for each different town or route? Do I make a pygame.sprite.group and update it every time the town changes, killing everything, then adding the new stuff in? If so, do I make a separate class for each different wall or is there an easier way? – Would Rather Not Share My Name Mar 25 '17 at 10:26
  • `pygame.sprite.group` for each levels static sprites such as boundaries etc and another for enemies and maybe 1 for NPC's if you have one – Joshua Nixon Mar 25 '17 at 12:32
  • Thanks for the idea! To go about this, do I need to make a class for each individual block? If not, how do I add different blocks that are in a class to a group? – Would Rather Not Share My Name Mar 25 '17 at 12:37
  • You may have a class for a block and you could create that same block multiple times with different x, y etc and add them to the same boundary group – Joshua Nixon Mar 25 '17 at 12:39
  • `blocks = pygame.sprite.Group() ; B = Block(x,y) ; blocks.add(B)` – Joshua Nixon Mar 25 '17 at 12:40
  • Do I not need to edit my code on class blocks at all to allow this? – Would Rather Not Share My Name Mar 25 '17 at 14:25
  • How do I rewrite this to take this method into account? class Blocks(pygame.sprite.Sprite): def __init__(self): super(Blocks, self).__init__() self.trees = pygame.Surface((10*screen_multiplier, 14*screen_multiplier)) self.trees.fill(WHITE) self.rect = self.trees.get_rect() self.rect.x = 25 self.rect.y = 25 – Would Rather Not Share My Name Mar 25 '17 at 14:43

0 Answers0