Estoy intentando hacer un juego en 2d, con un mundo generado a partir de una imágen en pixeles de diferentes colores. Alternativamente, será terreno normal, agua o los límites, lo prohibido. Dado que necesito que sea un mundo muy ámplio, debo distribuirlo en chunks y hacer que estos se carguen y descarguen al estar a cierta distancia de el chunk en el que está el jugador con su avatar. De momento el avatar es un quadrado, dado que solo estoy probando la generación de mundo y la carga-descarga de chunks. En teoria no hay nada de malo, sin embargo no funciona. La pantalla simplemente se queda negra.
Alguna idea? Por favor, llevo días con esto.
from PIL import Image
import pygame
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((1000, 750))
all_chunks = pygame.sprite.Group()
visible_chunks = pygame.sprite.Group()
players = pygame.sprite.Group()
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.pos_x = 0
self.pos_y = 0
self.m_r = False
self.m_l = False
self.m_u = False
self.m_d = False
self.speed = 3
self.scrolling_x = 0
self.scrolling_y = 0
self.image = pygame.Surface((10, 10))
self.image.fill((255,100,255))
self.rect = pygame.Rect(self.pos_x, self.pos_y, 10, 10)
def update(self):
if self.m_r == True:
self.scrolling_x = self.speed
if self.m_l == True:
self.scrolling_x = (0 - self.speed)
if self.m_u == True:
self.scrolling_y = self.speed
if self.m_d == True:
self.scrolling_y = (0 - self.speed)
if self.m_r == False:
self.scrolling_x = 0
if self.m_l == False:
self.scrolling_x = 0
if self.m_u == False:
self.scrolling_y = 0
if self.m_d == True:
self.scrolling_y = 0
player = Player()
players.add(player)
class Controller():
def __init__(self):
self.t = []
self.image_map_file = "image_map_file.png"
self.t_file = None
self.t_file = None
self.image_size = None
self.current_color = None
self.opened_image = Image.open(self.image_map_file)
self.loaded = self.opened_image.load()
self.image_size = self.opened_image.size
self.width = 30
self.height = 30
self.chunk_place_x = 0
self.chunk_place_y = 0
self.t_file = None
self.current_pixel = 0
def create_t(self):
for i in range(self.image_size[0]):
self.t.append(["-"]*self.image_size[1])
control = Controller()
control.create_t()
class chunk(pygame.sprite.Sprite):
def __init__(self, biome, pos_x, pos_y, *groups):
super().__init__(*groups)
self.biome = biome
self.pos_x = pos_x
self.pos_y = pos_y
self.is_loaded = False
@classmethod
def calificate(cls):
for y in range(control.image_size[1]):
for x in range(control.image_size[0]):
control.current_pixel = control.loaded[x,y] # Get the RGBA Value of the a pixel of an image
if control.current_pixel[0] == 255: #Si és vemrell....
control.t[x][y] = 0
elif control.current_pixel[1] == 255:
control.t[x][y] = 1
elif control.current_pixel[2] == 255:
control.t[x][y] = 2
x += 1
y +=1
def load(self):
if self.biome == "nice_desert":
self.image = pygame.Surface((control.width, control.height))
self.image.fill((255,184,77))
self.rect = pygame.Rect(self.pos_x, self.pos_y, control.width, control.height)
if self.biome == "water":
self.image = pygame.Surface((control.width, control.height))
self.image.fill((0,0,255))
self.rect = pygame.Rect(self.pos_x, self.pos_y, control.width, control.height)
if self.biome == "forbidden":
self.image = pygame.Surface((control.width, control.height))
self.image.fill((255,0,0))
self.rect = pygame.Rect(self.pos_x, self.pos_y, control.width, control.height)
visible_chunks.add(self)
self.is_loaded = True
def unload(self):
self.is_loaded = False
try:
del self.image
except:
pass
try:
del self.rect
except:
pass
def find_itself(self):
self.comprehension = []
self.result = [0,0]
for i in range(control.image_size[0]):
self.comprehension = control.t[i]
try:
self.result[1] = self.comprehension.index(self)
self.result[0] = i
return self.result
except ValueError:
pass
def update(self, scrolling_x, scrolling_y):
try:
if pygame.sprite.spritecollideany(self, players):
now = self.find_itself()
for i in range(10):
control.t[now[0]-5+i][now[1]+5].load
control.t[now[0]+5][now[1]+5-i].load
control.t[now[0]+5-i][now[1]-5].load
control.t[now[0]-5][now[1]-5+i].load
except AttributeError:
pass
self.pos_x += scrolling_x
self.pos_y += scrolling_y
if self.is_loaded == True:
self.rect.x = self.pos_x
self.rect.y = self.pos_y
#Linia 100,, UUUUUUUU
#Això ho ha escrit el Hansel! :D
#ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
def paint():
for y in range(control.image_size[1]):
for x in range(control.image_size[0]):
if control.t[x][y] == 0:
control.t[x][y] = chunk("forbidden", control.chunk_place_x, control.chunk_place_y, all_chunks)
if control.t[x][y] == 1:
control.t[x][y] = chunk("nice_desert", control.chunk_place_x, control.chunk_place_y, all_chunks)
if control.t[x][y] == 2:
control.t[x][y] = chunk("water", control.chunk_place_x, control.chunk_place_y, all_chunks)
control.chunk_place_x += control.width
control.chunk_place_x = 0
control.chunk_place_y += control.height
chunk.calificate()
paint()
print(control.t[0][0])
control.t[0][0].load()
def main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
player.m_r = True
if event.key == K_LEFT:
player.m_l = True
if event.key == K_UP:
player.m_u = True
if event.key == K_DOWN:
player.m_d = True
if event.type == KEYUP:
if event.key == K_RIGHT:
player.m_r = False
if event.key == K_LEFT:
player.m_l = False
if event.key == K_UP:
player.m_u = False
if event.key == K_DOWN:
player.m_d = False
screen.fill((0,0,0))
#print(len(sprites.sprites))
players.draw(screen)
players.update()
visible_chunks.draw(screen)
all_chunks.update(player.scrolling_x, player.scrolling_y)
pygame.display.flip()
main()