Black BG during render under tiles, looks fine in Tiled map editor - python

I started learning Pygame and Tiled map editor. I have the following test:
Example
It looks fine in the editor, the objects that have the black BG are currently on layer_4 (but they do it regardless of layer count).
I already tried using convert_alpha on it, ticking and unticking the transparency color in Tiled when loading in the tile map.
This is the original picture:
Full picture
This is the part where I load in the picture:
def import_cut_graphics(path):
surface = pygame.image.load(path).convert_alpha()
tile_num_x = int(surface.get_size()[0] / TILESIZE)
tile_num_y = int(surface.get_size()[1] / TILESIZE)
cut_tiles = []
for row in range(tile_num_y):
for col in range(tile_num_x):
x = col * TILESIZE
y = row * TILESIZE
new_surface = pygame.Surface((TILESIZE, TILESIZE))
new_surface.blit(surface, (0, 0), pygame.Rect(x, y, TILESIZE, TILESIZE))
cut_tiles.append(new_surface)
return cut_tiles
And this is where I work with it:
class Level:
def __init__(self, level_data, surface):
# general setup
self.all_layers = import_cut_graphics("img\ProjectUtumno_full.png")
self.display_surface = surface
self.world_shift = 0
# terrain setup
layer1_layout = import_csv_layout(level_data["layer_1"])
self.layer1_sprites = self.create_tile_group(layer1_layout, 'layer_1')
# grass setup
layer2_layout = import_csv_layout(level_data["layer_2"])
self.layer2_sprites = self.create_tile_group(layer2_layout, "layer_2")
# crates
layer3_layout = import_csv_layout(level_data["layer_3"])
self.layer3_sprites = self.create_tile_group(layer3_layout, "layer_3")
# layer 4
layer4_layout = import_csv_layout(level_data["layer_4"])
self.layer4_sprites = self.create_tile_group(layer4_layout, "layer_4")
def create_tile_group(self, layout, type):
sprite_group = pygame.sprite.Group()
for row_index, row in enumerate(layout):
for col_index, val in enumerate(row):
if val != '-1':
x = col_index * TILESIZE
y = row_index * TILESIZE
if type == 'layer_1':
tile_surface = self.all_layers[int(val)]
sprite = StaticTile(TILESIZE, x, y, tile_surface)
sprite_group.add(sprite)
if type == "layer_2" :
tile_surface = self.all_layers[int(val)]
sprite = StaticTile(TILESIZE, x, y, tile_surface)
sprite_group.add(sprite)
if type == "layer_3" :
tile_surface = self.all_layers[int(val)]
sprite = StaticTile(TILESIZE, x, y, tile_surface)
sprite_group.add(sprite)
if type == "layer_4" :
tile_surface = self.all_layers[int(val)]
sprite = StaticTile(TILESIZE, x, y, tile_surface)
sprite_group.add(sprite)
return sprite_group
def run(self):
# run the entire game / level
self.camera()
# layer1
self.layer1_sprites.update(self.world_shift)
self.layer1_sprites.draw(self.display_surface)
# layer2
self.layer2_sprites.update(self.world_shift)
self.layer2_sprites.draw(self.display_surface)
# layer3
self.layer3_sprites.update(self.world_shift)
self.layer3_sprites.draw(self.display_surface)
# layer4
self.layer4_sprites.update(self.world_shift)
self.layer4_sprites.draw(self.display_surface)
This is my main game loop:
class Game:
def __init__(self):
pygame.init()
pygame.font.init()
self.screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
self.clock = pygame.time.Clock()
self.screen_name = pygame.display.set_caption("New Game")
self.running = True
self.playing = False
self.level = Level(level_0, self.screen)
self.character_spritesheet = Spritesheet('img\MainCharacter\B_witch_idle.png')
def main(self):
# game loop
while self.running:
self.events()
self.update()
self.draw()
self.running = False
def draw(self):
self.screen.fill(BLACK)
self.level.run()
self.all_sprites.draw(self.screen)
self.clock.tick(FPS)
pygame.display.update()
I did try commenting the screen fill black and got the same result. I tried out set_colorkey with white/black colors but didn't work out.
I add my tile create class too:
class Tile(pygame.sprite.Sprite):
def __init__(self, size, x, y):
super().__init__()
self.image = pygame.Surface((size, size))
self.rect = self.image.get_rect(topleft=(x, y))
def update(self, shift):
self.rect.x += shift
class StaticTile(Tile):
def __init__(self, size, x, y, surface):
super().__init__(size, x, y)
self.image = surface
Thanks in advance if you can figure out why i have this problem.

