def render(self): #draw box = RectangleShape(self.size) box.position = self.position box.outline_thickness = 1 box.outline_color = Color(255, 0, 0, 100) box.fill_color = Color(0, 0, 0, 10) self.box = box
def _create_box(self): #draw size = self.size position = self.position # Box = RectangleShape(size) Box.position = position Box.fill_color = Color(255, 255, 255) Box.outline_color = Color(0, 0, 0) Box.outline_thickness = 1 # self.Box = Box # self._create_box_shading()
def __init__(self): self._create_corners() self.snap = True self.color = Color(255, 255, 255, 255) self.expand = True self.selected = False self.absolute = False
class _Rendered_Grid_Room(Drawable, Rectangle): # Drawable, forwards to Sprite # * Generated and rendered ONCE globally when called. ################################# # PUBLIC sprite = None def __init__(self): Drawable.__init__(self) self.w, self.h = ROOM_WIDTH, ROOM_HEIGHT self._create_Lines() self._render_Lines() def draw(self, target, states): self._draw_Lines(target, states) # color = Color(255, 255, 255, 255) ################################# # PRIVATE # Lines _rows = [] _columns = [] def _create_Lines(self): #init self._columns = [] for x in range(self.tile_w + 1): w = 1 if x == 0 or x == self.tile_w: w = 5 r = RectangleShape((w, self.h)) r.fill_color = self.color r.position = self.x + (TILE * x) - (w / 2), self.y self._columns.append(r) self._rows = [] for y in range(self.tile_h + 1): h = 1 if y == 0 or y == self.tile_h: h = 5 r = RectangleShape((self.w, h)) r.fill_color = self.color r.position = self.x, self.y + (TILE * y) - (h / 2) self._rows.append(r) # Rendering _texture = RenderTexture(ROOM_WIDTH + 1, ROOM_HEIGHT + 1) def _render_Lines(self): #init for line in self._columns + self._rows: self._texture.draw(line) self._texture.display() self.sprite = Sprite(self._texture.texture) def _draw_Lines(self, target, states): #draw target.draw(self.sprite, states)
def _create_highlight(self): #init, controls #Find the position of the current box. char = self().character x, y = 0, 0 if char in Text_Ref.characters.upper(): x = Text_Ref.characters.upper().index(char) y = 0 if char in Text_Ref.characters: x = Text_Ref.characters.index(char) y = 1 if char in Text_Ref.grammar: x = Text_Ref.grammar.index(char) y = 2 if char in Text_Ref.numbers: x = Text_Ref.numbers.index(char) y = 3 #Highlight it w, h = 8, 12 highlight = RectangleShape((w, h)) highlight.position = w * x, h * y highlight.fill_color = Color(100, 100, 100, 100) self.highlight = highlight
def _create_box_shading(self): #_create_box w = self.w x, y = self.x, self.y2 - 2 # Box_shading = RectangleShape((w, 2)) Box_shading.fill_color = Color(150, 150, 150) Box_shading.position = x, y # self.Box_shading = Box_shading
def _create_baseLine(self): if self.using == "x": self.baseLine = RectangleShape((self.w, 0)) self.baseLine.position = self.x, self.center[1] if self.using == "y": self.baseLine = RectangleShape((0, self.h)) self.baseLine.position = self.center[0], self.y self.baseLine.outline_thickness = 1 self.baseLine.outline_color = Color(0, 0, 0) #color b = self.baseLine c = b.outline_color c.a = self.alpha b.outline_color = c
class Grid_Room(Drawable, Rectangle): # Drawable # * Uses a pre-rendered sprite. Size cannot be changed. # (Sprite is rendered ONCE as a global upon access.) ################################# # PUBLIC def draw(self, target, states): self._Grid_Room.sprite.position = self.position target.draw(self._Grid_Room, states) ################################# # PRIVATE _Grid_Room = _Rendered_Grid_Room() _Grid_Room.sprite.color = Color(0, 0, 0, 100) ################################################
def texture_slices(self, columns, sprites, bob): x = 0 for column in columns: try: # XXX This is crap, should check in the fix_distance height = int((64 / column[0] * PP_DISTANCE)+.5) except(ZeroDivisionError): height = int(64 * PP_DISTANCE+.5) if column[3] == True: sprites[x].color = Color(200,200,200,255) else: sprites[x].color = Color.WHITE sprites[x].set_texture_rect(IntRect(column[2]+64*column[1],0,1,64)) sprites[x].position = (x, PP_HEIGHT/2 - height/2 + bob) sprites[x].scale = (1, height / 64) x += 1 return sprites
def _create_vertices(self): #Room.render self.vertices = [] #points point1 = Vertex() point2 = Vertex() point3 = Vertex() point4 = Vertex() points = [point1, point2, point3, point4] #position x1, y1, x2, y2 = self.points point1.position = x1, y1 point2.position = x2, y1 point3.position = x2, y2 point4.position = x1, y2 #clip if self.texture == None: for point in points: point.tex_coords = 0, 0 point.color = Color(0, 0, 0, 0) else: clip_x, clip_y = self.clip x1 = (clip_x + 0) * TILE y1 = (clip_y + 0) * TILE x2 = (clip_x + 1) * TILE y2 = (clip_y + 1) * TILE point1.tex_coords = x1, y1 point2.tex_coords = x2, y1 point3.tex_coords = x2, y2 point4.tex_coords = x1, y2 for point in points: self.vertices.append(point)
class active_colors: normal_color = Color(240,240,240) hovered_color = Color(255,255,255) held_color = hovered_color selected_color = hovered_color
class Button(Box): # State Handling # * Hovering, Pressing, Selecting # Graphics # * Rises based on state. # * Colored based on state. # * (Optional) contains text. w, h = 50, 20 def __init__(self): Box.__init__(self) self._init_Text() def controls(self, Key, Mouse, Camera): self._hover(Mouse) self._select(Mouse) self._held(Mouse) self._color_states() Box.controls(self, Key, Mouse, Camera) def draw(self, target, states): Box.draw(self, target, states) self._draw_Text(target, states) #################### ### STATE HANDLING hovered = False held = False selected = False def _hover(self, Mouse): self.hovered = Mouse.inside(self) def _held(self, Mouse): if Mouse.left.pressed(): if self.hovered: self.held = True self.rise = 0 elif not Mouse.left.held(): self.held = False self.rise = self.old_rise def _select(self, Mouse): self.selected = False if not Mouse.left.held(): if self.held: self.selected = True ### COLORING box_fill = Color(240, 240, 240) old_fill = box_fill hovered_color = Color(255, 255, 255) held_color = hovered_color selected_color = hovered_color def _color_states(self): self.box_fill = self.old_fill if self.hovered: self.box_fill = self.hovered_color if self.held: self.box_fill = self.held_color if self.selected: self.box_fill = self.selected_color ### TEXT (Optional) # Create, pos/alpha, draw def _init_Text(self): self.text = "" self._Text = Text(Font("speech")) self._text = self.text def _draw_Text(self, target, states): #update text if self._text != self.text: self._Text.write(self.text) #color text = self._Text c = text.color c.a = self.alpha text.color = c #pos text.center = self.center text.y -= self.rise_offset #draw target.draw(text)
class Cursor(Rectangle): ################################# # PUBLIC absolute = False expand = True color = Color(255, 255, 255, 255) selected = False #readonly def __init__(self): self._create_corners() self.snap = True self.color = Color(255, 255, 255, 255) self.expand = True self.selected = False self.absolute = False def controls(self, Key, Mouse, Camera): self._update_position(Mouse, Camera) if self.expand: self._expand(Mouse, Camera) def draw(self, target, states): self._update_corners_position() self._update_corners_alpha() self._draw_corners(target, states) ################################# # PRIVATE # Position _selected = False def _update_position(self, Mouse, Camera): if not self._selected: self.tile_position = Mouse.tile_position self.tile_size = 0, 0 if self.absolute: self.tile_x = int(self.tile_x / Camera.zoom) self.tile_y = int(self.tile_y / Camera.zoom) self.tile_x += Camera.tile_x self.tile_y += Camera.tile_y if not Mouse.left.released(): self.selected = False _start_anchor = 0, 0 _finish_anchor = 0, 0 def _expand(self, Mouse, Camera): #LOGIC #start if Mouse.left.pressed() and not self._selected: self._selected = True self._start_anchor = Mouse.tile_position if self.absolute: self._start_anchor[0] += Camera.tile_x self._start_anchor[1] += Camera.tile_y #loop if self._selected: self._finish_anchor = Mouse.tile_position if self.absolute: self._finish_anchor[0] += Camera.tile_x self._finish_anchor[1] += Camera.tile_y #reset self.selected = False if not Mouse.left.held(): if self._selected: self.selected = True self._selected = False self._start_anchor = 0, 0 self._finish_anchor = 0, 0 #EFFECTS if self._selected: x1, y1, x2, y2 = self._start_anchor + self._finish_anchor if x1 > x2: x2, x1 = x1, x2 if y1 > y2: y2, y1 = y1, y2 self.tile_position = x1, y1 self.tile_size = x2 - x1, y2 - y1 # Corners # color _corners = [] def _create_corners(self): self._corners = [] t = Texture.from_file("assets/ui/cursor.png") s1 = MySprite(t) s1.clip.set(8, 8) s1.clip.use(0, 0) s2 = MySprite(t) s2.clip.set(8, 8) s2.clip.use(2, 0) s3 = MySprite(t) s3.clip.set(8, 8) s3.clip.use(0, 2) s4 = MySprite(t) s4.clip.set(8, 8) s4.clip.use(2, 2) s2.origin = -TILE + 8, 0 s3.origin = 0, -TILE + 8 s4.origin = -TILE + 8, -TILE + 8 self._corners = [[s1, s2], [s3, s4]] def _update_corners_position(self): self._corners[0][0].position = self.x1, self.y1 self._corners[0][1].position = self.x2, self.y1 self._corners[1][0].position = self.x1, self.y2 self._corners[1][1].position = self.x2, self.y2 def _update_corners_alpha(self): for side in self._corners: for edge in side: edge.color = self.color def _draw_corners(self, target, states): for side in self._corners: for edge in side: target.draw(edge, states)
class active_colors: normal_color = Color(0, 255, 0) hovered_color = Color(100, 255, 100) held_color = hovered_color selected_color = hovered_color
def clear(self, color): self._events() c = Color(*color) self.window.clear(c)
def __init__(self, position, letter): self.position = position self.size = 0, 0 self.letter = letter self.color = Color(255, 255, 255, 255)
def color(self, c): color = [v for v in c] for letter in self.letters: letter.color = Color(*color) self._color = Color(*color)
class Text(_Text, Drawable, Rectangle): color = None x, y, w, h = 0, 0, 0, 0 follow = True #for UI def __init__(self, Font): self.Font = Font def draw(self, target, states): self._create_vertex_array() target.draw(self.vertex_array, self.render_states) # #CALL string = "" def write(self, string): self.string = string self._create_letters() self._create_vertex_array() #POSITION _x, _y = 0, 0 @property def x(self): return self._x @x.setter def x(self, x): amt = x - self._x self._x = x for letter in self.letters: letter.x += amt @property def y(self): return self._y @y.setter def y(self, y): amt = y - self._y self._y = y for letter in self.letters: letter.y += amt @property def w(self): try: w = 0 for letter in self.letters: new_w = letter.x2 - self.letters[0].x1 if new_w > w: w = new_w return w except: return 0 @property def h(self): try: return self.letters[-1].y2 - self.letters[0].y1 except: return 0 # #COLOR _color = Color(255, 255, 255, 255) @property def color(self): return self._color @color.setter def color(self, c): color = [v for v in c] for letter in self.letters: letter.color = Color(*color) self._color = Color(*color) _alpha = 255 @property def alpha(self): return self._alpha @alpha.setter def alpha(self, a): for letter in self.letters: c = letter.color c.a = a letter.color = c self._alpha = a # class Letter(_Letter, Rectangle): # * Position # * Color def __init__(self, position, letter): self.position = position self.size = 0, 0 self.letter = letter self.color = Color(255, 255, 255, 255)
def shadow(self): #draw w, h = self.size b = RectangleShape((w, self.rise)) b.position = self.x, self.y2 b.fill_color = Color(0, 0, 0, 255) return b
def main(): logg.info('Starting the main function') #Instancing, etc. main_view = View().from_rect( FloatRect(0, 0, PP_WIDTH, PP_HEIGHT + BAR_HEIGHT)) window = RenderWindow( VideoMode(PP_WIDTH * SCALE, (PP_HEIGHT + BAR_HEIGHT) * SCALE), GAME_TITLE + ' v.' + GAME_VERSION) window.framerate_limit = 61 window.view = main_view # INITIALIZE TEXTURES HERE OR AFTER \o/ TEXTURE_WALL = Texture.load_from_file('main/walls.png') TEXTURE_BAR = Texture.load_from_file('main/bar.png') TEXTURE_HUDWEAPONS = Texture.load_from_file('main/hud_weapons.png') TEXTURE_FACES = Texture.load_from_file('main/faces.png') TEXTURE_NUMBERS = Texture.load_from_file('main/numbers.png') TEXTURE_WEAPONS = Texture.load_from_file('main/weapons.png') TEXTURE_ENEMIES = Texture.load_from_file('main/test.png') #Create an instance for all game variables and loop functions # and set the level to TESTLEVEL game = Gameworld(TEXTURE_ENEMIES) game.create_dict_map() game.player.gamemap = game.current_level game.init_physics() game.physics.mob_bodies(game.entities) #prepare the hud hud = Hud(player=game.player, background=TEXTURE_BAR, faces=TEXTURE_FACES, hudweapons=TEXTURE_HUDWEAPONS, weapons=TEXTURE_WEAPONS, numbers=TEXTURE_NUMBERS) #prepare the wall textures wall_sprites = game.create_wall_sprite_list(TEXTURE_WALL) rays = Raycaster(player=game.player, sprites=wall_sprites, gamemap=game.current_level) #prepare other stuff player_action = '' running = True nofocus = False ## # MAIN LOOP ## logg.info('Main loop starting...') while running: #iterate events for event in window.iter_events(): if event.type == Event.CLOSED or player_action == 'quit': running = False if event.type == Event.LOST_FOCUS: nofocus = True elif event.type == Event.GAINED_FOCUS: nofocus = False if event.type == Event.KEY_RELEASED: if game.player.bob > 0: #level the headbobbing game.player.bob -= .5 elif game.player.bob < 0: game.player.bob += .5 game.player.strafing = False #disable speed limiter for moving in 2 axii window.clear(Color(235, 235, 235, 255)) #clear the window of everything for sprite in wall_sprites: #draw walls window.draw(sprite) #draw entities here for entity in game.entities: if entity.visible == True: for sprite_slice in entity.sprite: window.draw(sprite_slice) hud.display(window) #draw the hud debug_txt = text('[' + str(draw_fps(frame)) + '] ' + str("{0:.2f}".format(game.player.ux)) + '(' + str(game.player.x) + '),' + str("{0:.2f}".format(game.player.uy)) + '(' + str(game.player.y) + '):' + str(game.player.heading), style=1) window.draw(debug_txt) wall_sprites = rays.texture_slices( rays.cast_rays(), wall_sprites, game.player.bob) #determine which walls to display #determine wich entities to display and prepare for entity in game.entities: #calculate the distance of entities to the player entity.distance_to_player(game.player) # print(str(round(entity.distance, 2)) + " " + str(entity.x) + "," + str(entity.y)) game.entities.sort(key=lambda x: x.distance, reverse=True) #sort entities based on the distance for entity in game.entities: entity.set_sprite_for_display(game.player, rays.distances) ### # TIMERS ### if game.player.attack_delay > 0 and game.player.attack == True: game.player.attack_delay -= 1 elif game.player.attack == True and game.player.attack_delay == 0: game.player.attack = False game.player.attack_delay = 2 elif game.player.attack == False and game.player.attack_delay > 0: game.player.attack_delay -= 1 if len(game.doors) != 0: # print('lol doors') for door in game.doors: # print(door.openess) state = door.open_close() # print(state) if state == 'done': # print('removing doors') game.doors.remove(door) game.physics.world.ClearForces() if not nofocus: player_action = game.handle_keys() #player input game.physics.world.Step(1.0 / 60.0, 10, 8) game.player.update_position() for entity in game.entities: entity.update_position() window.display() #blit to window window.close() logg.info('Terminating. Have a nice day.') logg.info('Average FPS: %s', round(average_fps / all_frames, 2))
class Cancel_Button(Button): hovered_color = Color(255, 150, 150) held_color = Color.RED selected_color = Color.RED text = "Nah"
class active_colors: normal_color = Color(100, 100, 100) hovered_color = Color(150, 150, 150) held_color = hovered_color selected_color = hovered_color
class Accept_Button(Button): hovered_color = Color(150, 255, 150) held_color = Color.GREEN selected_color = Color.GREEN text = "Sure"