import pygame, random from socket import * import select def initialize_constants(): """ This function sets some global constants (i.e., the values of these global variables never change). """ pygame.init() global UDP_sock, buf my_host = '' my_port = 3001 my_addr = (my_host,my_port) buf = 1024 UDP_sock = socket(AF_INET,SOCK_DGRAM) UDP_sock.bind(my_addr) # set up the display window - only need to know size global width, height width = 800 height = 600 # load the ballon picture - again: to get the size global balloon balloon = pygame.image.load("red_balloon.gif") def move_and_bounce_object(dt, x, y, speed_x, speed_y, obj_width, obj_height): """ This function calculates an object's new position based on its old position, the time that has passed since the last update, and its speed. It also makes sure that the object bounces off the edge of the screen. """ x = x + dt * speed_x if (x < 0): x = 0 speed_x = - speed_x elif (x > width - obj_width): x = width - obj_width speed_x = - speed_x y = y + dt * speed_y if (y < 0): y = 0 speed_y = - speed_y elif (y > height - obj_height): y = height - obj_height speed_y = - speed_y return x, y, speed_x, speed_y def clicked_image(mouse_x, mouse_y, x, y, w, h): if mouse_x >= x and mouse_x <= x+w and mouse_y >= y and mouse_y <= y+h: return True else: return False def make_scores_msg (client_d): string = "scores" for (name, score) in client_d.values(): string = string + " "+name+" " +str(score) return string def run_game(): # initialize the balloons' positions and speeds b_x = 100 b_y = 100 b_speed_x = 0.2 b_speed_y = 0.2 b_w = balloon.get_width() b_h = balloon.get_height() b2_x = 300 b2_y = 300 b2_speed_x = 0.1 b2_speed_y = 0.2 # dictionary of clients # address (IP, port) will be keys, values will be (name, score) client_d = {} clock = pygame.time.Clock() keepGoing = True while (keepGoing): dt = clock.tick(60) ### handle messages from the clients [in_msgs, out, err] = select.select([UDP_sock], [], [], 0) if len(in_msgs) > 0: received_string, client = UDP_sock.recvfrom(buf) received_string = received_string.strip() received_list = received_string.split(" ") if received_list[0] == "click": if clicked_image (int(received_list[1]), int(received_list[2]), b_x, b_y, b_w, b_h): client_d[client] = (client_d[client][0], client_d[client][1] + 1) UDP_sock.sendto("hit "+str(client_d[client][1]), client) b_x = random.randint(0,width) b_y = random.randint(0,height) elif received_list[0] == "connect": name = received_list[1] print "new connection from "+name+" at "+str(client) client_d[client] = (name,0) elif received_list[0] == "disconnect": print client_d[client][0] + " has disconnected" del client_d[client] ### Update the positions and speeds of the balloons. b_x, b_y, b_speed_x, b_speed_y = move_and_bounce_object(dt, b_x, b_y, b_speed_x, b_speed_y, b_w, b_h) b2_x, b2_y, b2_speed_x, b2_speed_y = move_and_bounce_object(dt, b2_x, b2_y, b2_speed_x, b2_speed_y, b_w, b_h) ### send information about position and scores to each client pos = "position "+str(int(b_x))+" "+str(int(b_y))+" "+str(int(b2_x))+" "+str(int(b2_y)) scores = make_scores_msg(client_d) for addr in client_d.keys(): UDP_sock.sendto(pos, addr) UDP_sock.sendto(scores, addr) def done(): pygame.quit() def main(): initialize_constants() run_game() done() main()