So for anyone who's having this problem, I managed to solve it by this:
Inside my import_cut_graphics methode I added a set_colorkey(BLACK), turns out the problem occured when I was originally cutting the picture, therefore it didnt matter how much I change it at the latter phases since the problem was in an earlier stage.
def import_cut_graphics(path):
surface = pygame.image.load(path).convert_alpha()
tile_num_x = int(surface.get_size()[0] / TILESIZE)
tile_num_y = int(surface.get_size()[1] / TILESIZE)
cut_tiles = []
for row in range(tile_num_y):
for col in range(tile_num_x):
x = col * TILESIZE
y = row * TILESIZE
new_surface = pygame.Surface((TILESIZE, TILESIZE))
new_surface.blit(surface, (0, 0), pygame.Rect(x, y, TILESIZE, TILESIZE))
new_surface.set_colorkey(BLACK)
cut_tiles.append(new_surface)
return cut_tiles

Related

Pygame sprite group won't draw because of error: Source objects must be a suface

im having a problem with my 2D Platformer game and when trying to display sprite group, an error message appears saying: Pygame: TypeError: Source objects must be a surface, The problem seems to be around the coins sprite group when trying to draw it to the self.display_surface in my code here.
I would highly appreciate any help.
Here is my code for my level.py:
import pygame
from support import import_csv_layout, import_cut_graphics
from settings import tile_size
from tiles import Tile, StaticTile, Crate, AnimatedTile
class Level:
def __init__(self, level_data, surface):
# general setup
self.display_surface = surface
self.world_shift = -5
# terrain setup
terrain_layout = import_csv_layout(level_data['terrain'])
self.terrain_sprites = self.create_tile_group(terrain_layout, 'terrain')
# grass setup
grass_layout = import_csv_layout(level_data['grass'])
self.grass_sprites = self.create_tile_group(grass_layout, 'grass')
# crates
crate_layout = import_csv_layout(level_data['crates'])
self.crate_sprites = self.create_tile_group(crate_layout, 'crates')
# coins
coins_layout = import_csv_layout(level_data['coins'])
self.coin_sprites = self.create_tile_group(coins_layout, 'coins')
def create_tile_group(self, layout, type):
sprite_group = pygame.sprite.Group()
for row_index, row in enumerate(layout):
for col_index, val in enumerate(row):
if val != '-1':
x = col_index * tile_size
y = row_index * tile_size
if type == 'terrain':
terrain_tile_list = import_cut_graphics('../gfx/terrain/terrain_tiles.png')
tile_surface = terrain_tile_list[int(val)]
sprite = StaticTile(tile_size, x, y, tile_surface)
if type == 'grass':
grass_tile_list = import_cut_graphics('../gfx/decoration/grass/grass.png')
tile_surface = grass_tile_list[int(val)]
sprite = StaticTile(tile_size, x, y, tile_surface)
if type == 'crates':
sprite = Crate(tile_size, x, y)
if type == 'coins':
sprite = AnimatedTile(tile_size, x, y, '../gfx/coins/gold')
sprite_group.add(sprite)
return sprite_group
def run(self):
# run the entire game / level
# terrain
self.terrain_sprites.update(self.world_shift)
self.terrain_sprites.draw(self.display_surface)
# grass
self.grass_sprites.update(self.world_shift)
self.grass_sprites.draw(self.display_surface)
# crate
self.crate_sprites.update(self.world_shift)
self.crate_sprites.draw(self.display_surface)
# coins
self.coin_sprites.update(self.world_shift)
self.coin_sprites.draw(self.display_surface)
And Here is the tile.py file: AnimatedTile and SaticTile class:
class AnimatedTile(Tile):
def __init__(self, size, x, y, path):
super().__init__(size, x, y)
self.frames = import_folder(path)
self.frame_index = 0
self.image = self.frames[self.frame_index]
class StaticTile(Tile):
def __init__(self, size, x, y, surface):
super().__init__(size, x, y)
self.image = surface
Also import folder has a surface variable, so i can't tell if its in here:
surface_list = []
for _,__, image_files in walk(path):
for image in image_files:
full_path = path + '/' + image
image_surf = pygame.image.load(full_path).convert_alpha()
surface_list.append(image)
return surface_list
Crate:
class Crate(StaticTile):
def __init__(self, size, x, y):
super().__init__(size, x, y, pygame.image.load('../gfx/terrain/crate.png').convert_alpha())
offset_y = y + size
self.rect = self.image.get_rect(bottomleft = (x,offset_y))
The bug is in the code that creates the surface_list. You have to append image_surf to the surface_list, but not image:
surface_list.append(image)
surface_list.append(image_surf)

