forked from unlimitedbacon/kfungus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·541 lines (467 loc) · 16.2 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#!/usr/bin/python2
#
# FUNGUS
#
# A modern clone of NetFungus. Sort of a combination between Tetris and Reversi,
# with networked multiplayer
__version__ = '0.3'
import kivy
kivy.require('1.8.0')
from random import randint
from kivy.app import App
from kivy.config import Config
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.scatter import Scatter
from kivy.uix.settings import SettingsWithSidebar
from kivy.uix.widget import Widget
from kivy.animation import Animation
from kivy.properties import ObjectProperty, StringProperty, BooleanProperty
from kivy.graphics import Color, Rectangle
# install_twisted_reactor must be called before importing and using the reactor
from kivy.support import install_twisted_reactor
install_twisted_reactor()
from twisted.internet import reactor
from tetrominoes import tetros
from settings import settings_json
from game import *
from net import *
# Set window to same resolution as Nexus 4
#Config.set('graphics','width','1280')
#Config.set('graphics','height','768')
server = 'kramerica.x64.me'
sprite_size = 32
# Shows a single lonesome tetromino
class TetroGrid(GridLayout):
def setup(self, tetromino, fungus, ftype='norm'):
self.clear_widgets()
self.col_default_width = sprite_size
self.row_default_height = sprite_size
self.rows = len(tetromino)
self.cols = len(tetromino[0])
self.size = ( sprite_size*self.cols, sprite_size*self.rows )
# Add each block to the GridLayout
for y in range(self.rows):
for x in range(self.cols):
b = GridBlock()
b.background = False
b.ftype = ftype
if tetromino[y][x]:
b.fungus = fungus
# Join neighboring blocks
neighbors = ''
# Up
if y > 0:
if tetromino[y-1][x]:
neighbors = neighbors+'u'
# Left
if x > 0:
if tetromino[y][x-1]:
neighbors = neighbors+'l'
# Right
if x < self.cols-1:
if tetromino[y][x+1]:
neighbors = neighbors+'r'
# Down
if y < self.rows-1:
if tetromino[y+1][x]:
neighbors = neighbors+'d'
b.neighbors = neighbors
self.add_widget(b)
# A single grid block containing either a fungus or nothing
class GridBlock(FloatLayout):
fungus = StringProperty('None')
ftype = StringProperty('norm')
neighbors = StringProperty('')
background = BooleanProperty(True)
sprite = ObjectProperty(None)
grid_background = ObjectProperty(None)
sammich = BooleanProperty(False)
def on_fungus(self, instance, value):
self.update_sprite()
def on_sammich(self, instance, value):
self.update_sprite()
def on_ftype(self, instance, value):
self.update_sprite()
def on_neighbors(self, instance, value):
self.update_sprite()
def on_background(self, instance, value):
if value:
self.grid_background.source = 'Graphics/Grid/block.png'
else:
self.grid_background.source = 'Graphics/blank.png'
def update_sprite(self):
if self.fungus == 'None':
if self.sammich:
self.sprite.source = 'Graphics/sammich.png'
else:
self.sprite.source = 'Graphics/blank.png'
else:
if self.neighbors == '':
self.sprite.source = 'atlas://Graphics/'+self.fungus+'/'+self.ftype+'/x'
else:
self.sprite.source = 'atlas://Graphics/'+self.fungus+'/'+self.ftype+'/'+self.neighbors
# Drag and zoomable view of the entire game grid
class GameGridView(Scatter):
gglayout = GridLayout(cols = grid_size_x,
rows = grid_size_y,
col_default_width = sprite_size,
row_default_height = sprite_size,
padding=0,
pos_hint=(None,None),
size=( sprite_size*grid_size_x, sprite_size*grid_size_y ))
def __init__(self, **kwargs):
super(GameGridView, self).__init__(**kwargs)
self.size = self.gglayout.size
self.gglayout.pos = self.pos
self.add_widget(self.gglayout)
def setup(self, grid):
self.gglayout.clear_widgets()
for y in grid:
for x in y:
self.gglayout.add_widget(x)
def on_touch_down(self, touch):
if self.parent.collide_point(*touch.pos):
super(GameGridView, self).on_touch_down(touch)
if touch.is_double_tap:
self.center = self.parent.center
self.scale = 1
if touch.is_mouse_scrolling:
if touch.button == 'scrollup':
self.scale -= 0.1
else:
self.scale += 0.1
def on_touch_move(self, touch):
if self.parent.collide_point(*touch.pos):
super(GameGridView, self).on_touch_move(touch)
def global_coords_to_block(self, x, y):
x, y = self.to_local(x, y)
x = int( x / sprite_size )
y = int( (self.gglayout.height-y) / sprite_size )
return x, y
# Layout dividers
class VertLine(Widget):
pass
class HorizLine(Widget):
pass
# Outline showing where new blocks will be placed
class Ghost(Scatter):
ghost_grid = ObjectProperty(None)
def setup(self, np, color):
self.ghost_grid.setup( np, color, 'ghost' )
self.ghost_grid.y = sprite_size - self.ghost_grid.height
def on_touch_up(self, touch):
super(Ghost, self).on_touch_up(touch)
scale = self.parent.ggview.scale
t_x = touch.x - self.ghost_grid.cols*sprite_size*scale
t_y = touch.y + self.ghost_grid.rows*sprite_size*scale
x, y = self.parent.ggview.global_coords_to_block( t_x, t_y )
self.parent.place_block( x, y )
self.parent.remove_widget(self)
def on_touch_move(self, touch):
# Snap when inside grid
g_x, g_y = self.parent.ggview.to_local(*touch.pos)
s_x, s_y = self.parent.ggview.gglayout.size
scale = self.parent.ggview.scale
if g_x > sprite_size and g_x < s_x+sprite_size and g_y > -sprite_size and g_y < s_y-sprite_size:
self.x = (g_x//sprite_size-self.ghost_grid.cols)*sprite_size*scale + self.parent.ggview.x
self.y = (g_y//sprite_size+self.ghost_grid.rows)*sprite_size*scale + self.parent.ggview.y
else:
self.x = touch.x - self.width - sprite_size
self.y = touch.y + self.ghost_grid.rows*sprite_size
class NewPieceBox(Widget):
grid = ObjectProperty(None)
class PlayerWidget(Widget):
icon = ObjectProperty(None)
name_label = ObjectProperty(None)
bites_grid = ObjectProperty(None)
def update(self, player):
self.bites_grid.clear_widgets()
for x in range(player.bites):
self.bites_grid.add_widget( Image(source='atlas://Graphics/Bite/norm/x') )
self.name_label.text = player.name
class ButtonsGrid(BoxLayout):
pass
class FungusGame(FloatLayout):
side_panel = ObjectProperty(None)
ggview = ObjectProperty(None)
#new_piece_box = ObjectProperty(None)
ghost = None
grid = Grid()
players = []
curr_player_num = 0
new_piece = tetros[0]
bite_mode = False
pause = True
def init_game(self, num_players):
# Don't let player make any moves until we are told to start the game
self.pause = True
# Clear everything
self.grid = Grid()
self.side_panel.clear_widgets()
# Setup players
if num_players == 4:
self.players = [ Player( 'Green', 'Algae', [ 5, 5 ] ),
Player( 'Red', 'E Coli', [ 5, grid_size_x-6 ] ),
Player( 'Blue', 'Nanites', [ grid_size_y-6, grid_size_x-6 ] ),
Player( 'Yellow', 'Penicillium', [ grid_size_y-6, 5 ] )]
elif num_players == 3:
self.players = [ Player( 'Green', 'Algae', [ 5, 5 ] ),
Player( 'Red', 'E Coli', [ 5, grid_size_x-6 ] ),
Player( 'Blue', 'Nanites', [ grid_size_y-6, grid_size_x-6 ] )]
else:
self.players = [ Player( 'Green', 'Algae', [ 5, 5 ] ),
Player( 'Blue', 'Nanites', [ grid_size_y-6, grid_size_x-6 ] )]
# Set all players to local if this is not a networked game
self.net = app.config.getboolean('game', 'enable_networking')
if not self.net:
for player in self.players:
player.local = True
# Initialize player widgets and add them to the side panel
for n in range(len(self.players)):
p = self.players[n]
p.panel = PlayerWidget()
p.panel.name_label.text = p.name
p.panel.icon.source = 'atlas://Graphics/'+p.color+'/home/x'
p.panel.update( p )
self.side_panel.add_widget( p.panel )
# Add dividers
self.side_panel.add_widget( HorizLine() )
# Add box and buttons to side panel
self.new_piece_box = NewPieceBox()
self.side_panel.add_widget( self.new_piece_box )
self.side_panel.add_widget( ButtonsGrid() )
# Initialize matrix of block objects.
# Because of the way GridLayout fills itself, coordinates are [Y][X]
# from top left corner.
for y in range(grid_size_y):
self.grid.append([])
for x in range(grid_size_x):
self.grid[y].append(GridBlock())
# Add home blocks
for player in self.players:
self.grid[ player.home[0] ][ player.home[1] ].fungus = player.color
self.grid[ player.home[0] ][ player.home[1] ].ftype = 'home'
# Add Sandwiches in corners
self.grid[ 0 ][ 0 ].sammich = True
self.grid[ 0 ][ grid_size_x-1 ].sammich = True
self.grid[ grid_size_y-1 ][ 0 ].sammich = True
self.grid[ grid_size_y-1 ][ grid_size_x-1 ].sammich = True
self.ggview.setup(self.grid)
def start_game(self, starting_player, starting_piece):
# Set starting piece
self.new_piece = tetros[ starting_piece ]
# Set starting player
self.curr_player_num = starting_player
self.curr_player = self.players[ starting_player ]
# Update UI
self.update_new_piece_box()
# Start game
self.pause = False
def on_touch_down(self, touch):
super(FungusGame, self).on_touch_down(touch)
# Don't accept input if its a network player's turn
# or if the game is on hold
if not self.pause:
if self.curr_player.local:
# If player is trying to drag the new piece, generate ghost
if self.new_piece_box.collide_point(*touch.pos):
self.ghost = Ghost()
self.ghost.center = self.new_piece_box.center
self.ghost.scale = self.ggview.scale
if self.bite_mode:
self.ghost.setup( [[True]], 'Bite' )
else:
self.ghost.setup( self.new_piece, self.curr_player.color )
self.add_widget(self.ghost)
def on_touch_up(self, touch):
super(FungusGame, self).on_touch_up(touch)
# If player taps the box, rotate new piece
if self.new_piece_box.collide_point(*touch.pos):
self.rotate_new_piece()
def rotate_new_piece(self):
# Don't accept input if its a network player's turn
# or if the game is on hold
if not self.pause:
if self.curr_player.local:
self.new_piece.rotate()
self.update_new_piece_box()
if self.net:
app.connection.sendRot()
def place_block(self, x, y):
if self.bite_mode:
r = self.grid.bite( self.curr_player, x, y )
else:
r = self.grid.place_block( self.new_piece, self.curr_player, x, y )
if r:
self.check_pulse()
self.grid.imperial_census( self.players )
self.curr_player.panel.update( self.curr_player )
if self.net and self.curr_player.local: # Don't send moves if move came from net in the first place
app.connection.sendMove( self.bite_mode, x, y )
self.next_turn()
def toggle_bite_mode(self):
# Don't accept input if its a network player's turn
# or if the game is on hold
if not self.pause:
if self.curr_player.local:
self.bite_mode = not self.bite_mode
self.update_new_piece_box()
def next_turn(self):
self.curr_player_num += 1 # Increment player number
if self.curr_player_num >= len(self.players):
self.curr_player_num = 0
self.curr_player = self.players[ self.curr_player_num ] # Set current player
self.bite_mode = False # Disable bite mode
if not self.net:
self.set_new_piece( randint(0,9) ) # Generate new random piece
def set_new_piece(self, tetro_num):
self.new_piece = tetros[ tetro_num ]
self.update_new_piece_box()
def check_pulse(self):
# Check to make sure the head of each team is alive
# if not, remove them from the players list
# and delete the rest of their blocks
#for x in range(len(self.players)):
# patient = self.players[x]
for patient in self.players:
home_fungus = self.grid[ patient.home[0] ][ patient.home[1] ].fungus
if home_fungus != patient.color:
self.grid.kill(patient)
self.side_panel.remove_widget( patient.panel )
self.players.remove(patient)
def update_new_piece_box(self):
box = self.new_piece_box
if self.bite_mode:
box.grid.setup( [[True]], 'Bite' )
else:
box.grid.setup( self.new_piece, self.curr_player.color )
box.grid.center = box.center
# Shows error message and OK button
class ErrorPopup(Popup):
textLabel = ObjectProperty(None)
pass
# Dialog that requires no answer
class RhetoricalPopup(Popup):
textLabel = ObjectProperty(None)
text = '...'
def __init__(self, title, text, **kwargs):
super(RhetoricalPopup, self).__init__(**kwargs)
self.title = title
self.textLabel.text = text
# Shows other players as they join the game
class LobbyPopup(Popup):
name1 = ObjectProperty(None)
name2 = ObjectProperty(None)
name3 = ObjectProperty(None)
name4 = ObjectProperty(None)
def __init__(self, **kwargs):
super(LobbyPopup, self).__init__(**kwargs)
self.num_players = app.config.getint('game', 'num_players') # Maybe this should be passed as an argument
if self.num_players == 2:
self.name2.text = 'Nobody'
self.name2.color = [1,1,1,0.5]
self.name4.text = 'Nobody'
self.name4.color = [1,1,1,0.5]
elif self.num_players == 3:
self.name4.text = 'Nobody'
self.name4.color = [1,1,1,0.5]
def setSelf(self, num):
name = app.config.get('game', 'username')
self.setName(num, name)
def setName(self, player_num, name):
if player_num == 0:
self.name1.text = name
self.name1.italic = False
elif player_num == 1:
if self.num_players == 2:
self.name3.text = name
self.name3.italic = False
else:
self.name2.text = name
self.name2.italic = False
elif player_num == 2:
self.name3.text = name
self.name3.italic = False
elif player_num == 3:
self.name4.text = name
self.name4.italic = False
# First screen shown on launch
# Lets player choose network or local game, number of players, name
class FirstPopup(Popup):
def go(self):
self.dismiss()
loadingPopup = RhetoricalPopup( 'Loading', 'Loading Stuff...' )
loadingPopup.open()
app.newGame()
loadingPopup.dismiss()
class FungusApp(App):
connection = None # Twisted Protocol instance
firstPopup = None # Setup screen
connectingPopup = None # Message shown while connecting to server
lobbyPopup = None # Message to be shown while waiting for players to join game
def build(self):
self.icon = 'icon.png'
#self.use_kivy_settings = False # Disable kivy tab in settings screen
self.game = FungusGame()
#self.newGame()
return self.game
def on_start(self):
self.firstPopup = FirstPopup()
self.firstPopup.open()
def on_stop(self):
self.config.write()
def newGame(self):
# Drop network connection if open
try:
app.connection.transport.loseConnection()
except AttributeError:
pass
num_players = self.config.getint('game', 'num_players')
self.game.init_game( num_players )
# Initialize Networking
if self.config.getboolean('game', 'enable_networking'):
# If this is a networked game, things will be initialized by the server
self.connect_to_server()
else:
# If this is not a networked game, then we choose a random starting player
# and piece and get the ball rolling
player = randint(0,num_players-1)
piece = randint(0,9)
self.game.start_game( player, piece )
def build_config(self, config):
config.setdefaults('game', {'username': 'Anonymous Coward'})
config.setdefaults('game', {'num_players': '4' })
config.setdefaults('game', {'enable_networking': True })
config.setdefaults('graphics', {'width': 1280,
'height': 768})
def build_settings(self, settings):
settings.add_json_panel('Game Settings',
self.config,
data=settings_json)
def on_config_change(self, config, section, key, value):
if key == 'num_players' or key == 'enable_networking':
self.newGame()
def showErrorPopup(self, title, text):
popup = ErrorPopup( title = title )
popup.textLabel.text = text
popup.open()
def connect_to_server(self):
reactor.connectTCP( server, 1701, NetFactory(self))
self.connectingPopup = RhetoricalPopup( 'Connecting...', 'Connecting to %s' % server )
self.connectingPopup.open()
def on_connection(self, connection):
self.connection = connection
self.connectingPopup.dismiss()
self.lobbyPopup = LobbyPopup()
self.lobbyPopup.open()
print( 'App connected succesfully' )
if __name__ == '__main__':
app = FungusApp()
app.run()