-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
155 lines (126 loc) · 4.69 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import socket
import pickle
import pygame
import level
import tiles
import physical_object
import ship
import turret
import constants
import network
from network import Message
from network import Type
import struct
from pygame.rect import Rect
import play_sound
from pygame import mixer
from pygame.mixer import Sound
#from pygame import font
#initialize sound objects and set their volumes
startSFX = pygame.mixer.Sound(constants.START_MUSIC)
startSFX.set_volume(0.7)
endSFX = pygame.mixer.Sound(constants.END_MUSIC)
endSFX.set_volume(0.7)
backgroundSFX = pygame.mixer.Sound(constants.BACKGROUND_MUSIC)
backgroundSFX.set_volume(0.5)
# ready the display
screen = pygame.display.set_mode((constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT))
pygame.display.set_caption("Tyrian Defense SERVER")
# load introscreen
introscreen = pygame.image.load('images/pyrian.png')
play_sound.PlaySounds(startSFX, 0)
screen.blit(introscreen, introscreen.get_rect())
# Display some text
pygame.font.init()
arial = pygame.font.match_font('doesNotExist,Arial')
font = pygame.font.Font(arial, 36)
text = font.render("Waiting for client", 1, (10, 10, 10))
textpos = text.get_rect()
textpos.centerx = screen.get_rect().centerx
screen.blit(text, textpos)
pygame.display.flip()
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
s.bind( ('', constants.PORT) )
s.listen( 1 )
conn, addr = s.accept()
print 'Connection from', addr
clock = pygame.time.Clock()
level = level.Level()
removeQueue = []
lastNetworkID = 0
physical_object.init(True)
theship = ship.Ship((constants.SCREEN_WIDTH/2, level.rect.height - 60), level)
level.physicalObjects.append(theship)
# sprinkle some targets across the map purely for testing physics
# this should be removed from the server code eventually
#level.physicalObjects.append(turret.Turret((170, 4475), level))
#level.physicalObjects.append(turret.Turret((250, 4275), level))
#level.physicalObjects.append(turret.Turret((450, 4425), level))
leveldata = pickle.dumps( level.terrian, constants.PROTOCOL )
network.sendData( conn, leveldata )
scrollPosition = level.rect.height - constants.SCREEN_HEIGHT
movedlevelrect = level.rect.move(0,-level.yoffset)
running = True
# create the background music and send it to the class to play the sounds
#pygame.time.wait(310)
play_sound.PlaySounds(backgroundSFX, 1)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if not theship.handle_event(event):
pass #todo: handle events the ship isn't interested in
level.step(scrollPosition)
# do some network stuff
for o in level.physicalObjectsExternalRemoveList:
removeQueue.append(o.networkID)
screen.fill(pygame.Color('black'))
movedlevelrect.y = -scrollPosition;
screen.blit(level.image, movedlevelrect)
for o in level.physicalObjects:
# commented lines are for physics testing
#screen.fill((255, 255, 255), Rect((o.rect.x, o.rect.y - scrollPosition),(o.rect.width, o.rect.height)))
#screen.fill((255, 0, 0), Rect((o.physicsRect.x, o.physicsRect.y - scrollPosition), (o.physicsRect.width, o.physicsRect.height)))
screen.blit(o.image, Rect((o.rect.x, o.rect.y - scrollPosition),(o.rect.width, o.rect.height)), o.area)
#if not at the end of the level, update the display and scroll the level down
if scrollPosition != 0:
pygame.display.flip()
clock.tick(60)
scrollPosition -= constants.SCROLL_RATE
else:
winscreen = pygame.image.load('images/win.png')
screen.blit(winscreen, winscreen.get_rect())
pygame.display.flip()
#Network:
network.sendIntData( conn, Message.SCROLLSYNC )
network.sendIntData( conn, scrollPosition )
for netid in removeQueue:
network.sendIntData( conn, Message.DESTROYOBJ )
network.sendIntData( conn, netid )
removeQueue = []
for o in level.physicalObjects[:]:
if o.networkID == None:
network.sendIntData( conn, Message.NEWOBJ )
lastNetworkID = lastNetworkID + 2 # stick to even numbers
o.networkID = lastNetworkID
network.sendData( conn, struct.pack( "ii", o.typ, o.networkID ) )
network.sendIntData( conn, Message.OBJSYNC )
network.sendData( conn, struct.pack( "iffff", o.networkID, o.getX(), o.getY(), o.getVX(), o.getVY() ) )
message = network.receiveIntData( conn, True )
obj = None
while message != None:
message, = message
if message == Message.NEWOBJ:
typ, netid = struct.unpack( "ii", network.receiveData( conn ) )
if typ == Type.TURRET:
obj = turret.Turret((0,0), level)
obj.networkID = netid
level.physicalObjects.append(obj)
elif message == Message.OBJSYNC:
netid, x, y, vx, vy = struct.unpack( "iffff", network.receiveData( conn ) )
#obj = physicalObjects[ netid ]
obj.setX( x )
obj.setY( y )
obj.v_x, obj.v_y = (vx, vy)
message = network.receiveIntData( conn, True )
conn.close()