forked from orneo1212/PythonFarmGame
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pyFarmGame.py
executable file
·127 lines (99 loc) · 2.66 KB
/
pyFarmGame.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import pygame
from farmlib import __VERSION__
from farmlib.menuwindow import MenuWindow
from farmlib.pluginsystem import base_plugin_system as PluginSystem
pygame.init()
pygame.key.set_repeat(100, 100)
class FarmGamePygame(object):
"""
Farm Game Pygame
"""
def __init__(self):
"""Init game"""
self.screen = pygame.display.set_mode((800, 600), pygame.DOUBLEBUF)
pygame.display.set_caption("PyFarmGame " + "v. " + __VERSION__)
# timer
self.timer = pygame.time.Clock()
self.activescr = None
self.ingame = False
self.inmenu = True
def set_active_screen(self, activescreen):
"""set active screen
:param activescreen:
:return:
"""
self.activescr = activescreen
self.activescr.parent = self
def update(self):
"""update
:return:
"""
self.activescr.update()
def events(self):
"""events
:return:
"""
self.activescr.events()
def redraw(self, surface):
"""redraw
:param surface:
:return:
"""
self.activescr.redraw(surface)
def run(self):
"""
Run game. Remove lock when error
"""
try:
self.main()
except Exception:
import traceback
traceback.print_exc()
self.remove_game_lock()
exit(1)
@staticmethod
def check_game_lock():
"""check game lock
:return:
"""
if os.path.isfile("game.lock"):
print("Game is already running. If not manualy"
" remove game.lock file and try again")
exit(0)
else:
open("game.lock", "w").close()
@staticmethod
def remove_game_lock():
"""remove game lock
:return:
"""
if os.path.isfile("game.lock"):
os.remove("game.lock")
def main(self):
"""Main"""
# check for lock file
self.check_game_lock()
# IN GAME
if self.ingame:
self.activescr.init()
elif not self.ingame and self.inmenu:
pass
while self.activescr.running:
self.events()
self.update()
self.redraw(self.screen)
PluginSystem.run()
self.timer.tick(30)
pygame.display.flip()
# Save game
if self.ingame:
self.activescr.deinit()
# remove lock
self.remove_game_lock()
if __name__ == '__main__':
f = FarmGamePygame()
f.set_active_screen(MenuWindow())
f.run()