-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.py
More file actions
171 lines (139 loc) · 5.09 KB
/
Game.py
File metadata and controls
171 lines (139 loc) · 5.09 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import CommonLayers
import cocos
from cocos.scenes.transitions import FadeTRTransition
import pyglet
import sys
class GameController(object):
"""
"""
default_title = "Cocosoids"
default_window_width = 1366
default_window_height = 700
playLayerClass = CommonLayers.PlayLayer
def __init__(self):
""" """
super( GameController, self ).__init__()
self.game_layer = self.playLayerClass()
self.game_layer.addAsteroids(3)
self.ui_layer = CommonLayers.UILayer()
self.ui_layer.add(self.game_layer)
self.game_scene = cocos.scene.Scene(self.ui_layer)
def start(self):
""" """
cocos.director.director.replace(FadeTRTransition(
self.get_scene(), 2))
self.game_layer.do(CommonLayers.PlayLayerAction())
self.game_layer.do(CommonLayers.InteractivePlayLayerAction())
self.game_layer.addPlayer(CommonLayers.PlayLayer.ownID)
def get_scene(self):
""" """
return self.game_scene
class IntroController(object):
"""
"""
def __init__(self):
""" """
super( IntroController, self ).__init__()
director_width = GameController.default_window_width
director_height = GameController.default_window_height
caption = GameController.default_title + ' ' + \
CommonLayers.PlayLayer.ownID
window = cocos.director.director.init(
director_width, director_height,
caption = caption, fullscreen=False)
print(window.get_viewport_size())
intro_layer = CommonLayers.PlayLayer()
intro_layer.anchor_x = director_width * 0.5
intro_layer.anchor_y = director_height * 0.5
intro_layer.addAsteroids(3)
intro_menu = IntroMenu(self)
intro_layer.add(intro_menu)
self.intro_scene = cocos.scene.Scene(intro_layer)
def run(self, host=None, port=None):
""" """
self.host = host
self.port = port
cocos.director.director.set_show_FPS(True)
cocos.director.director.run (self.intro_scene)
def on_join_game( self ):
""" """
##################################################################
# NEW TO SUPPORT MULTIPLAYER
gameController = ClientGameController()
gameController.start(self.host, self.port)
# End NEW TO SUPPORT MULTIPLAYER
##################################################################
def on_host_game( self ):
""" """
gameController = ServerGameController() # TO SUPPORT MULTIPLAYER
gameController.start()
def on_name( self, value ):
""" """
self.player_name = value
def on_quit( self ):
""" """
pyglet.app.exit()
class IntroMenu(cocos.menu.Menu):
"""
"""
def __init__( self, game ):
""" """
super( IntroMenu, self ).__init__()
self.game = game
self.font_item = {
'font_name': 'Arial',
'font_size': 32,
'bold': True,
'color': (220, 200, 220, 100),
}
self.font_item_selected = {
'font_name': 'Arial',
'font_size': 42,
'bold': True,
'color': (255, 255, 255, 255),
}
menuItems = []
menuItems.append( cocos.menu.MenuItem('Join Game',
self.game.on_join_game ) )
menuItems.append( cocos.menu.MenuItem('Host Game',
self.game.on_host_game ) )
menuItems.append( cocos.menu.EntryMenuItem('Name:',
self.game.on_name,
CommonLayers.PlayLayer.ownID) )
menuItems.append( cocos.menu.MenuItem('Quit', self.game.on_quit ) )
self.create_menu( menuItems )
##################################################################
# NEW TO SUPPORT MULTIPLAYER
import GameChatServer
import GameChatClient
class ServerGameController(GameController):
"""
"""
def start(self):
""" """
cocos.director.director.replace(FadeTRTransition(
self.get_scene(), 2))
self.game_layer.do(GameChatServer.GameChatServerNetworkAction())
self.game_layer.do(CommonLayers.PlayLayerAction())
class ClientGameController(GameController):
"""
"""
playLayerClass = GameChatClient.ClientPlayLayer
def start(self, host, port):
""" """
cocos.director.director.replace(FadeTRTransition(
self.get_scene(), 2))
self.game_layer.do(GameChatClient.GameChatClientNetworkAction(
host, port))
self.game_layer.do(CommonLayers.InteractivePlayLayerAction())
self.game_layer.addPlayer(CommonLayers.PlayLayer.ownID)
# End NEW TO SUPPORT MULTIPLAYER
##################################################################
if __name__ == "__main__":
controller = IntroController()
if len(sys.argv) == 2:
host, port = sys.argv[1].split(":")
print(host, port)
controller.run(host, int(port))
else:
controller.run()