Why does this bug happen when I click on two sprites at the same time?

I'm making a simple game using pygame where you keep clicking on tiles as fast as you can until you miss a tile. this is the progress I've made so far. sometimes when I click on a tile (usually when 2 tiles are next to each other and you click between them) one of them does what they're supposed to while the other just disappears from the screen.
import pygame
import random
import sys
#Setting up all possible Tile positions
grid = [[0,0], [0,150], [0,300], [0,450], [0,600],
[150,0],[150,150],[150,300],[150,450],[150,600],
[300,0],[300,150],[300,300],[300,450],[300,600],
[450,0],[450,150],[450,300],[450,450],[450,600],
[600,0],[600,150],[600,300],[600,450],[600,600]]
taken = []
#Classes
class Cursor(pygame.sprite.Sprite):
def __init__(self, pic):
super().__init__()
self.image = pygame.image.load(pic).convert_alpha()
self.image = pygame.transform.scale(self.image, (50,50))
self.rect = self.image.get_rect()
def destroyTile(self):
pygame.sprite.spritecollide(cursor, tileGroup, True)
def update(self):
self.rect.topleft = pygame.mouse.get_pos()
class Tiles(pygame.sprite.Sprite):
def __init__(self, tileSize, color, x, y):
super().__init__()
self.image = pygame.Surface(([tileSize, tileSize]))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.topleft = [x, y]
def drawTiles():
takenLen = len(taken)
while takenLen != 3:
m = random.randint(0,24)
x, y = grid[m]
if grid[m] not in taken:
blackTile = Tiles(150, black, x, y)
blackTile.add(tileGroup)
taken.append(grid[m])
takenLen += 1
def handleTiles():
mx, my = pygame.mouse.get_pos()
modx = mx % 150
mody = my % 150
x = mx - modx
y = my - mody
taken.remove([x, y])
drawTiles()
def drawRedTile():
mx, my = pygame.mouse.get_pos()
modx = mx % 150
mody = my % 150
x = mx - modx
y = my - mody
redTile = Tiles(150, red, x, y)
redTile.add(tileGroup)
#Colours
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
grey = (46, 46, 46)
#Initializing Pygame
pygame.init()
clock = pygame.time.Clock()
#Screen
screenWidth = 750
screenHeight = 900
screen = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("Tiles Game")
whiteSurface = pygame.Surface((750, 750))
whiteSurface.fill(white)
pygame.mouse.set_visible(False)
#Blue line
line = pygame.Surface((750, 10))
line.fill(blue)
#Groups
tileGroup = pygame.sprite.Group()
cursor = Cursor("cursor.png")
cursorGroup = pygame.sprite.Group()
cursorGroup.add(cursor)
score = 0
drawTiles()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
score += 1
print(score)
print(taken)
print(tileGroup)
cursor.destroyTile()
handleTiles()
#Background
screen.fill(grey)
screen.blit(whiteSurface, (0,0))
screen.blit(line, (0,750))
tileGroup.draw(screen)
cursorGroup.draw(screen)
cursorGroup.update()
pygame.display.update()
In the code I tried using print statements to see if the tile that seems to have disappeared is still there. When this happens, I assume that the tile is not in its group anymore since the number of sprites in the tile group went from 3 to 2. But the list showing all the taken positions still shows that there are 3 positions that are taken. I can still click on the tile if I just click on the space where there should be a tile and the tile comes back. I thought the game should exit when a tile isn't clicked on but it doesn't if there is an "invisible" tile in that position.
How do I make it so that this bug doesn't happen and every new tile made is visible?
The problem is that the cursor has an area and can hit more than one block at a time. So in destroyTile more than 1 block can be removed at once:
def destroyTile(self):
pygame.sprite.spritecollide(cursor, tileGroup, True)
However, the function handleTiles cannot handle this, because it can only remove one block position from the taken list. I suggest to simplify the code and recreate the taken list completely from tileGroup when blocks are removed:
def handleTiles():
taken.clear()
for tile in tileGroup:
x, y = tile.rect.topleft
taken.append([x, y])
drawTiles()

