Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2013
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.22 KB | None | 0 0
  1. from PodSixNet.Connection import connection, ConnectionListener
  2. import threading, time, sys, pygame, random
  3.  
  4. global t1, t2, ready
  5. ready = False
  6. class Client(ConnectionListener):
  7.     global players, ready
  8.     players = []
  9.     def getPlayers(self):
  10.         return self.players
  11.     def __init__(self, host, port):
  12.         self.Connect((host, port))
  13.  
  14.         nick = "Anonymous"
  15.         connection.Send({"action": "nickname", "nickname": nick})
  16.  
  17.     def Loop(self):
  18.         connection.Pump()
  19.         self.Pump()
  20.     def sendData(self, data):
  21.         connection.Send(data)
  22.     #######################################
  23.     ### Network event/message callbacks ###
  24.     #######################################
  25.  
  26.     def Network_players(self, data):
  27.         global players
  28.         # Recieved list of players.
  29.     def Network_connected(self, data):
  30.         print "You are now connected to the server"
  31.     def Network_ready(self, data):
  32.         ready = True
  33.         print "ready"
  34.  
  35.     def Network_error(self, data):
  36.         print 'error:', data['error'][1]
  37.         connection.Close()
  38.  
  39.     def Network_disconnected(self, data):
  40.         print 'Server disconnected'
  41.         exit()
  42. def startClient():
  43.     global c
  44.     while 1:
  45.         # Try except stops exception when quitting
  46.         try:
  47.             c.Loop()
  48.             time.sleep(0.001)
  49.         except:
  50.             pass
  51. def connect():
  52.     global c, t1, t2
  53.     c = Client("localhost", 12345)
  54.     t1 = threading.Thread(target=startClient)
  55.     t1.start()
  56.     #t1.join()
  57.     t2 = threading.Thread(target=start)
  58.     t2.start()
  59.     t2.join()
  60.    
  61.  
  62. def start():
  63.     pygame.init()
  64.     global dir_x, dir_y, ready
  65.     width = 640
  66.     height = 400
  67.     x = width / 2
  68.     y = height / 2
  69.     dir_x = 0
  70.     dir_y = -1
  71.     screen = pygame.display.set_mode((width, height))
  72.     BLACK = (0, 0, 0)
  73.     clock = pygame.time.Clock()
  74.     class Food:
  75.         def __init__(self, s):
  76.             self.foodheight = random.randint(10, 21)
  77.             self.screen = s
  78.             self.x = random.randint(50, self.screen.get_width() - 50)
  79.             self.y = random.randint(50, self.screen.get_height() - 50)
  80.             self.eaten = False
  81.         def draw(self):
  82.             pygame.draw.rect(self.screen, (0, 255, 0), (self.x, self.y, self.foodheight, self.foodheight))
  83.         def setEaten(self, eaten):
  84.             self.eaten = eaten
  85.         def isEaten(self):
  86.             return self.eaten
  87.         def didHit(self, point):
  88.             if point[0] < self.x or point[0] > self.x + self.foodheight:
  89.                 return False
  90.             elif point[1] < self.y or point[1] > self.y + self.foodheight:
  91.                 return False
  92.             else:
  93.                 return True
  94.     def getRand():
  95.         return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
  96.     def do(x, y, dir_x, dir_y):
  97.         global ready
  98.         food = Food(screen)
  99.         food2 = Food(screen)
  100.         points = []
  101.         length = 50
  102.         count = 0
  103.         tempcount = 0
  104.         foodEaten = 0
  105.         foodJustEaten = False
  106.         rateOfRemoval = 15
  107.         lengthReached = False
  108.         death = False
  109.         #highscore = 0
  110.         events = []
  111.         myfont = pygame.font.SysFont("monospace", 15)
  112.         bigFont = pygame.font.SysFont("monospace", 36)
  113.         while 1:
  114.             events = pygame.event.get()
  115.             if not ready:
  116.                 screen.fill(BLACK)
  117.                 deathLabel = bigFont.render("Waiting", 1, (255, 255, 255))
  118.                 screen.blit(deathLabel, (width / 2 - 40, height / 2 - 40))
  119.             else:
  120.                 if len(points) >= length and not lengthReached:
  121.                     lengthReached = True
  122.                 if lengthReached and len(points) <= 5:
  123.                     death = True
  124.                 #    highscore = count - 720
  125.                 tempscore = 0 if count < 720 else count - 720
  126.                 label = myfont.render("(" + str(x) + ", " + str(y) + ")" + "        Score: " + str(tempscore), 1, (255, 255, 255))
  127.                 foodLabel = myfont.render(str(len(points)) + ", " + str(count) + " " + str(foodEaten) + ", " + str(len(points)) + ", " + str(rateOfRemoval), 1, (255, 255, 255))
  128.                 screen.fill(BLACK)
  129.                 screen.blit(label, (250, 10))
  130.                 screen.blit(foodLabel, (50, 10))
  131.                 if food.isEaten():
  132.                     food = Food(screen)
  133.                     tempcount = length
  134.                     length += 80
  135.                     foodEaten += 1
  136.                     foodJustEaten = True
  137.                 if food2.isEaten():
  138.                     food2 = Food(screen)
  139.                     tempcount = length
  140.                     length += 80
  141.                     foodEaten += 1
  142.                     foodJustEaten = True
  143.                 if tempcount <= length:
  144.                     tempcount += 1
  145.                 elif foodEaten > 5 and foodJustEaten:
  146.                     if foodEaten % 2 == 0:
  147.                         if rateOfRemoval > 6:
  148.                             rateOfRemoval -= 1
  149.                             foodJustEaten = False
  150.                 food.draw()
  151.                 food2.draw()
  152.                 count += 1
  153.                 if count % rateOfRemoval == 0 and lengthReached:
  154.                     del points[0]
  155.                     length -= 2
  156.                 x += dir_x
  157.                 y += dir_y
  158.                 points.append((x, y))
  159.                 if len(points) > length:
  160.                     for i in range(0, 1):
  161.                         del points[i]
  162.                     # del points[0]
  163.                 if x <= 0:
  164.                     x = width
  165.                 elif x >= width:
  166.                     x = 0
  167.                 elif y <= 0:
  168.                     y = height
  169.                 elif y >= height:
  170.                     y = 0
  171.                 color = getRand()
  172.                 for point in points:
  173.                     screen.set_at(point, color)
  174.                 screen.set_at((x, y), color)
  175.                 for event in events:
  176.                     if event.type == pygame.KEYDOWN:
  177.                         if event.key == pygame.K_w:
  178.                             if dir_y == 1:
  179.                                 break
  180.                             dir_x = 0
  181.                             dir_y = -1
  182.                         elif event.key == pygame.K_s:
  183.                             if dir_y == -1:
  184.                                 break
  185.                             dir_x = 0
  186.                             dir_y = 1
  187.                         elif event.key == pygame.K_a:
  188.                             if dir_x == 1:
  189.                                 break
  190.                             dir_x = -1
  191.                             dir_y = 0
  192.                         elif event.key == pygame.K_d:
  193.                             if dir_x == -1:
  194.                                 break
  195.                             dir_x = 1
  196.                             dir_y = 0
  197.                         elif event.key == pygame.K_q:
  198.                             if dir_x == 1 and dir_y == 1:
  199.                                 break
  200.                             dir_x = -1
  201.                             dir_y = -1
  202.                         elif event.key == pygame.K_e:
  203.                             if dir_x == -1 and dir_y == 1:
  204.                                 break
  205.                             dir_x = 1
  206.                             dir_y = -1
  207.                         elif event.key == pygame.K_z:
  208.                             if dir_x == 1 and dir_y == -1:
  209.                                 break
  210.                             dir_x = -1
  211.                             dir_y = 1
  212.                         elif event.key == pygame.K_c:
  213.                             if dir_x == -1 and dir_y == -1:
  214.                                 break
  215.                             dir_x = 1
  216.                             dir_y = 1
  217.                 if food.didHit((x, y)):
  218.                     food.setEaten(True)
  219.             for event in events:
  220.                 if event.type == pygame.QUIT:
  221.                     return
  222.                 if event.type == pygame.MOUSEBUTTONDOWN and death:
  223.                     do(x, y, dir_x, dir_y)
  224.                     return
  225.             pygame.display.flip()
  226.             clock.tick(150)
  227.     do(x, y, dir_x, dir_y)
  228. connect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement