-
Notifications
You must be signed in to change notification settings - Fork 0
/
timaaospygame.py
243 lines (199 loc) · 6.4 KB
/
timaaospygame.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
# Pygame шаблон - скелет для нового проекта Pygame
# !/usr/bin/env python3
"""Script for Tkinter GUI chat client."""
import ast
from threading import Timer
import random
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
from os import system, name
import pygame
import os
from utils import Block
screensize = [32, 16]
# Collect events until released
# import sleep to show output for some time period
from time import sleep
# define our clear function
def clear():
# for windows
if name == 'nt':
_ = system('cls')
# for mac and linux(here, os.name is 'posix')
else:
_ = system('clear')
import screen
receivequit = False
import keyboard
class player():
def __init__(self):
self.x = 0
self.y = 0
self.reload = False
self.rotation = 0
self.id = 0
self.players = {}
self.sockets = {}
def move(self, x, y):
self.x = x
self.y = y
movedict = {'event': 'move', 'x': self.x, 'y': self.y,
'id': str(myid)}
# print(movedict)
send(str(movedict))
def infodict(self):
return str({'x': self.x, 'y': self.y, 'id': myid})
def reload():
sleep(1)
player.reload = False
# Создаем игру и окно
pygame.init()
# настройка папки ассетов
WIDTH = 360
HEIGHT = 480
FPS = 30
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, 'images')
player_img = pygame.image.load(os.path.join(img_folder, 'character_for_game.png')).convert()
pygame.quit()
class PlayerSpr(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = player_img
self.image.set_colorkey((228,228,228))
self.rect = self.image.get_rect()
self.rect.center = (0,0)
def reloadSprites():
sprites = {}
for key, value in player.players.items():
newspr = PlayerSpr()
newspr.x = value['x']
newspr.y = value['y']
print(newspr.x, newspr.y)
sprites[value['id']] = newspr
sprites[value['id']] = newspr
def receive():
"""Handles receiving of messages."""
while True:
try:
msg = client_socket.recv(BUFSIZ).decode("utf8")
# print("i recieved: " + msg)
if (msg == "{quit:true}"):
receivequit = True
# if("^sockets^" in msg):
# player.sockets = ast.literal_eval(msg.split('⊘')[1])
if ("^players^" in msg):
send('getBlocks')
player.players = ast.literal_eval(msg.split('⊘')[1])
reloadSprites()
if ("^blocks^" in msg):
# print('igotblocks')
player.blocks = ast.literal_eval(msg.split('⊘')[1])
if (msg.startswith("^bullets^")):
# print(msg.split(';')[0].split('R')[1][:-1])
player.bullets = ast.literal_eval(msg.split(';')[0].split('R')[1])
if ("{'event':" in msg):
mydict = ast.literal_eval(msg.split(';')[0])
# print(mydict)
player.players[str(mydict['id'])]['x'] = mydict['x']
player.players[str(mydict['id'])]['y'] = mydict['y']
if(not str(mydict['id']) in sprites):
sprites[mydict['id']] = PlayerSpr()
sprites[mydict['id']].rect.x = mydict['x']
sprites[mydict['id']].rect.y = mydict['y']
# if ("{'blockplace':" in msg):
# mydict = ast.literal_eval(msg.split(';')[0])
# player.blocks[str(mydict['block']['id'])] = mydict['block']
# player.renderPlayers()
if ("updatePls" == msg):
send("getPlayers")
if ("giveInfo" == msg):
send(player.infodict())
send("getPlayers")
except OSError: # Possibly client has left the chat.
break
def getplayerbyid(playerarr, id):
for obj in playerarr:
if obj.id == id:
return obj
return False
def send(msg): # event is passed by binders.
"""Handles sending of messages."""
# print("i sended: " + msg)
msg = msg + ";"
client_socket.send(bytes(msg, "utf8"))
# ----Now comes the sockets part----
HOST = input('Enter host: ')
PORT = input('Enter port: ')
if not PORT:
PORT = 33000
else:
PORT = int(PORT)
BUFSIZ = 32000
ADDR = (HOST, PORT)
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(ADDR)
player = player()
receive_thread = Thread(target=receive)
receive_thread.start()
player.id = str(random.randint(0, 9999))
send(str(player.id))
myid = player.id
pygame.init()
# настройка папки ассетов
WIDTH = 360
HEIGHT = 480
FPS = 30
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# Задаем цвета
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()
# Цикл игры
sprites = {}
for key, value in player.players.items():
newspr = PlayerSpr()
newspr.x = value['x']
newspr.y = value['y']
print(newspr.x, newspr.y)
sprites[value['id']] = newspr
running = True
while running:
# Держим цикл на правильной скорости
clock.tick(FPS)
# Ввод процесса (события)
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
#TODO: block placing
elif event.type == pygame.KEYDOWN:
xadd = 0
yadd = 0
print('keydown ' + str(event.key))
if(event.key == pygame.K_w):
yadd = -10
if (event.key == pygame.K_s):
yadd = 10
if (event.key == pygame.K_d):
xadd = 10
if (event.key == pygame.K_a):
xadd = -10
player.move(player.x+xadd,player.y+yadd)
# Обновление
# Рендеринг
screen.fill(BLACK)
print(sprites)
for key,value in sprites.items():
screen.blit(value.image, (value.rect.x, value.rect.y))
pygame.display.update()
pygame.quit()