How do I make my player image bigger on collision while keeping its proportions?

I am making a game in pygame where you now swim around and eat small squares, with an animation to the jellyfish. I've made it so you get bigger when eating, but when adding a number between 1-9 to the scale the image sort of gets wider than I want it to become. When I have the scale go up by 10 or more when eating this problem does not occur as badly.
This is the code for the jellyfish/player:
import pygame, math, time
pt = time.time()
speed = 40
pygame.display.set_mode((800, 500))
img0 = pygame.transform.scale(pygame.image.load("assets/glow0.png"), (30,30)).convert_alpha()
img1 = pygame.transform.scale(pygame.image.load("assets/glow1.png"), (30,30)).convert_alpha()
img2 = pygame.transform.scale(pygame.image.load("assets/glow2.png"), (30,30)).convert_alpha()
img3 = pygame.transform.scale(pygame.image.load("assets/glow3.png"), (30,30)).convert_alpha()
img4 = pygame.transform.scale(pygame.image.load("assets/glow4.png"), (30,30)).convert_alpha()
img5 = pygame.transform.scale(pygame.image.load("assets/glow5.png"), (30,30)).convert_alpha()
img6 = pygame.transform.scale(pygame.image.load("assets/glow6.png"), (30,30)).convert_alpha()
img7 = pygame.transform.scale(pygame.image.load("assets/glow7.png"), (30,30)).convert_alpha()
img8 = pygame.transform.scale(pygame.image.load("assets/glow8.png"), (30,30)).convert_alpha()
class Glow():
rot = 0
rotp = 1
xsp = 0
ysp = 0
def __init__(self, x, y, scale):
self.x, self.y = x, y
self.scale = scale
self.list = [img0, img1, img2, img3, img4, img5, img6, img7, img8]
self.current = 0
self.image = self.list[int(self.current)]
self.rect = self.image.get_rect(center = (x, y))
self.colRect = pygame.rect.Rect((0, 0), (self.rect.width/3, self.rect.height/3))
self.colRect.center = self.rect.center
def update(self, x, y, accex):
global pt, speed
now = time.time()
dt = now - pt
pt = now
self.rect = self.image.get_rect(center = (x, y))
if pygame.key.get_pressed()[pygame.K_UP] or accex:
# animation
self.current += dt*5
if self.current >= len(self.list):
self.current = 0
self.image = pygame.transform.rotate(self.list[int(self.current)], self.rot)
self.rect = self.image.get_rect(center = (x, y))
self.colRect.center = self.rect.center
# go in direction of rotation
self.rotr = math.radians(self.rot)
self.ysp = math.cos(self.rotr)
self.xsp = math.sin(self.rotr)
self.x -= self.xsp*dt*speed
self.y -= self.ysp*dt*speed
if not accex:
if pygame.key.get_pressed()[pygame.K_LEFT]:
self.rot += math.pi*dt*7
if pygame.key.get_pressed()[pygame.K_RIGHT]:
self.rot -= math.pi*dt*7
if accex:
speed += dt*10
def scaleup(self):
self.scale += 2
i = 0
for img in self.list:
self.list.remove(img)
img = pygame.transform.scale(img, (self.scale, self.scale))
self.list.insert(i, img)
i += 1
This is the main code in the game loop that has to do with this:
W, H = 800, 500
sc = pygame.display.set_mode((W, H))
score = 0
score_x, score_y = 10, 10
font = pygame.font.SysFont("calibri", 30)
score_text = font.render("0", True, (255,255,255))
score _rect = score_text.get_rect(topleft = (score_x, score_y))
if lvl0:
glow.update(glow.x, glow.y, accex)
sc.fill((0,0,0))
for food in Food.food_list:
sc.blit(food.image, (food.x, food.y))
if pygame.Rect.colliderect(glow.colRect, food.rect):
Food.food_list.remove(food)
glow.scaleup()
score += 3
score_text = font.render(str(score), True, (255,255,255))
score_rect = score_text.get_rect(topleft = (score_x, score_y))
sc.blit(glow.image, glow.rect)
sc.blit(label, label_rect)
pygame.display.update()
Pygame behaves weird when using the transformed image repetitively as in case of rotation...
I even have faced crashes due to it
So try using the the same image which is initially loaded as img0,img1,etc. and scale it to the desired size. As of now you were using the same scaled image again and again .
This might help
I know there is an already accepted answer, but here is how I do it:
class Wabbit:
# this store the main, unaltered image loaded from disk
_main_image = None
def __init__(self, pos, scale=1):
# if the class._main_image variable is not set
# we do this so we only have to load the image once
# when the first instance is created
if not self._main_image:
# load the image from disk
self._main_image = pygame.image.load('wabbit_alpha.png')
self.pos = Vector2(pos)
self.vel = Vector2()
self.scale = scale
# some variables to store the "previous" values
self._scale = None
self._image = None
#property
def size(self):
# returns the size of our image, as a Vector2
return Vector2(self.image.get_size())
#property
def image(self):
# this is where we look at our current scale
# compare it to our previous _scale
# and update our _image if the two arent the same
if self.scale != self._scale:
# update the previous _scale value
self._scale = self.scale
# get the size of the original image and scale it
# according to our scale value
size = Vector2(self._main_image.get_size()).elementwise() * self.scale
# set our _image to be the scaled version of the main, original image
self._image = pygame.transform.scale(self._main_image, size)
# return our scaled image
return self._image
def update(self, dt):
self.pos += self.vel * dt
def draw(self, surface):
surface.blit(self.image, self.pos-self.size/2)

