'''
Created on 2012-7-25
@author: gaoyang
@group: game-netease
@copyright: This program is free software. You can redistribute and modify it under the terms of the GNU General Public License as published by the Free Software Foundation.
'''
import pickle, pygame, random, sys, threading, time
import SnakeData, SnakeDld, SnakeFood, SnakeTcp, SnakeObject
from pygame.locals import *
HOST = 'localhost'
PORT = 12345
THREAD_STOP = False
DOWNLOADED = False
class SnakeP1(SnakeObject.Snake):
def score(self, score, level):
global screen
font = pygame.font.SysFont('calibri', 28)
if len(str(score)) < 2:
scoreStr = ' '+str(score)
else:
scoreStr = str(score)
game_info = font.render(scoreStr, True, SnakeData.WHITE, SnakeData.BLACK)
screen.blit(game_info, (540, 80))
if len(str(level)) < 2:
levelStr = ' '+str(level)
else:
levelStr = str(level)
game_info = font.render(levelStr, True, SnakeData.WHITE, SnakeData.BLACK)
screen.blit(game_info, (540, 130))
class SnakeP2(SnakeObject.Snake):
def score(self, score, level):
global screen
font = pygame.font.SysFont('calibri', 28)
if len(str(score)) < 2:
scoreStr = ' '+str(score)
else:
scoreStr = str(score)
game_info = font.render(scoreStr, True, SnakeData.WHITE, SnakeData.BLACK)
screen.blit(game_info, (540, 315))
if len(str(level)) < 2:
levelStr = ' '+str(level)
else:
levelStr = str(level)
game_info = font.render(levelStr, True, SnakeData.WHITE, SnakeData.BLACK)
screen.blit(game_info, (540, 365))
class dldThread(threading.Thread):
global THREAD_STOP, DOWNLOADED
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global DOWNLOADED
while not THREAD_STOP:
SnakeDld.main()
DOWNLOADED = True
time.sleep(3)
def stop(self):
global THREAD_STOP
THREAD_STOP = True
def sendMsg(msg):
client = SnakeTcp.snakeTcp()
client.initClient(HOST, PORT)
client.clientSend(pickle.dumps(msg))
temp = client.clientRecv()
data = pickle.loads(temp)
client.close()
return data
def terminate():
global THREAD_STOP
THREAD_STOP = True
pygame.quit()
sys.exit()
def init():
global screen
pygame.init()
screen = pygame.display.set_mode(SnakeData.SCREEN_SIZE, 0, 32)
logo = pygame.image.load('logo.png').convert_alpha()
pygame.display.set_icon(logo)
pygame.display.set_caption("SnakeX")
font = pygame.font.SysFont('calibri', 40)
game_info = font.render('SnakeX', True, SnakeData.RED)
screen.blit(game_info, (240, 60))
font = pygame.font.SysFont('calibri', 32)
game_info = font.render('1. Arrow key: direction', True, SnakeData.WHITE)
screen.blit(game_info, (150, 150))
game_info = font.render('2. ESC: quit/exit', True, SnakeData.WHITE)
screen.blit(game_info, (150, 200))
game_info = font.render('3. P: pause/start', True, SnakeData.WHITE)
screen.blit(game_info, (150, 250))
game_info = font.render('!!!Press enter to start!!!', True, SnakeData.GREEN)
screen.blit(game_info, (130, 350))
pygame.display.update()
while True:
event = pygame.event.wait()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
terminate()
if event.key == pygame.K_RETURN:
break
screen.fill((0, 0, 0), (0, 0, 640, 480))
pygame.draw.rect(screen, SnakeData.GREEN, (10 , 10 , 410, 460), SnakeData.BORDER)
pygame.draw.rect(screen, SnakeData.GREEN, (440, 10 , 190, 225), SnakeData.BORDER)
pygame.draw.rect(screen, SnakeData.GREEN, (440, 245, 190, 225), SnakeData.BORDER)
font = pygame.font.SysFont('calibri', 42)
player = 'PLAYER-' + str(SnakeData.P1_NUM+1)
game_info = font.render(player, True, SnakeData.PLAYER_COLOR[SnakeData.P1_NUM])
screen.blit(game_info, (460, 20))
font = pygame.font.SysFont('calibri', 24)
game_info = font.render('SCORE: ', True, SnakeData.RED)
screen.blit(game_info, (460, 80))
game_info = font.render('LEVEL: ', True, SnakeData.RED)
screen.blit(game_info, (460, 130))
game_info = font.render('STATUS:', True, SnakeData.RED)
screen.blit(game_info, (460, 180))
font = pygame.font.SysFont('calibri', 42)
player = 'PLAYER-' + str(SnakeData.P2_NUM+1)
game_info = font.render(player, True, SnakeData.PLAYER_COLOR[SnakeData.P2_NUM])
screen.blit(game_info, (460, 255))
font = pygame.font.SysFont('calibri', 24)
game_info = font.render('SCORE: ', True, SnakeData.RED)
screen.blit(game_info, (460, 315))
game_info = font.render('LEVEL: ', True, SnakeData.RED)
screen.blit(game_info, (460, 365))
game_info = font.render('STATUS:', True, SnakeData.RED)
screen.blit(game_info, (460, 415))
if len(str(SnakeData.P1_SCORE)) < 2:
scoreStr = ' '+str(SnakeData.P1_SCORE)
else:
scoreStr = str(SnakeData.P1_SCORE)
game_info = font.render(scoreStr, True, SnakeData.WHITE, SnakeData.BLACK)
screen.blit(game_info, (540, 80))
if len(str(SnakeData.P1_LEVEL)) < 2:
levelStr = ' '+str(SnakeData.P1_LEVEL)
else:
levelStr = str(SnakeData.P1_LEVEL)
game_info = font.render(levelStr, True, SnakeData.WHITE, SnakeData.BLACK)
screen.blit(game_info, (540, 130))
game_info = font.render(' RUN', True, SnakeData.WHITE, SnakeData.BLACK)
screen.blit(game_info, (540, 180))
if len(str(SnakeData.P2_SCORE)) < 2:
scoreStr = ' '+str(SnakeData.P2_SCORE)
else:
scoreStr = str(SnakeData.P2_SCORE)
game_info = font.render(scoreStr, True, SnakeData.WHITE, SnakeData.BLACK)
screen.blit(game_info, (540, 315))
if len(str(SnakeData.P2_LEVEL)) < 2:
levelStr = ' '+str(SnakeData.P2_LEVEL)
else:
levelStr = str(SnakeData.P2_LEVEL)
game_info = font.render(levelStr, True, SnakeData.WHITE, SnakeData.BLACK)
screen.blit(game_info, (540, 365))
game_info = font.render(' RUN', True, SnakeData.WHITE, SnakeData.BLACK)
screen.blit(game_info, (540, 415))
def start():
global screen, DOWNLOADED
P1 = SnakeP1(SnakeData.P1_INI_DRCT, SnakeData.P1_INI_POS[:], SnakeData.P1_LEVEL)
P2 = SnakeP2(SnakeData.P2_INI_DRCT, SnakeData.P2_INI_POS[:], SnakeData.P2_LEVEL)
food = SnakeFood.Food((-1, -1))
lst = P2.pos[:]
lst.append(2)
sendMsg(lst[:])
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
terminate()
if event.key == pygame.K_LEFT:
lst = [20]
sendMsg(lst[:])
break
elif event.key == pygame.K_RIGHT:
lst = [21]
sendMsg(lst[:])
break
elif event.key == pygame.K_UP:
lst = [22]
sendMsg(lst[:])
break
elif event.key == pygame.K_DOWN:
lst = [23]
sendMsg(lst[:])
break
lst = [24]
data = sendMsg(lst[:])
if len(data) > 0:
P2.pos = data[0][:]
food.pos = data[1]
p2score = data[2]
p2level = data[3]
P1.pos = data[4][:]
food.pos = data[5]
p1score = data[6]
p1level = data[7]
评论7
最新资源