def reset_map(self): self.gen += 1 self.restart = False self.level = GameMap(self, self.gen) self.cur_items += self.next_items self.next_items = [] self.level.handle_nextgen(self.cur_items)
def __init__(self): if not self.was_inited_: self.was_inited_ = True super().__init__() self.french_player_ = FrenchPlayer() self.british_player_ = BritishPlayer() self.active_player_ = self.british_player_ self.game_map_ = GameMap(10, 5) self.current_scene_ = None self.instance_ = self
def event_loop(self): # get the pygame screen and create some local vars screen = pygame.display.get_surface() screen_rect = screen.get_rect() screen_width = screen.get_width() screen_height = screen.get_height() # set up font basicFont = pygame.font.SysFont(None, 48) # initialize a clock clock = pygame.time.Clock() # initialize the score counter score = 0 deltat = 0 # initialize the enemy speed enemy_speed = [6, 6] # initialize the game map self.gen = 0 self.restart = False self.level = GameMap(self, self.gen) self.next_items = [] self.cur_items = [] # main game loop while 1: if (self.restart == True): self.reset_map() self.level.handle_input() self.level.update(deltat) self.level.render() # set up the score text text = basicFont.render('Game Over?', True, (255, 0, 0)) textRect = text.get_rect() textRect.centerx = screen_rect.centerx textRect.centery = screen_rect.centery # draw the text onto the surface if (self.level.gameover == True): screen.blit(text, textRect) # update the screen pygame.display.flip() # limit to 60 FPS deltat = clock.tick(60)
def main(): from Mediator import Mediator pygame.init() WINDOW_SIZE = (1024, 768) clock = pygame.time.Clock() screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32) display = pygame.Surface((256, 192)) player = Player(display, 'player', Mediator) print(Mediator.i) Mediator.all_game_entities.append(player) gameMap = GameMap(display, 'tiles', Mediator, player) hud = HUD(display, player) while True: display.fill((57, 138, 215)) Mediator.all_game_tiles.clear() gameMap.draw_map() hud.draw_overlay() for object in Mediator.all_game_entities: object.loop() object.draw() Mediator.all_game_entities = [ object for object in Mediator.all_game_entities if object not in Mediator.to_be_removed ] Mediator.to_be_removed.clear() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit( 0 ) #TODO remember to exit with a code so we dont throw an exception i think surf = pygame.transform.scale(display, WINDOW_SIZE) screen.blit(surf, (0, 0)) pygame.display.update() clock.tick(60)
from GameMap import * from HUD import * pygame.init() WINDOW_SIZE = (1024,768) clock = pygame.time.Clock() screen = pygame.display.set_mode(WINDOW_SIZE,0,32) display = pygame.Surface((256,192)) mediator = Mediator() player = Player(display, 'player', mediator) mediator.all_game_entities.append(player) gameMap = GameMap(display, 'tiles', mediator, player) hud = HUD(display,player) while True: display.fill((57, 138, 215)) mediator.all_game_tiles.clear() gameMap.draw_map() hud.draw_overlay() for object in mediator.all_game_entities: object.loop() object.draw() mediator.all_game_entities = [object for object in mediator.all_game_entities if object not in mediator.to_be_removed]
# ClientGuiIntercomm.py """ This module is responsible for passing data between the client terminal and the Gui. It will parse MAP data, too, so the client doesn't have to do that. Both modules import this one and can call its methods to access data. """ import logging, GameMap logger = logging.getLogger("CGI") GUI = GameMap.PlayerMap() def send_to_gui(data): """ MAP type packets are parsed here before being sent off to the gui. Subtypes: S: this player's position (Self) P: other players' positions M: the position of other Mobs Currently, there is no way to distinguish one player from another or one mob from another. Only between players, mobs, and the client. Whenever the player moves, all the mapping data needed to draw the screen will be sent immediately afterwards, so it should be okay to clear the screen when the Self is updated. data will contain a list in the form [0,0,0] of the player's position. """
import GameMap screen = GameMap.game_map() class player: def __init__(self, sprite, user): self.user = user self.player_sprite = sprite self.x = len(screen.Map[0]) / 2 - 1 self.y = len(screen.Map) / 2 - 1 self.prev_x = len(screen.Map[0]) / 2 - 1 self.prev_y = len(screen.Map) / 2 - 1 def Transform(self, key): if key == "w": self.prev_x = self.x self.prev_y = self.y self.y += -1 elif key == "s": self.prev_x = self.x self.prev_y = self.y self.y += 1 elif key == "d": self.prev_x = self.x self.prev_y = self.y self.x += 1 elif key == "a": self.prev_x = self.x self.prev_y = self.y self.x += -1
class Game(QObject): turn_changed = pyqtSignal() unit_added_in_map = pyqtSignal(Unit, int, int) instance_ = None was_created_ = False was_inited_ = False def __init__(self): if not self.was_inited_: self.was_inited_ = True super().__init__() self.french_player_ = FrenchPlayer() self.british_player_ = BritishPlayer() self.active_player_ = self.british_player_ self.game_map_ = GameMap(10, 5) self.current_scene_ = None self.instance_ = self def __new__(cls): if not cls.was_created_: cls.instance_ = QObject.__new__(cls) cls.was_created_ = True return cls.instance_ def set_scene(self, scene): self.current_scene_ = scene self.unit_added_in_map.connect(self.current_scene_.add_unit_item) def get_french_player(self): return self.french_player_ def get_british_player(self): return self.british_player_ def get_game_map(self): return self.game_map_ def get_game_scene(self): return self.game_scene_ def add_unit_in_map(self, unit_type, x, y): if self.game_map_.can_unit_be_placed(x, y): new_unit = None if unit_type == UnitType.SWORDSMAN: new_unit = self.active_player_.add_swordsman() elif unit_type == UnitType.ARCHER: new_unit = self.active_player_.add_archer() elif unit_type == UnitType.CAVALRY: new_unit = self.active_player_.add_cavalry() if new_unit is not None: self.game_map_.add_unit(new_unit, x, y) self.unit_added_in_map.emit(new_unit, x, y) self.end_turn() def end_turn(self): if self.active_player_ is self.british_player_: self.active_player_ = self.french_player_ else: self.active_player_ = self.british_player_ self.turn_changed.emit()
class GameEngine: def event_loop(self): # get the pygame screen and create some local vars screen = pygame.display.get_surface() screen_rect = screen.get_rect() screen_width = screen.get_width() screen_height = screen.get_height() # set up font basicFont = pygame.font.SysFont(None, 48) # initialize a clock clock = pygame.time.Clock() # initialize the score counter score = 0 deltat = 0 # initialize the enemy speed enemy_speed = [6, 6] # initialize the game map self.gen = 0 self.restart = False self.level = GameMap(self, self.gen) self.next_items = [] self.cur_items = [] # main game loop while 1: if (self.restart == True): self.reset_map() self.level.handle_input() self.level.update(deltat) self.level.render() # set up the score text text = basicFont.render('Game Over?', True, (255, 0, 0)) textRect = text.get_rect() textRect.centerx = screen_rect.centerx textRect.centery = screen_rect.centery # draw the text onto the surface if (self.level.gameover == True): screen.blit(text, textRect) # update the screen pygame.display.flip() # limit to 60 FPS deltat = clock.tick(60) def reset_map(self): self.gen += 1 self.restart = False self.level = GameMap(self, self.gen) self.cur_items += self.next_items self.next_items = [] self.level.handle_nextgen(self.cur_items) def run(self): # initialize pygame pygame.init() global SCREEN_HEIGHT global SCREEN_WIDTH # create the window size = width, height = SCREEN_WIDTH, SCREEN_HEIGHT screen = pygame.display.set_mode(size) # set the window title pygame.display.set_caption("Example Framework") # create the menu menu = cMenu(50, 50, 20, 5, 'vertical', 100, screen, [('Start Game', 1, None), ('Other Option', 2, None), ('Exit', 3, None)]) # center the menu menu.set_center(True, True) menu.set_alignment('center', 'center') # state variables for the finite state machine menu state = 0 prev_state = 1 # ignore mouse and only update certain rects for efficiency pygame.event.set_blocked(pygame.MOUSEMOTION) rect_list = [] while 1: # check if the state has changed, if it has, then post a user event to # the queue to force the menu to be shown at least once if prev_state != state: pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key=0)) prev_state = state # get the next event e = pygame.event.wait() # update the menu if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE: if state == 0: # "default" state rect_list, state = menu.update(e, state) elif state == 1: # start the game self.event_loop() elif state == 2: # just to demonstrate how to make other options pygame.display.set_caption("y u touch this") state = 0 else: # exit the game and program pygame.quit() sys.exit() # quit if the user closes the window if e.type == pygame.QUIT: pygame.quit() sys.exit() # update the screen pygame.display.update(rect_list)
def update(self): self.initSprite() self.gm = GameMap.GameMap() self.peta_game = self.gm.createMap("./assets/peta/map.txt") self.client.start() while(self.STATE==State.State.RUNNING): self.clock.tick(15) self.screen.fill(0) location=self.packageclient.location(self.player.x,self.player.y,self.player.file_player,self.room) self.client.sendall(location) self.up=pygame.key.get_pressed()[pygame.K_w] self.down = pygame.key.get_pressed()[pygame.K_s] self.left = pygame.key.get_pressed()[pygame.K_a] self.right = pygame.key.get_pressed()[pygame.K_d] self.space = pygame.key.get_pressed()[pygame.K_SPACE] for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if (self.space): self.peta_game[self.player.x][self.player.y]='!' bombData=self.packageclient.createPackageBomb(self.player.x,self.player.y,self.room) self.client.sendall(bombData) bomb=Bomb.Bomb(self) bomb.taruh(self.player.x,self.player.y) bomb.start() if (self.up): if (self.player.x-1>=0 and self.peta_game[self.player.x-1][self.player.y]=='0' ): self.player.x=self.player.x-1 if (self.down): if (self.player.x+1<11 and self.peta_game[self.player.x+1][self.player.y]=='0' ): self.player.x = self.player.x + 1 if (self.left): if (self.player.y-1>=0 and self.peta_game[self.player.x][self.player.y-1]=='0' ): self.player.y = self.player.y - 1 if (self.right): if (self.player.y+1<16 and self.peta_game[self.player.x][self.player.y+1]=='0' ): self.player.y = self.player.y + 1 for i in range(len(self.peta_game)): for j in range(len(self.peta_game[i])): if i==self.player.x and j==self.player.y: if (self.player.file_player=="player1"): self.screen.blit(self.p1, [j * 50, i * 50]) elif (self.player.file_player == "player2"): self.screen.blit(self.p2, [j * 50, i * 50]) elif i == self.player2.x and j == self.player2.y: if (self.player2.file_player == "player1"): self.screen.blit(self.p1, [j * 50, i * 50]) elif (self.player2.file_player == "player2"): self.screen.blit(self.p2, [j * 50, i * 50]) elif self.peta_game[i][j] == '0': self.screen.blit(self.grass, [j*50, i*50]) elif self.peta_game[i][j] == '1': self.screen.blit(self.wall,[j*50,i*50]) elif self.peta_game[i][j] == '2': self.screen.blit(self.box,[j*50,i*50]) elif self.peta_game[i][j] == '!': self.screen.blit(self.bomb,[j*50,i*50]) elif self.peta_game[i][j] == '@' : self.screen.blit(self.explosion,[j*50,i*50]) pygame.display.flip() if (self.isWin==False): self.gameOver() else : self.gameWin()
def init_game(self): SoundPlayer.play_background_music() gmap = GameMap() # init the map, it will be loaded for map files afterwards gmap.initMap(stones)