Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PodSixNet.Connection import connection, ConnectionListener
- import threading, time, sys, pygame, random
- global t1, t2, ready
- ready = False
- class Client(ConnectionListener):
- global players, ready
- players = []
- def getPlayers(self):
- return self.players
- def __init__(self, host, port):
- self.Connect((host, port))
- nick = "Anonymous"
- connection.Send({"action": "nickname", "nickname": nick})
- def Loop(self):
- connection.Pump()
- self.Pump()
- def sendData(self, data):
- connection.Send(data)
- #######################################
- ### Network event/message callbacks ###
- #######################################
- def Network_players(self, data):
- global players
- # Recieved list of players.
- def Network_connected(self, data):
- print "You are now connected to the server"
- def Network_ready(self, data):
- ready = True
- print "ready"
- def Network_error(self, data):
- print 'error:', data['error'][1]
- connection.Close()
- def Network_disconnected(self, data):
- print 'Server disconnected'
- exit()
- def startClient():
- global c
- while 1:
- # Try except stops exception when quitting
- try:
- c.Loop()
- time.sleep(0.001)
- except:
- pass
- def connect():
- global c, t1, t2
- c = Client("localhost", 12345)
- t1 = threading.Thread(target=startClient)
- t1.start()
- #t1.join()
- t2 = threading.Thread(target=start)
- t2.start()
- t2.join()
- def start():
- pygame.init()
- global dir_x, dir_y, ready
- width = 640
- height = 400
- x = width / 2
- y = height / 2
- dir_x = 0
- dir_y = -1
- screen = pygame.display.set_mode((width, height))
- BLACK = (0, 0, 0)
- clock = pygame.time.Clock()
- class Food:
- def __init__(self, s):
- self.foodheight = random.randint(10, 21)
- self.screen = s
- self.x = random.randint(50, self.screen.get_width() - 50)
- self.y = random.randint(50, self.screen.get_height() - 50)
- self.eaten = False
- def draw(self):
- pygame.draw.rect(self.screen, (0, 255, 0), (self.x, self.y, self.foodheight, self.foodheight))
- def setEaten(self, eaten):
- self.eaten = eaten
- def isEaten(self):
- return self.eaten
- def didHit(self, point):
- if point[0] < self.x or point[0] > self.x + self.foodheight:
- return False
- elif point[1] < self.y or point[1] > self.y + self.foodheight:
- return False
- else:
- return True
- def getRand():
- return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
- def do(x, y, dir_x, dir_y):
- global ready
- food = Food(screen)
- food2 = Food(screen)
- points = []
- length = 50
- count = 0
- tempcount = 0
- foodEaten = 0
- foodJustEaten = False
- rateOfRemoval = 15
- lengthReached = False
- death = False
- #highscore = 0
- events = []
- myfont = pygame.font.SysFont("monospace", 15)
- bigFont = pygame.font.SysFont("monospace", 36)
- while 1:
- events = pygame.event.get()
- if not ready:
- screen.fill(BLACK)
- deathLabel = bigFont.render("Waiting", 1, (255, 255, 255))
- screen.blit(deathLabel, (width / 2 - 40, height / 2 - 40))
- else:
- if len(points) >= length and not lengthReached:
- lengthReached = True
- if lengthReached and len(points) <= 5:
- death = True
- # highscore = count - 720
- tempscore = 0 if count < 720 else count - 720
- label = myfont.render("(" + str(x) + ", " + str(y) + ")" + " Score: " + str(tempscore), 1, (255, 255, 255))
- foodLabel = myfont.render(str(len(points)) + ", " + str(count) + " " + str(foodEaten) + ", " + str(len(points)) + ", " + str(rateOfRemoval), 1, (255, 255, 255))
- screen.fill(BLACK)
- screen.blit(label, (250, 10))
- screen.blit(foodLabel, (50, 10))
- if food.isEaten():
- food = Food(screen)
- tempcount = length
- length += 80
- foodEaten += 1
- foodJustEaten = True
- if food2.isEaten():
- food2 = Food(screen)
- tempcount = length
- length += 80
- foodEaten += 1
- foodJustEaten = True
- if tempcount <= length:
- tempcount += 1
- elif foodEaten > 5 and foodJustEaten:
- if foodEaten % 2 == 0:
- if rateOfRemoval > 6:
- rateOfRemoval -= 1
- foodJustEaten = False
- food.draw()
- food2.draw()
- count += 1
- if count % rateOfRemoval == 0 and lengthReached:
- del points[0]
- length -= 2
- x += dir_x
- y += dir_y
- points.append((x, y))
- if len(points) > length:
- for i in range(0, 1):
- del points[i]
- # del points[0]
- if x <= 0:
- x = width
- elif x >= width:
- x = 0
- elif y <= 0:
- y = height
- elif y >= height:
- y = 0
- color = getRand()
- for point in points:
- screen.set_at(point, color)
- screen.set_at((x, y), color)
- for event in events:
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_w:
- if dir_y == 1:
- break
- dir_x = 0
- dir_y = -1
- elif event.key == pygame.K_s:
- if dir_y == -1:
- break
- dir_x = 0
- dir_y = 1
- elif event.key == pygame.K_a:
- if dir_x == 1:
- break
- dir_x = -1
- dir_y = 0
- elif event.key == pygame.K_d:
- if dir_x == -1:
- break
- dir_x = 1
- dir_y = 0
- elif event.key == pygame.K_q:
- if dir_x == 1 and dir_y == 1:
- break
- dir_x = -1
- dir_y = -1
- elif event.key == pygame.K_e:
- if dir_x == -1 and dir_y == 1:
- break
- dir_x = 1
- dir_y = -1
- elif event.key == pygame.K_z:
- if dir_x == 1 and dir_y == -1:
- break
- dir_x = -1
- dir_y = 1
- elif event.key == pygame.K_c:
- if dir_x == -1 and dir_y == -1:
- break
- dir_x = 1
- dir_y = 1
- if food.didHit((x, y)):
- food.setEaten(True)
- for event in events:
- if event.type == pygame.QUIT:
- return
- if event.type == pygame.MOUSEBUTTONDOWN and death:
- do(x, y, dir_x, dir_y)
- return
- pygame.display.flip()
- clock.tick(150)
- do(x, y, dir_x, dir_y)
- connect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement