def __init__(self): pygame.init() self.surface = pygame.display.set_mode( (world.SCREENWIDTH, world.SCREENHEIGHT)) pygame.display.set_caption(TITLE) self.surface.fill(world.UGLY_PINK) self.world_map = Map(MAPFILE, self)
def add_resources(the_map: world.Map, food_threshold=0.85, money_threshold=0.15): for x, y in itertools.product(range(the_map.width), range(the_map.height)): if the_map.get(x, y).is_wall: the_noise = mn.simplex_noise_2d(x, y) if the_noise > food_threshold: the_map.set(FoodTile(), x, y, 0) elif the_noise < money_threshold: the_map.set(MoneyTile(), x, y, 0) return the_map
def make_crevasse(the_map: world.Map): h = the_map.height w = the_map.width for y in range(h): the_noise = mn.simplex_noise_1d(y, the_map.seed) * (w / 2) # Output from 0 to width/2 border = (w - the_noise) / 2.0 # Space on either side of the crevice for x in range(w): if border < x < w - border: the_map.set(CrevasseTile(), x, y, 0) return the_map
def __init__(self, width=640, height=480, map_width=60, map_height=60, map_depth=60): # version hints: create GL window with >= OpenGL 3.3 and core profile glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL.GL_TRUE) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.RESIZABLE, False) self.win = glfw.create_window(width, height, 'Viewer', None, None) # glfw.maximize_window(self.win) # self.add(self.terrain.create()) # make win's OpenGL context current; no OpenGL calls can happen before glfw.make_context_current(self.win) # register event handlersVertexProf glfw.set_key_callback(self.win, self.on_key) # useful message to check OpenGL renderer characteristics print( 'OpenGL', GL.glGetString(GL.GL_VERSION).decode() + ', GLSL', GL.glGetString(GL.GL_SHADING_LANGUAGE_VERSION).decode() + ', Renderer', GL.glGetString(GL.GL_RENDERER).decode()) # initialize GL by setting viewport and default render characteristics GL.glClearColor(0.1, 0.1, 0.1, 1) GL.glEnable(GL.GL_DEPTH_TEST) GL.glEnable(GL.GL_CULL_FACE) # compile and initialize shader programs once globally #self.color_shader = Shader(COLOR_VERT, COLOR_FRAG) # initially empty list of object to draw self.drawables = [] self.trackball = GLFWTrackball(self.win) self.fill_modes = cycle([GL.GL_LINE, GL.GL_POINT, GL.GL_FILL]) GL.glEnable(GL.GL_DEPTH_TEST) #Maxime t super().__init__() #Init du node self.x = 0 self.y = 0 self.z = 10 self.terrain = Map(map_width, map_height, map_depth) self.add(self.terrain.create())
def __init__(self, save): #self.position = (0,0) feature_list = {} if save is None: self.map = Map() self.location = 0
def test_get_cell(self): m = Map() m.add_cell(0, 1) m.add_cell(1, 1) self.assertIsNotNone(m.get_cell(0, 1)) self.assertIsNone(m.get_cell(0, 0)) self.assertIsNotNone(m.get_cell(1, 1)) self.assertIsNone(m.get_cell(1, 0))
class Game(): def __init__(self): pygame.init() self.surface = pygame.display.set_mode( (world.SCREENWIDTH, world.SCREENHEIGHT)) pygame.display.set_caption(TITLE) self.surface.fill(world.UGLY_PINK) self.world_map = Map(MAPFILE, self) def _get_decision(self): d = ['above', 'below', 'right', 'left'] ran = randint(0, 3) return d[ran] def next_turn(self): # --------- Read patterns ------------- # read tiles from left to right, top to bottom. self.world_map.examine_world() # -------- decision ----------------- self.world_map.decide() def draw(self): self.world_map.draw() def game_loop(self): print("===== BEGIN WHILE LOOP ========") while True: # print("begin while loop") for event in pygame.event.get(): # print("inside pygame.event.get()") if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: print("inside pygame.KEYDOWN") if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() if event.key == pygame.K_t: print("before next turn") mygame.next_turn() print("after next turn") mygame.next_turn() mygame.draw() pygame.display.update() time.sleep(1) print("===== END WHILE LOOP ========")
def add_forts(the_map: world.Map, food_range=4, money_range=4, fort_range=6): max_range = max(food_range, money_range, fort_range) for x, y in itertools.product(range(the_map.width), range(the_map.height)): if not the_map.get(x, y).is_wall: food = 0 money = 0 fort = 0 for ox, oy in itertools.product(get_safe_range(x, the_map.width, max_range), get_safe_range(y, the_map.height, max_range)): if max(abs(ox - x), abs(oy - y)) <= food_range and \ isinstance(the_map.get(ox, oy), resource.FoodTile().__class__): food += 1 if max(abs(ox - x), abs(oy - y)) <= money_range and \ isinstance(the_map.get(ox, oy), resource.MoneyTile().__class__): money += 1 if max(abs(ox - x), abs(oy - y)) <= fort_range and \ isinstance(the_map.get(ox, oy), FortTile().__class__): fort += 1 if not fort and money > 2 and food > 2 and food + money > 5: the_map.set(FortTile(), x, y, 0) return the_map
def add_forts(the_map: world.Map, food_range=4, money_range=4, fort_range=6): max_range = max(food_range, money_range, fort_range) for x, y in itertools.product(range(the_map.width), range(the_map.height)): if not the_map.get(x, y).is_wall: food = 0 money = 0 fort = 0 for ox, oy in itertools.product( get_safe_range(x, the_map.width, max_range), get_safe_range(y, the_map.height, max_range)): if max(abs(ox - x), abs(oy - y)) <= food_range and \ isinstance(the_map.get(ox, oy), resource.FoodTile().__class__): food += 1 if max(abs(ox - x), abs(oy - y)) <= money_range and \ isinstance(the_map.get(ox, oy), resource.MoneyTile().__class__): money += 1 if max(abs(ox - x), abs(oy - y)) <= fort_range and \ isinstance(the_map.get(ox, oy), FortTile().__class__): fort += 1 if not fort and money > 2 and food > 2 and food + money > 5: the_map.set(FortTile(), x, y, 0) return the_map
class Viewer(Node): """ GLFW viewer window, with classic initialization & graphics loop """ def __init__(self, width=640, height=480, map_width=60, map_height=60, map_depth=60): # version hints: create GL window with >= OpenGL 3.3 and core profile glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL.GL_TRUE) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.RESIZABLE, False) self.win = glfw.create_window(width, height, 'Viewer', None, None) # glfw.maximize_window(self.win) # self.add(self.terrain.create()) # make win's OpenGL context current; no OpenGL calls can happen before glfw.make_context_current(self.win) # register event handlersVertexProf glfw.set_key_callback(self.win, self.on_key) # useful message to check OpenGL renderer characteristics print( 'OpenGL', GL.glGetString(GL.GL_VERSION).decode() + ', GLSL', GL.glGetString(GL.GL_SHADING_LANGUAGE_VERSION).decode() + ', Renderer', GL.glGetString(GL.GL_RENDERER).decode()) # initialize GL by setting viewport and default render characteristics GL.glClearColor(0.1, 0.1, 0.1, 1) GL.glEnable(GL.GL_DEPTH_TEST) GL.glEnable(GL.GL_CULL_FACE) # compile and initialize shader programs once globally #self.color_shader = Shader(COLOR_VERT, COLOR_FRAG) # initially empty list of object to draw self.drawables = [] self.trackball = GLFWTrackball(self.win) self.fill_modes = cycle([GL.GL_LINE, GL.GL_POINT, GL.GL_FILL]) GL.glEnable(GL.GL_DEPTH_TEST) #Maxime t super().__init__() #Init du node self.x = 0 self.y = 0 self.z = 10 self.terrain = Map(map_width, map_height, map_depth) self.add(self.terrain.create()) def run(self): """ Main render loop for this OpenGL window """ while not glfw.window_should_close(self.win): # clear draw buffer GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) # draw our scene objects projection = self.trackball.projection_matrix( glfw.get_window_size(self.win)) view = self.trackball.view_matrix() model = identity() # NodeStorage.get("cube1") super().draw(projection, view, model, win=self.win) # flush render commands, and swap draw buffers glfw.swap_buffers(self.win) # Poll for and process events glfw.poll_events() def on_key(self, _win, key, _scancode, action, _mods): """ 'Q' or 'Escape' quits """ if action == glfw.PRESS or action == glfw.REPEAT: if key == glfw.KEY_ESCAPE: glfw.set_window_should_close(self.win, True) if key == glfw.KEY_Z: #W GL.glPolygonMode(GL.GL_FRONT_AND_BACK, next(self.fill_modes)) if key == glfw.KEY_O: NodeStorage.get("player").reset_time() if key == glfw.KEY_W: self.trackball.translate_easy(0, 0, 1) if key == glfw.KEY_S: self.trackball.translate_easy(0, 0, -1) if key == glfw.KEY_DOWN: self.trackball.translate_easy(0, 1, 0) if key == glfw.KEY_UP: self.trackball.translate_easy(0, -1, 0) if key == glfw.KEY_A: self.trackball.rotate_easy((0, 1, 0), -2) if key == glfw.KEY_D: self.trackball.rotate_easy((0, 1, 0), 2) if key == glfw.KEY_LEFT: self.trackball.rotate_easy((1, 0, 0), -2) if key == glfw.KEY_RIGHT: self.trackball.rotate_easy((1, 0, 0), 2) if key == glfw.KEY_N: self.trackball.reset_rotation() if key == glfw.KEY_M: self.trackball.reset_hard() if key == glfw.KEY_Y: NodeStorage.get("player_node").translate(0, 0, -1) self.terrain.elevate(NodeStorage.get("player_node")) NodeStorage.get("player").reset_time() if key == glfw.KEY_G: NodeStorage.get("player_node").translate(-1, 0, 0) self.terrain.elevate(NodeStorage.get("player_node")) NodeStorage.get("player").reset_time() if key == glfw.KEY_H: NodeStorage.get("player_node").translate(0, 0, 1) self.terrain.elevate(NodeStorage.get("player_node")) NodeStorage.get("player").reset_time() if key == glfw.KEY_J: NodeStorage.get("player_node").translate(1, 0, 0) self.terrain.elevate(NodeStorage.get("player_node")) NodeStorage.get("player").reset_time() def set_terrain(self, terrain): """ Set the terrain Warning : do immediately after the initialization. Not before because to create the map we need first to create the viewer """ self.terrain = terrain
def drawscreen(): screen.fill((173, 173, 173)) for tile in level: tile.draw_tile(screen) player.playerdraw(screen) for i in slimelist: i.draw(screen) pygame.display.flip() #Screen 1 overworld1_spritesheet = Spritesheet('src/assets/temp/texturepack.png') sheet_items = [('grass0000.png', 1), ('sand0000.png', 0)] level = Map("src/assets/temp/level1.csv") level = level.create_map() level = make_tile_map(level, 32, overworld1_spritesheet, sheet_items) player = character.Player(224, 0) slimelist = [enemies.BlueSlime(540, 540), enemies.BlueSlime(400, 780)] #sound.beta_soundtrack() while True: clock.tick(30) pygame.display.set_caption( f"{player.rect.x}, {player.rect.y} - {player.health}") player.update(level) player.check_collision_enemy(slimelist) for action in pygame.event.get():
while True: self.step_count += 1 if self.advance_by( self.speed, noisy=False, checker=lambda r, dx, dy: not maze.segment_is_intersecting_walls( r.x, r.y, r.x+dx, r.y+dy)): break # Bumped into something or too long in same direction, # chose random new direction # print('self.chose_random_direction()') self.chose_random_direction() # ------------------------------------------------------------------------ world = Map('dfki-2nd-floor.json') world.draw() db = Database() all_loc_docs = db.view_result("all_location_documents_from_reckonme/") all_loc_docs = all_loc_docs[['2014-02-26']:] reckonme_location_data = {} for row in all_loc_docs: data = {k: v for k, v in db[row.id].iteritems()} if row.key[1] not in reckonme_location_data: reckonme_location_data[row.key[1]] = [data] else: reckonme_location_data[row.key[1]].append(data)
def test_add_cell(self): m = Map() m.add_cell(0, 0) self.assertIsNotNone(m.cells[0]) pass
text2 = font.render("GAME ROUND:%d, STEP:%d"%(GAME_ROUND, STEP), 1, (255,255,0)) screen.blit(text1, (250,5)) screen.blit(text2, (250,25)) if flip: pygame.display.flip() #print airship_rot. def draw_strategy(themap): # draws arrow showing wind direciton windarrow = themap.get_windsurface(scale=SCALE2) screen.blit(windarrow, (25, 500)) pygame.display.flip() world_map = Map("dublin.jpg", "windarrow.png", (1,1), windspeed=0.1, wind_direction=2*math.pi*random()) airship1 = Airship('airship.png', position=world_map.position) airship2 = Airship('Flecheairship.png', position=world_map.position+Vector(0,100)) airship3 = Airship('Vleermuisairship.png', position=world_map.position-Vector(0,100)) vessels = [airship1, airship2, airship3] font = pygame.font.Font(None, 36) blips = [] mapcenter = MAP_CENTER #delta_pos = Vector(0,0) GAME_ROUND = 0 STEP = 0 draw_action(screen, vessels) # GUI stuff