def resize(self,tilesize , typ=None): if typ == None: for name in self.tile_org: self.tile_resize[name] = utils.imagetransformscale(self.tile_org[name] , tilesize) elif typ == "big": for name in self.tile_orgbig: self.tile_resizebig[name] = utils.imagetransformscale(self.tile_orgbig[name] , tilesize)
def loadtypes(tilesize): types = {} imgs_org = {} imgs = {} img_ani_pos = {} img_ani_speed = {} img_ani_tiles = {} img_ani_tile_pos = {} Config = configparser.ConfigParser() Config.read(os.path.join(main_dir, "basic.ini")) sections = Config.sections() modname = Config.get("info", "name") path = Config.get("info", "path") mouse = Config.get("info", "mouse") imgs_org["path"] = utils.loadImage(os.path.join(main_dir, path)) imgs["path"] = utils.imagetransformscale(imgs_org["path"], tilesize) imgs_org["mouse"] = utils.loadImage(os.path.join(main_dir, mouse), colorkey="-1") imgs["mouse"] = utils.imagetransformscale(imgs_org["mouse"], tilesize) types["mouse"] = "" types["path"] = "" img_ani_speed["mouse"] = 0 img_ani_tiles["mouse"] = 0 img_ani_speed["path"] = 0 img_ani_tiles["path"] = 0 for line in sections: if line != "info": imgpath = os.path.join(main_dir, Config.get(line, "img")) types[modname + "_" + line] = float(Config.get(line, "path", fallback=1)) tilesizeimg = int(Config.get(line, "tilesize")) colorkey = Config.get(line, "colorkey", fallback=None) img_ani_speed[modname + "_" + line] = int(Config.get(line, "speed", fallback=0)) img_ani_tiles[modname + "_" + line] = int(Config.get(line, "tiles", fallback=1)) imgs_org[modname + "_" + line] = utils.loadImage( imgpath, img_ani_tiles[modname + "_" + line], tilesizeimg, colorkey=colorkey ) print(imgs_org[modname + "_" + line]) imgs[modname + "_" + line] = utils.imagetransformscale(imgs_org[modname + "_" + line], tilesize) return types, imgs, imgs_org, img_ani_speed, img_ani_tiles
def main(): displaysize = 500, 500 mapsize = 0, 0, 500, 400 x = 0 y = 0 xx = 100 yy = 100 zoomx = 0 zoomy = 0 zoomxx = 100 zoomyy = 100 tilesize = 20 pygame.init() screen = pygame.display.set_mode(displaysize) # Titel des Fensters setzen, Mauszeiger nicht verstecken und Tastendrücke wiederholt senden. pygame.display.set_caption("Loading: Map") mapscreen = screen.subsurface((mapsize)) map_background_screen = mapscreen.subsurface((mapsize)) menuscreen = screen.subsurface((0, 400, 500, 100)) pygame.mouse.set_visible(1) pygame.key.set_repeat(1, 1) loadtype = loadtypes(tilesize) types = loadtype[0] imgs = loadtype[1] imgs_org = loadtype[2] img_ani_speed = loadtype[3] img_ani_tiles = loadtype[4] matrixtype = [[1 for x in range(xx)] for y in range(yy)] matrixschritt = numpy.empty((xx, yy), dtype=numpy.float) matrixgeprueft = numpy.empty((xx, yy), dtype=numpy.int) matrix_tile_pos = numpy.zeros((xx, yy), dtype=numpy.int) matrix_pos = numpy.zeros((xx, yy), dtype=numpy.int) # matrixtype = numpy.array([[0 for x in range(xx+1)] for y in range(yy+1)] , int) # matrixschritt = numpy.array([[0 for x in range(xx+1)] for y in range(yy+1)] , float) # matrixgeprueft = numpy.array([[0 for x in range(xx+1)] for y in range(yy+1)] , int) file = open(os.path.join(main_dir, "100X100.txt"), "r") y = 0 x = 0 for line in file: line = line.rstrip() # print(line) split = line.split(",") # print (split) x = 0 for string in split: # print ("x:" , x, " y:", y , " line:" , line) if string != "": matrixtype[x][y] = string if img_ani_speed[matrixtype[x][y]] != 0: matrix_pos[x][y] = random.randrange(0, img_ani_speed[matrixtype[x][y]]) x += 1 y += 1 file.close() # print(matrixtype) mp.set_start_method("spawn") func_holen = mp.Queue() func_geben = mp.Queue() p = mp.Process(target=Dijkstra.algo, args=(func_holen, func_geben, types, matrixschritt, matrixgeprueft)) p.start() startx = 1 starty = 1 zielx = 1 ziely = 1 path = -2 # Clock-Objekt erstellen, das wir benötigen, um die Framerate zu begrenzen. clock = pygame.time.Clock() pygame.display.set_caption("Loading:Paint Map") myfont = pygame.font.SysFont("timesnewroman", 30) # Die Schleife, und damit unser Spiel, läuft solange running == True. running = True zoomx = int(zoomx / tilesize) zoomy = int(zoomy / tilesize) zoomxx = int(mapsize[2] / tilesize) + 2 zoomyy = int(mapsize[3] / tilesize) + 2 for x in range(zoomx, zoomxx): for y in range(zoomy, zoomyy): map_background_screen.blit( imgs[matrixtype[x][y]][matrix_tile_pos[x][y]], ((x - zoomx) * tilesize, (y - zoomy) * tilesize) ) map_background_screen = pygame.Surface.copy(map_background_screen) mapscreen.blit(map_background_screen, (0, 0)) print("fertig") pygame.display.set_caption("Starts") while running: # Framerate auf 30 Frames pro Sekunde beschränken. # Pygame wartet, falls das Programm schneller läuft. clock.tick(30) # screen-Surface mit Schwarz (RGB = 0, 0, 0) füllen. screen.fill((0, 0, 0)) mousepos = pygame.mouse.get_pos() mouseposx = str(mousepos[0]) mouseposy = str(mousepos[1]) if mouseposx[-1] >= "5": mouseposx = int(int(mouseposx[:-1] + "5") / tilesize) elif mouseposx[-1] < "5": mouseposx = int(int(mouseposx[:-1] + "0") / tilesize) if mouseposy[-1] >= "5": mouseposy = int(int(mouseposy[:-1] + "5") / tilesize) elif mouseposy[-1] < "5": mouseposy = int(int(mouseposy[:-1] + "0") / tilesize) # menu -------------------------------------------- label = myfont.render("x:" + str(mouseposx + zoomx) + " y:" + str(mouseposy + zoomy), 1, (255, 255, 0)) labelfps = myfont.render("fps:" + str(clock.get_fps()), 1, (255, 255, 0)) menuscreen.blit(label, (0, 0)) menuscreen.blit(labelfps, (0, 30)) # menu end------------------------------- # pygame.surfarray.blit_array(mapscreen , map_background_screen_array) for x in range(zoomx, zoomxx): for y in range(zoomy, zoomyy): if img_ani_speed[matrixtype[x][y]] != 0: matrix_pos[x][y] += 1 if img_ani_speed[matrixtype[x][y]] == matrix_pos[x][y]: matrix_pos[x][y] = 0 matrix_tile_pos[x][y] += 1 if matrix_tile_pos[x][y] == img_ani_tiles[matrixtype[x][y]]: matrix_tile_pos[x][y] = 0 map_background_screen.blit( imgs[matrixtype[x][y]][matrix_tile_pos[x][y]], ((x - zoomx) * tilesize, (y - zoomy) * tilesize) ) mapscreen.blit(map_background_screen, (0, 0)) if func_holen.empty() == False: path = func_holen.get() if path != -1 and path != -2: for wege in path: string = wege.split(",") x = int(string[0]) y = int(string[1]) mapscreen.blit(imgs["path"][0], ((x - zoomx) * tilesize, (y - zoomy) * tilesize)) mapscreen.blit(imgs["mouse"][0], (mouseposx * tilesize, mouseposy * tilesize)) # Alle aufgelaufenen Events holen und abarbeiten. for event in pygame.event.get(): # Spiel beenden, wenn wir ein QUIT-Event finden. if event.type == pygame.QUIT: running = False # Wir interessieren uns auch für "Taste gedrückt"-Events. if event.type == pygame.KEYDOWN: # Wenn Escape gedrückt wird, posten wir ein QUIT-Event in Pygames Event-Warteschlange. if event.key == pygame.K_ESCAPE: pygame.event.post(pygame.event.Event(pygame.QUIT)) if event.key == pygame.K_KP_PLUS: print("plus") if tilesize <= 40: tilesize += 2 zoomx = int(zoomx / tilesize) zoomy = int(zoomy / tilesize) zoomxx = int(mapsize[2] / tilesize) + 1 zoomyy = int(mapsize[3] / tilesize) + 1 for img in types: imgs[img] = utils.imagetransformscale(imgs_org[img], tilesize) if event.key == pygame.K_KP_MINUS: print("minus") print(tilesize) if tilesize >= 20: tilesize -= 2 zoomx = int(zoomx / tilesize) zoomy = int(zoomy / tilesize) zoomxx = int(mapsize[2] / tilesize) + 1 zoomyy = int(mapsize[3] / tilesize) + 1 if zoomyy > yy: zoomyy = yy if zoomxx > xx: zoomxx = xx for img in types: imgs[img] = utils.imagetransformscale(imgs_org[img], tilesize) if event.key == pygame.K_UP: print("UP") if zoomy != 0: zoomy -= 1 zoomyy -= 1 if event.key == pygame.K_DOWN: print("DOWN") if zoomyy < yy: zoomy += 1 zoomyy += 1 if event.key == pygame.K_LEFT: print("LEFT") if zoomx != 0: zoomx -= 1 zoomxx -= 1 if event.key == pygame.K_RIGHT: print("RIGHT") if zoomxx < xx: zoomx += 1 zoomxx += 1 map_background_screen.fill((0, 0, 0)) for x in range(zoomx, zoomxx): for y in range(zoomy, zoomyy): # print("x:" , x , "y:",y) map_background_screen.blit( imgs[matrixtype[x][y]][matrix_tile_pos[x][y]], ((x - zoomx) * tilesize, (y - zoomy) * tilesize), ) map_background_screen = pygame.Surface.copy(map_background_screen) if event.type == pygame.MOUSEBUTTONDOWN: mouse = pygame.mouse.get_pressed() if mouse[0] == 1: startx = mouseposx + zoomx starty = mouseposy + zoomy func_geben.put([matrixtype, startx, starty, zielx, ziely]) elif mouse[2] == 1: zielx = mouseposx + zoomx ziely = mouseposy + zoomy func_geben.put([matrixtype, startx, starty, zielx, ziely]) pygame.display.flip()