-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
178 lines (146 loc) · 5.27 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
import pygame as pg
import sys
from settings import *
from render_handels.map import *
from player_handels.player import *
from render_handels.raycasting import *
from render_handels.object_renderer import *
from sprite_handels.sprite_object import *
from sprite_handels.object_handler import *
from sprite_handels.weapon import *
from sprite_handels.sound import *
from render_handels.pathfinding import *
# from server.server_classes import *
from connection_handels.client_classes import *
from connection_handels.msg_classes import *
from login import *
import socket
import json
import pickle
playerId=None
class Cli_updates:
position=[1,0]
yaw=0
shoot=False
def __init__(self,position=[1,0],yaw=0,shoot=False):
self.position=position
self.yaw=yaw
self.shoot=shoot
def getJson(self):
# print(self.__dict__)
return json.dumps(self.__dict__)
class Game:
connector=None # SOCKET CONNECTOR
def __init__(self,s,x,y):
# def __init__(self,x,y):
pg.init()
pg.mouse.set_visible(False)
self.screen = pg.display.set_mode(RES)
pg.event.set_grab(True)
self.clock = pg.time.Clock()
self.delta_time = 1
self.global_trigger = False
self.global_event = pg.USEREVENT + 0
pg.time.set_timer(self.global_event, 40)
self.connector=s
self.new_game(x,y)
def new_game(self,x,y):
self.map = Map(self)
self.player = Player(self,x,y)
self.object_renderer = ObjectRenderer(self)
self.raycasting = RayCasting(self)
self.object_handler = ObjectHandler(self)
self.weapon = Weapon(self)
self.sound = Sound(self)
self.pathfinding = PathFinding(self)
pg.mixer.music.play(-1)
def update(self):
self.player.update()
self.raycasting.update()
self.object_handler.update()
global playerId
obj=DataPlayer([self.player.pos],self.player.angle,self.player.shot,self.player.health,self.player.shotWho)
# print("check point 3")
# print(obj)
# exampleObj = {'Python':3,'KDE':5,'Windows':10}
# fileObj = open('data.obj', 'wb')
# pickle.dump(obj,fileObj)
# fileObj.close()
game_state_res=self.connector.playerUpdate(obj)
# for i in game_state_res.msg:
# # print(type(i))
# if i != str(playerId):
# print(str(i) + " " +str(playerId)+ " -> ")
# print(game_state_res.msg[i])
# remove last outputed line
# print(int(game_state_res.msg[str(playerId)]["health"]),self.player.health)
if int(game_state_res.msg[str(playerId)]["health"])!=self.player.health:
print("got shot")
self.player.health=int(game_state_res.msg[str(playerId)]["health"])
self.player.get_damage()
currPlayerServerUpdate=game_state_res.msg[str(playerId)]
self.player.shotWho=currPlayerServerUpdate["shotWho"]
# print("shot : "+str(currPlayerServerUpdate["shotWho"]))
del game_state_res.msg[str(playerId)]
self.object_handler.updateGameStateNpc(game_state_res.msg)
self.weapon.update()
pg.display.flip()
self.delta_time = self.clock.tick(FPS)
pg.display.set_caption(f'{self.clock.get_fps() :.1f}')
def draw(self):
# self.screen.fill('black')
self.object_renderer.draw()
self.weapon.draw()
# self.map.draw()
# self.player.draw()
def check_events(self):
self.global_trigger = False
for event in pg.event.get():
if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE):
pg.quit()
sys.exit()
elif event.type == self.global_event:
self.global_trigger = True
self.player.single_fire_event(event)
def run(self):
# self.object_handler.spawn_npc()
while True:
self.check_events()
self.update()
self.draw()
def init_new_game():
connector = ClientJsonCon()
print("---Connected to server---")
# spawn_location = connector.login()
spawn_location = connector.login()
global playerId
playerId= spawn_location[2]
print("spawn location: ",spawn_location)
game = Game(connector,spawn_location[0],spawn_location[1])
# game = Game(1,1)
game.run()
if __name__ == '__main__':
# init_new_game()
username=(LoginState().run())
print(username)
connector = ClientJsonCon()
print("---Connected to server---")
# spawn_location = connector.login()
spawn_location = connector.login(username)
# global playerId
playerId= spawn_location[2]
print("spawn location: ",spawn_location)
game = Game(connector,spawn_location[0],spawn_location[1])
# game = Game(1,1)
game.run()
# def startGame():
# connector = ClientJsonCon()
# print("---Connected to server---")
# # spawn_location = connector.login()
# spawn_location = connector.login()
# # global playerId
# playerId= spawn_location[2]
# print("spawn location: ",spawn_location)
# game = Game(connector,spawn_location[0],spawn_location[1])
# # game = Game(1,1)
# game.run()