def winGame(self, obj, arg): self.world.msgr.displayMsg('You win!', 2) self.log.info('win the game') self.manager.removeBehavioursByName('player-movement') self.keys = [0] * 5 timer.setTimer(common.E_GAME_OVER, 3) olctlhub.send(['win'])
def inc(self): self.value[0] += 1 if self.value[0] > self.maxValue: self.value[0] = 1 self.updateText() track.track_num[0] = self.value[0] if self.world.name == 'waiting-screen': olctlhub.send(['chg-track', self.value[0]])
def addedToWorld(self, world): super(MainScreen, self).addedToWorld(world) self.manager = world.findActorByName('behaviours') # #Now add actors to the world # #for example, # # self.player = serge.blocks.utils.addActorToWorld( # world, # player.PlayerCar('player', 'player') # ) # self.sock = olctlhub.sock self.player = serge.blocks.utils.addActorToWorld( world, player.Player(True, True, self.sock), physics = serge.physical.PhysicalConditions( mass = 4, width = 48, height = 64, elasticity = 0.95, friction = 0.02, update_angle = True ), center_position = (300, 300), origin = (300, 300), ) # TODO add a condition statement for non-network self.olctl = serge.blocks.utils.addActorToWorld( world, olctlhub.OLControlHub(self.sock) ) self.log.info('Create OLCtl complete with players '+str(self.other_player_num)) self.log.info(len(world.findActorsByTag('player'))) while len(world.findActorsByTag('player')) != self.other_player_num + 1: data = json.dumps(['request-player']) olctlhub.send(data) self.olctl.updateActor(1, world) camera = serge.engine.CurrentEngine().renderer.getCamera() camera.setTarget(self.player) self.bg = serge.blocks.utils.addActorToWorld( world, bg.Background('bg', 'bg'), ) self.ground = serge.blocks.utils.addActorToWorld( world, bg.Ground('ground', 'ground'), )
def __call__(self, world, actor, interval): mouse = serge.engine.CurrentEngine().getMouse() if mouse.isClicked(serge.input.M_LEFT): for button in mouse.getActorsUnderMouse(world): self.log.info('button type:'+button.tag) if button.tag == 'list-item': # join in a room button.hightlight() olctlhub.send(json.dumps(['join-room', int(button.name)])) else: if button.name == 'refresh': olctlhub.send(json.dumps(['view-rooms'])) elif button.name == 'new': olctlhub.send(json.dumps(['new-room']) ) olctlhub.send(json.dumps(['view-rooms'])) elif button.name == 'start': olctlhub.send(json.dumps(['start']))
def addedToWorld(self, world): super(MainScreen, self).addedToWorld(world) self.broadcaster.linkEvent(common.E_GAME_OVER, self.destroy) self.manager = world.findActorByName("behaviours") # # Now add actors to the world # # for example, # # self.player = serge.blocks.utils.addActorToWorld( # world, # player.PlayerCar('player', 'player') # ) # self.sock = olctlhub.sock # # add bars to the track print track.start_pos self.player = serge.blocks.utils.addActorToWorld( world, player.Player(self.isOLPlay, True, self.sock), physics=serge.physical.PhysicalConditions( mass=3, width=48, height=64, elasticity=0.0, friction=0.9, update_angle=True ), center_position=track.start_pos, origin=track.start_pos, ) camera = serge.engine.CurrentEngine().renderer.getCamera() camera.setTarget(self.player) if self.isOLPlay: self.olctl = serge.blocks.utils.addActorToWorld(world, olctlhub.OLControlHub(self.sock)) self.log.info("Create OLCtl complete with players " + str(self.other_player_num)) while len(world.findActorsByTag("player")) != self.other_player_num + 1: data = ["request-player"] olctlhub.send(data) self.olctl.updateActor(1, world) self.ground = serge.blocks.utils.addActorToWorld(world, bg.Ground("ground", "ground")) serge.blocks.utils.addActorToWorld(world, bg.Background())
def updateActor(self, interval, world): try: recieved = olctlhub.sock.recv(1024, socket.MSG_DONTWAIT) except: return recieved = json.loads(recieved) if recieved[1] == 'room-list': self.roomList.updateList(recieved[2]) elif recieved[1] == 'my-room': # DEBUG print recieved if recieved[2][0] == -1: return self.current_room_num.updateText('room #'+str(recieved[2][0])) self.current_room_players.updateText(str(recieved[2][1])+' players') self.current_room_track.value = [recieved[2][2]] self.current_room_track.updateText() elif recieved[1] == 'start': self.log.info('we will start') # param: players number and track mainscreen.main(int(recieved[2][0]) - 1, recieved[2][1], True) elif recieved[1] == 'keys': self.log.info('Other players have started but I\'m not. Trying to start...') olctlhub.send(["start-me"])
def updateActor(self, interval, world): """ Update the car's speed and direction """ # # Update forces and aquire speed body = self.physical_conditions.body body.reset_forces() self.speed = math.sqrt(body.velocity[0] ** 2 + body.velocity[1] ** 2) # # Handle turn left or turn right if self.keys[self.LEFT] and self.speed > 20: body.angle -= 0.05 if not self.keys[self.HANDBRAKE]: temp_angle = body.angle - math.pi / 2 body.apply_impulse( (math.cos(temp_angle) * self.force * 0.02, math.sin(temp_angle) * self.force * 0.02)) self.brake(0.95) else: # The car slip body.angle -= 0.04 if self.keys[self.RIGHT] and self.speed > 20: body.angle += 0.05 if not self.keys[self.HANDBRAKE]: temp_angle = body.angle + math.pi / 2 body.apply_impulse( (math.cos(temp_angle) * self.force * 0.02, math.sin(temp_angle) * self.force * 0.02)) self.brake(0.95) else: # The car slip body.angle += 0.04 # # Leave trace on the road if self.keys[self.HANDBRAKE] and self.speed > 200 and random.randint(1, 5) != 4: self.trace() # # the car go foward force_x = math.cos(body.angle) * self.force force_y = math.sin(body.angle) * self.force if self.keys[self.GO]: body.apply_force((force_x, force_y), (0, 0)) if self.keys[self.BRAKE] or \ (self.keys[self.HANDBRAKE] and self.speed < 100): self.brake(0.8) # # handle friction if body.velocity != (0, 0): self.brake(0.98) # TOO DIRTY! if self.speed > 600: self.brake(0.97) # # update camera # TODO WRAP it and make it more fluent. # TODO Rotate the camera according to the direction of the velocity if self.isMainPlayer: self.adjustCamera() # # save the last position self.last_pos = (self.x, self.y) # # Update the info to the server side if self.isOLPlay and self.isMainPlayer: key_data = json.dumps(['keys', self.keys]) olctlhub.send(key_data) # and adjust all info. if lantency to much then reject physical_data = json.dumps(['adjust-physical', [self.x, self.y, body.angle, tuple(body.velocity)]]) olctlhub.send(physical_data) if self.isMainPlayer: pass #self.log.info(self.speed) body.angular_velocity *= 0.9
def __call__(self, world, actor, interval): mouse = serge.engine.CurrentEngine().getMouse() if mouse.isClicked(serge.input.M_LEFT): for button in mouse.getActorsUnderMouse(world): self.log.info('button type:'+button.tag) if button.tag == 'list-item': # join in a room button.hightlight() olctlhub.send(['join-room', int(button.name)]) else: if button.name == 'refresh': olctlhub.send(['view-rooms']) olctlhub.send(['my-room']) elif button.name == 'new': olctlhub.send(['new-room'] ) olctlhub.send(['view-rooms']) elif button.name == 'start': olctlhub.send(['start']) elif button.name == 'practice': mainscreen.main(0, None) elif button.name == 'play-online': waitingscreen.main() elif button.name == 'inc': button.control.inc() elif button.name == 'dec': button.control.dec()
def updateActor(self, interval, world): """ Update the car's speed and direction """ # # Update forces and aquire speed body = self.physical_conditions.body body.reset_forces() # # Stop spinning if not self.keys[self.HANDBRAKE]: body.angular_velocity *= 0.8 self.speed = math.sqrt(body.velocity[0] ** 2 + body.velocity[1] ** 2) else: body.angular_velocity *= 0.9 self.speed = math.sqrt(body.velocity[0] ** 2 + body.velocity[1] ** 2) # # Handle turn left or turn right if self.keys[self.LEFT] and self.speed > 20: #body.angle -= 0.05 #if not self.keys[self.HANDBRAKE]: temp_angle = body.angle - math.pi / 2 body.apply_impulse( (math.cos(temp_angle) * (self.speed * 0.1), math.sin(temp_angle) * (self.speed * 0.1)), (math.cos(body.angle) * 6, math.sin(body.angle) * 6)) self.brake(0.99) """ else: # The car slip body.angle -= 0.09 """ if self.keys[self.RIGHT] and self.speed > 20: #body.angle += 0.05 #if not self.keys[self.HANDBRAKE]: temp_angle = body.angle + math.pi / 2 body.apply_impulse( (math.cos(temp_angle) * (self.speed * 0.1), math.sin(temp_angle) * (self.speed * 0.1)), (math.cos(body.angle) * 6, math.sin(body.angle) * 6)) self.brake(0.99) """ else: # The car slip body.angle += 0.09 """ # # Leave trace on the road if self.keys[self.HANDBRAKE] and self.speed > 200 and random.randint(1, 5) != 4: self.trace() # # the car go foward force_x = math.cos(body.angle) * self.force force_y = math.sin(body.angle) * self.force if self.keys[self.GO]: body.apply_force((force_x, force_y), (0, 0)) # # the car braked by handbrake if (self.keys[self.HANDBRAKE] and self.speed < 150): self.brake(0.8) # # the car go backward if self.keys[self.BACK]: body.apply_force((-force_x / 2, -force_y / 2), (0, 0)) # # handle friction if body.velocity != (0, 0): self.brake(0.98) if self.speed > 600: self.brake(0.98) if self.isMainPlayer: # # update camera # TODO WRAP it and make it more fluent. # TODO Rotate the camera according to the direction of the velocity self.adjustCamera() # # Check lap x1 = self.last_pos[0] y1 = self.last_pos[1] x2 = self.x y2 = self.y x3 = self.target_a[0] y3 = self.target_a[1] x4 = self.target_b[0] y4 = self.target_b[1] # 1-2 & 1-3, 1-2 & 1-4 # 3-4 & 3-1 3-4 & 3-2 if ((x1 - x2) * (y1 - y3) - (x1 - x3) * (y1 - y2)) * ((x1 - x2) * (y1 - y4) - (x1 - x4) * (y1 - y2)) <= 0 and \ ((x3 - x4) * (y3 - y1) - (x3 - x1) * (y3 - y4)) * ((x3 - x4) * (y3 - y2) - (x3 - x2) * (y3 - y4)) <= 0: if y1 - y2 < 0: self.lap -= 1 else: self.lap += 1 if self.lap == track.lap_num: self.broadcaster.processEvent((common.E_WIN_GAME, self)) else: world.msgr.displayMsg(str(self.lap + 1)+'/'+str(track.lap_num)+' lap', 2) # # save the last position self.last_pos = (self.x, self.y) self.last_angle = body.angle # # Update the info to the server side if self.isOLPlay and self.isMainPlayer: key_data = ['keys', self.keys] olctlhub.send(key_data) # and adjust all info. if lantency too much then reject physical_data = ['adjust-physical', [self.x, self.y, body.angle, tuple(body.velocity)]] olctlhub.send(physical_data)