def poll(self): p = self.model.pellets[0] # always target the first pellet b = self.model.players[self.model.myname] for event in get_pygame_events(): if event.type == QUIT: self.model.do_close() exit() if event.type == KEYDOWN: key = event.key if key == K_ESCAPE: self.model.do_close() exit() if p[0] > b[0]: cmd = 'right' elif p[0] + p[2] <= b[0]: # p[2] to avoid stuttering left-right movement cmd = 'left' elif p[1] > b[1]: cmd = 'down' else: cmd = 'up' msg = {'input': cmd} self.model.do_send(msg)
global borders, pellets, players, myname borders = [make_rect(b) for b in data['borders']] pellets = [make_rect(p) for p in data['pellets']] players = {name: make_rect(p) for name, p in data['players'].items()} myname = data['myname'] client = Client('localhost', 8888) # connect asynchronously valid_inputs = {K_UP: 'up', K_DOWN: 'down', K_LEFT: 'left', K_RIGHT: 'right'} while 1: poll() # push and pull network messages # send valid inputs to the server for event in get_pygame_events(): if event.type == QUIT: exit() if event.type == KEYDOWN: key = event.key if key == K_ESCAPE: exit() elif key in valid_inputs: msg = {'input': valid_inputs[key]} client.do_send(msg) # draw everything screen.fill((0, 0, 64)) # dark blue [draw_rect(screen, (0, 191, 255), b) for b in borders] # deep sky blue [draw_rect(screen, (255, 192, 203), p) for p in pellets] # shrimp for name, p in players.items():
#print line li = GObject(line['pos'], line['shape'], line['color'], line['linewidth']) li.draw(view) for dot in data['board']['dots']: #print dot do = GObject(dot['pos'], dot['shape'], dot['color'], dot['linewidth']) do.draw(view) for button in data['board']['buttons']: #print button bu = Button(button['pos'], button['shape'], button['color'], button['linewidth'], button['text'], button['textColor'], button['textSize']) bu.draw(view) thisip = IPwindow.get_ip(view) client = Client(thisip, 8888) # connect asynchronously while 1: poll() for event in get_pygame_events(): if event.type == QUIT: exit() if event.type == MOUSEBUTTONDOWN: msg = {'input': event.pos} client.do_send(msg) pygame.display.update() clock.tick(FRAMERATE)