Animated objects launch at a precise time with pytmx, pygame

I am in troubles, I am trying to make doors opening in my game.
I am using pygame, and pytmx, I have built a Level made with Rooms, and in each Room I have a Renderer using pytmx, what I want to achieve is for exemple on level 0 the player has to move to a door to open it and enter level 1, my goal is to launch and draw the animation when player hit the door.
I tried to make my Door object (his parent is pygame Sprite class) update, it has kind of worked but of course the object was modified and displayed, but the tiled map object also and was staying in the back of the map, so I tried to just not initialize my objects in the tile map but if so the door doesn't bit at all. Then I tried to modify the tile object image and to reload my rendering but still doesn't work, does anyone of you has any idea ?
Here is some parts of the code to let you have better understanding
"""
This is a test of using the pytmx library with Tiled.
"""
import pygame
import pytmx
class Renderer(object):
"""
This object renders tile maps from Tiled
"""
def __init__(self, filename):
tm = pytmx.load_pygame(filename, pixelalpha=True)
self.object_images = []
self.size = tm.width * tm.tilewidth, tm.height * tm.tileheight
self.tmx_data = tm
self.map_surface = self.make_map()
self.current_frame = 0
def render(self, surface):
tw = self.tmx_data.tilewidth
th = self.tmx_data.tileheight
print(self.tmx_data.tile_properties.items())
if self.tmx_data.background_color:
surface.fill(self.tmx_data.background_color)
for layer in self.tmx_data.layers:
if isinstance(layer, pytmx.TiledTileLayer):
for x, y, image in layer.tiles():
if image:
surface.blit(image.convert_alpha() , (x * tw, y * th))
elif isinstance(layer, pytmx.TiledObjectGroup):
for object in layer:
if object.image:
surface.blit(object.image.convert_alpha() , (object.x, object.y))
elif isinstance(layer, pytmx.TiledImageLayer):
if image:
surface.blit(image , (0, 0))
def make_map(self):
temp_surface = pygame.Surface(self.size)
self.render(temp_surface)
return temp_surface
def reload(self):
self.map_surface = self.make_map()
def update_object_image(self, object_id, surface):
pass
def get_layer(self, layer_name):
return self.tmx_data.get_layer_by_name(layer_name)
class Room:
def __init__(self, room_image, level_options):
self.renderer = Renderer(room_image)
self.surface = self.renderer.map_surface
self.rect = self.surface.get_rect()
self.level_options = level_options
self.obstacles = pygame.sprite.Group()
self.doors = pygame.sprite.Group()
self.load_obstacles()
self.load_doors()
def load_obstacles(self):
obstacles = self.level_options['obstacle_layers']
for obstacle in obstacles:
try:
items = self.renderer.get_layer(obstacle)
for x, y, image in items.tiles():
self.obstacles.add(Obstacle(x, y, image, SPRITE_SIZE))
except ValueError:
return
def load_doors(self):
doors = self.renderer.get_layer(self.level_options['door_layer'])
if isinstance(doors, pytmx.TiledTileLayer):
for x, y, image in doors.tiles():
if image:
self.doors.add(Door(self, x, y, image, SPRITE_SIZE, self.level_options['open_door_actions']))
elif isinstance(doors, pytmx.TiledObjectGroup):
for object in doors:
self.doors.add(Door(self, object.x, object.y, object.image, SPRITE_SIZE, self.level_options['open_door_actions'], object))
def update_doors(self):
for door in self.doors:
if door.object:
door.update()
#self.renderer.update_object_image(door.object.id, door.image)
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, image, sprite_size):
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(x * sprite_size,y * sprite_size, sprite_size, sprite_size)
self.image = image
class Obstacle(pygame.sprite.Sprite):
def __init__(self, x, y, image, sprite_size):
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(x * sprite_size,y * sprite_size, sprite_size, sprite_size)
self.image = image
class Door(pygame.sprite.Sprite):
def __init__(self, parent, x, y, image, sprite_size, open_actions, object = None):
self.parent = parent
pygame.sprite.Sprite.__init__(self)
self.object = object
if self.object:
self.rect = pygame.Rect(x ,y, self.object.width, self.object.height)
self.current_index = 0
self.load_animations()
else:
self.rect = pygame.Rect(x * sprite_size,y * sprite_size, sprite_size, sprite_size)
self.image = image
self.open_actions = open_actions
self.last_updated = 0
self.is_open = False
self.is_activated = False
def load_animations(self):
self.animations = []
filename = os.path.join(os.getcwd(), IMAGES_FOLDER, 'maps', 'animations', self.object.animated_image)
spritesheet = Spritesheet(filename, self.object.width, self.object.height)
for i in range(spritesheet.data['columns']):
self.animations.append(spritesheet.parse_sprite(i))
self.image = self.animations[self.current_index]
def open_animation(self):
if self.is_open:
return
if not self.object or not self.is_activated or not self.animations:
return
self.image = self.animations[self.current_index]
if self.current_index == len(self.animations) - 1:
self.is_open = True
def update(self):
if self.is_open:
if self.object:
self.image = self.animations[-1]
if self.is_activated:
if self.object:
now = pygame.time.get_ticks()
if now - self.last_updated > 200:
self.last_updated = now
self.current_index = (self.current_index + 1) % len(self.animations)
self.image = self.animations[self.current_index]
if not self.is_open:
self.image = self.animations[0]
def check_doors_state(self, player):
if 'any' in self.open_actions:
self.is_activated = True
for action in player.actions:
if action in self.open_actions:
self.is_activated = True
So right now what I am trying to do is to use an animated tile object and launch the animation from tmx data, but I can't even understand how to launch the animation at the first rendering.
Thank you in advance for you answers.
So I found a solution, I am not trying to change and reload the object tiles, I simply made my game sprites in groups, modified my generating and rendering of tiles.
Classes modified :
class Room:
def __init__(self, level, room_image, level_options):
self.level = level
self.renderer = Renderer(room_image)
self.surface = self.renderer.map_surface
self.tmx_data = self.renderer.tmx_data
self.rect = self.surface.get_rect()
self.level_options = level_options
self.walls = pygame.sprite.Group()
self.doors = pygame.sprite.Group()
#self.load_walls()
#self.load_doors()
def load_walls(self):
walls = self.renderer.get_layer(self.level_options['wall_layer'])
if isinstance(walls, pytmx.TiledObjectGroup):
for object in walls:
self.walls.add(Wall(object.x, object.y, object, SPRITE_SIZE))
def load_doors(self):
doors = self.renderer.get_layer(self.level_options['door_layer'])
if isinstance(doors, pytmx.TiledTileLayer):
for x, y, image in doors.tiles():
if image:
self.doors.add(Door(self, x, y, image, SPRITE_SIZE, self.level_options['open_door_actions']))
elif isinstance(doors, pytmx.TiledObjectGroup):
for object in doors:
self.doors.add(Door(self, object.x, object.y, object.image, SPRITE_SIZE, self.level_options['open_door_actions'], object))
def update(self):
self.doors.update()
def draw(self, display, camera = None):
self.doors.draw(self.surface)
if not camera:
display.blit(self.surface, self.rect)
else:
display.blit(self.surface, (self.rect.x - camera.offset.x, self.rect.y - camera.offset.y))
def get_opening_actions(self):
return self.level_options['open_door_actions']
class Wall(pygame.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.walls
pygame.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.wall_img
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = x * SPRITE_SIZE
self.rect.y = y * SPRITE_SIZE
class Obstacle(pygame.sprite.Sprite):
def __init__(self, game, x, y, w, h):
self.groups = game.walls
pygame.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.rect = pygame.Rect(x, y, w, h)
self.hit_rect = self.rect
self.x = x
self.y = y
self.rect.x = x
self.rect.y = y
class Door(pygame.sprite.Sprite):
def __init__(self, game, x, y, w, h, open_actions, animated_image):
self.groups = game.all_sprites, game.doors
pygame.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.rect = pygame.Rect(x ,y, w, h)
self.current_index = 0
self.animated_image = animated_image
self.load_animations()
self.open_actions = open_actions
self.last_updated = 0
self.is_open = False
self.is_activated = False
def load_animations(self):
self.animations = []
filename = os.path.join(os.getcwd(), IMAGES_FOLDER, 'maps', 'animations', self.animated_image)
spritesheet = Spritesheet(filename, self.rect.width, self.rect.height)
for i in range(spritesheet.data['columns']):
self.animations.append(spritesheet.parse_sprite(i))
self.image = self.animations[self.current_index]
def open_animation(self):
if self.is_open:
return
if not self.is_activated or not self.animations:
return
now = pygame.time.get_ticks()
if now - self.last_updated > 15:
self.last_updated = now
self.current_index = (self.current_index + 1) % len(self.animations)
self.image = self.animations[self.current_index]
if self.current_index == len(self.animations) - 1:
self.current_index = -1
self.is_open = True
def update(self):
if not self.is_open:
self.image = self.animations[0]
if self.is_open:
self.image = self.animations[-1]
def check_doors_state(self, player):
if 'any' in self.open_actions:
self.is_activated = True
for action in player.actions:
if action in self.open_actions:
self.is_activated = True
Sprites groups generating, updating and rendering added (in my game class) :
def new(self, level = 0):
# initialize all variables and do all the setup for a new game
self.all_sprites = pygame.sprite.Group()
self.walls = pygame.sprite.Group()
self.doors = pygame.sprite.Group()
self.mobs = pygame.sprite.Group()
## LOAD LEVEL
self.level = Level(level)
self.room = self.level.current_room
for tile_object in self.room.tmx_data.objects:
if tile_object.name == 'player':
self.player = Player(self, tile_object.x, tile_object.y)
if tile_object.name == 'obstacle':
Obstacle(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height)
if tile_object.name == 'wall':
Wall(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height)
if tile_object.name == 'door':
Door(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height, self.room.get_opening_actions(), tile_object.animated_image)
def update(self):
self.all_sprites.update()
def draw(self):
self.window.fill((0, 0, 0))
## DISPLAY MAP
self.level.draw_room(self.window, self.camera)
## DISPLAY SPRITES
for sprite in self.all_sprites:
self.window.blit(sprite.image, (sprite.rect.x - self.camera.offset.x, sprite.rect.y - self.camera.offset.y))
## REFRESH SCREEN
pygame.display.flip()
And here is the result (I am working on better hitbox for the player ^^)
door opening when player getting close to it

Pygame draw.rect 2d object array black screen

I have a problem with a program. I'm trying to code a battleship game and the problem is over here:
class Battleship(object):
def __init__(self):
pygame.init()
pygame.display.set_caption('battleship')
self.gameDisplay = pygame.display.set_mode((DISPLAYWIDTH, DISPLAYHEIGHT))
self.clock = pygame.time.Clock()
class Tile(object):
def __init__(self):
self.occupied = False
self.shipID = 0
self.playerID = 0
def setOccupied(self, occupied):
self.occupied = occupied
def setShipID(self, shipID):
self.shipID = shipID
def setPlayerID(self, pID):
self.playerID = pID
class Board:
shipSizes = [1, 2, 3]
sizeX = 25
sizeY = 25
def __init__(self, playerID):
self.playerID = playerID
self.playerShips = []
for i in range(0, len(self.shipSizes)):
self.playerShips.append(Ship(i, self.shipSizes[i]))
self.tiles = [[Tile() for i in range(self.sizeX)] for j in range(self.sizeY)]
def drawBoard(self):
x = 0
y = 0
for row in self.visual:
for col in row:
pygame.draw.rect(Battleship().gameDisplay, black, (x, y, CASILLA, CASILLA), LINEWIDTH)
x = x + CASILLA
y = y + CASILLA
x=0
I don't get any errors, but the function does not work properly and only shows a black screen, checked everything else and the problem definitely lies in that part.
Edit, i added the clas Battleship
I've written this minimal example to show you what I described in the comments. The board instance is an attribute of the Battleship now and you can just call its draw method in the draw method of the battleship.
import pygame as pg
GRAY = pg.Color('gray20')
BLUE = pg.Color('dodgerblue1')
class Battleship(object):
def __init__(self):
pg.init()
pg.display.set_caption('battleship')
self.display = pg.display.set_mode((800, 600))
self.clock = pg.time.Clock()
self.fps = 30
self.done = False
# Is an attribute of the Battleship now.
self.board = Board()
def run(self):
while not self.done:
self.handle_events()
self.run_logic()
self.draw()
self.clock.tick(self.fps)
def handle_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
def run_logic(self):
pass
def draw(self):
self.display.fill(GRAY)
# Call the board's draw method and pass the display.
self.board.draw(self.display)
pg.display.flip()
class Board:
shipSizes = [1, 2, 3]
sizeX = 25
sizeY = 25
def __init__(self):
self.tiles = [['-' for i in range(self.sizeX)] for j in range(self.sizeY)]
def draw(self, display):
for y, row in enumerate(self.tiles):
for x, col in enumerate(row):
pg.draw.rect(
display, BLUE,
(x*self.sizeX, y*self.sizeY, self.sizeX, self.sizeY),
2)
battleship = Battleship()
battleship.run()

Categories