def __init__(self): self.sender = Sender() self.sender_thread = threading.Thread(target=self.sender.run, name='sender_thread', daemon=True) self.sender_thread.start() pygame.init() # Set up the window self.window_surface = pygame.display.set_mode((800, 800), 0, 32) pygame.display.set_caption('Hello World') self.map = Map() self.character = Character() server_address = ('127.0.0.1', 8888) print('connecting to {} port {}'.format(*server_address)) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect(server_address) self.sock.setblocking(False) self.mysel = selectors.DefaultSelector() self.mysel.register( self.sock, selectors.EVENT_READ | selectors.EVENT_WRITE, ) self.received_msg = [] self.outgoing_msg = []
class GameEngine: def __init__(self): self.player = Character("test", main_batch, foreground) self.background = Background(self.player, main_batch, background) def draw(self, fps=0): self.background.update() self.player.apply_forces()
class GameServer: connections = [] MSG_NOTIFY_DELAY = 2 # sec def __init__(self): self.world = GameWorld() self.notify = True self.character = Character() async def tick(self): await self.world.run() async def notify_clients(self): while self.notify: for con in self.connections: con.send({'tick': self.world.counter}) con.send({'character': self.character.serialize()}) await asyncio.sleep(self.MSG_NOTIFY_DELAY) def add_connection(self, connection): self.connections.append(connection) def remove_connection(self, connection): self.connections.remove(connection)
app = App() app.gameMap.createGround(0, app.gameMap.width, app.gameMap.height, app.gameMap.height) # Create the ground app.gameMap.createGround(0, 150, app.gameMap.height - 100, app.gameMap.height - 100) # Create the ground app.gameMap.createGround(150, 300, app.gameMap.height - 200, app.gameMap.height - 200) # Create the ground app.gameMap.createGround(300, 450, app.gameMap.height - 300, app.gameMap.height - 300) # Create the ground app.gameMap.createGround(450, 600, app.gameMap.height - 400, app.gameMap.height - 400) # Create the ground mainCharacter = Character(type="Character", name="myCharacter", x=50, y=500, mapObject=app.gameMap, objectAnimationDir="character_1/", standard_vel_x=6) app.controlledObject = mainCharacter createObjectInGame(mainCharacter, app.gameMap) monster1 = Character(type="Monster", name="monster1", x=500, y=500, mapObject=app.gameMap, characters_path="assets/monsters/", objectAnimationDir="monster_1/") createObjectInGame(monster1, app.gameMap,
def main(self): #generating game objects def_img = pygame.image.load('char.png') character = Character([500, 500], [10, 10], 20, def_img) self.game_objects.append(character) dummy = Dummy([480, 480], [8, 8], def_img) dummy1 = Dummy([460, 500], [8, 8], def_img) self.game_objects.append(dummy) self.game_objects.append(dummy1) camera = Camera(self.screen, self.unit, character.pos) camera.scale(self.game_objects) #scale all game objects #game loop while self.running: camera.pos = character.pos tick = self.time.tick(Constants.FPS) self.screen.fill(Constants.WHITE) for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1 or event.button == 6: bullet = Bullet(character.pos, [1, 1], def_img) self.game_objects.append(bullet) camera.scale(bullet) if event.button == 4: camera.zoom(-1) camera.scale(self.game_objects) elif event.button == 5: camera.zoom(+1) camera.scale(self.game_objects) elif event.type == pygame.KEYDOWN: key = 0 if event.key == pygame.K_UP or event.key == pygame.K_w: key = 0 if event.key == pygame.K_LEFT or event.key == pygame.K_a: key = 1 if event.key == pygame.K_DOWN or event.key == pygame.K_s: key = 2 if event.key == pygame.K_RIGHT or event.key == pygame.K_d: key = 3 character.key_press(key) elif event.type == pygame.KEYUP: key = 0 if event.key == pygame.K_UP or event.key == pygame.K_w: key = 0 if event.key == pygame.K_LEFT or event.key == pygame.K_a: key = 1 if event.key == pygame.K_DOWN or event.key == pygame.K_s: key = 2 if event.key == pygame.K_RIGHT or event.key == pygame.K_d: key = 3 character.key_release(key) objects_to_render = camera.in_render_distance(self.game_objects) character.update(tick, objects_to_render, pygame.mouse.get_pos(), camera.game_to_screen_pos(character)) camera.draw(objects_to_render) pygame.display.update()
class Game(object): RUN = True def __init__(self): self.sender = Sender() self.sender_thread = threading.Thread(target=self.sender.run, name='sender_thread', daemon=True) self.sender_thread.start() pygame.init() # Set up the window self.window_surface = pygame.display.set_mode((800, 800), 0, 32) pygame.display.set_caption('Hello World') self.map = Map() self.character = Character() server_address = ('127.0.0.1', 8888) print('connecting to {} port {}'.format(*server_address)) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect(server_address) self.sock.setblocking(False) self.mysel = selectors.DefaultSelector() self.mysel.register( self.sock, selectors.EVENT_READ | selectors.EVENT_WRITE, ) self.received_msg = [] self.outgoing_msg = [] def incoming_messages(self): for key, mask in self.mysel.select(timeout=0): connection = key.fileobj if mask & selectors.EVENT_READ: data = connection.recv(1024) while data: pos = data.find(b'\n') if pos >= 0: msg = data[:pos] data = data[pos + 1:] self.received_msg.append(json.loads(msg)) def run(self): clock = pygame.time.Clock() while self.RUN: clock.tick(60) self.incoming_messages() try: msg = self.received_msg.pop(0) except IndexError: pass else: if msg.get('map'): self.map.initialize(msg['map']) elif msg.get('character'): self.character.move(msg) try: msg = self.outgoing_msg.pop(0) except IndexError: pass else: self.sock.sendall(json.dumps(msg).encode()) self.map.render(self.window_surface) self.character.render(self.window_surface) pygame.display.flip() key_pressed = pygame.key.get_pressed() if key_pressed[pygame.K_w]: print('SEND: ', time.time()) # self.sender.send({'move': 10}) self.outgoing_msg.append({'move': 10}) for event in pygame.event.get(): if event.type == QUIT: self.RUN = False pygame.quit()
def __init__(self): self.player = Character("test", main_batch, foreground) self.background = Background(self.player, main_batch, background)
def __initCharacter(self): char = Character(self.pg, self.screen) char.drawInit(self.CC, self.C_WIDTH, self.C_HEIGHT) return char
def __init__(self): self.world = GameWorld() self.notify = True self.character = Character()