-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnormal_program_1 (1).py
173 lines (142 loc) · 5.26 KB
/
normal_program_1 (1).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
import pygame
import math
import socket
import threading
if input() == 'host':
print('host_mode')
my_ip = socket.gethostname()
print(my_ip)
server = socket.socket()
server.bind(('', 9090))
server.listen(1)
client, client_ip = server.accept()
print('connected')
connect_type = True
else:
print('client_mode')
client = socket.socket()
client.connect((input(), 9090))
print('connected')
connect_type = False
pygame.init()
main_display = pygame.display.set_mode((1920, 1080), pygame.FULLSCREEN)
pygame.display.set_caption('Game')
clock = pygame.time.Clock()
display_x_size, display_y_size = pygame.display.get_window_size()
keys = []
class car:
def __init__(self, path):
self.degree = 0
self.coord = [500, 500]
self.images = []
self.x_speed = 0
self.y_speed = 0
for i in range(0, 360):
self.images.append(pygame.transform.scale(pygame.image.load(path + '\\' + '0' * (4 - len(str(i))) + str(i) + '.png'), (200, 200)))
def draw(self):
main_display.blit(self.images[self.degree], self.coord)
def move(self):
self.coord[0] += self.x_speed
self.coord[1] += self.y_speed
deg = 0
if self.x_speed > 0 and self.y_speed > 0:
deg = 360 - math.degrees(math.atan(self.y_speed / self.x_speed))
elif self.x_speed > 0 and self.y_speed < 0:
deg = math.degrees(math.atan(-self.y_speed / self.x_speed))
elif self.x_speed < 0 and self.y_speed > 0:
deg = 180 + math.degrees(math.atan(self.y_speed / -self.x_speed))
elif self.x_speed < 0 and self.y_speed < 0:
deg = 180 - math.degrees(math.atan(-self.y_speed / -self.x_speed))
self.x_speed *= 0.99 - abs(deg - self.degree) * 0.0002
self.y_speed *= 0.99 - abs(deg - self.degree) * 0.0002
def get_coord(self):
return self.coord
def set_coord(self, coord):
self.coord = list(coord)
def change_turn(self, degree):
self.degree = (self.degree - degree) % 360
def get_degree(self):
return self.degree
def set_degree(self, degree):
self.degree = degree
def change_speed(self, speed):
if self.degree >= 270:
self.x_speed -= speed * math.cos(math.radians(90 - self.degree - 270))
self.y_speed -= speed * math.sin(math.radians(90 - self.degree - 270))
elif 270 > self.degree >= 180:
self.x_speed -= speed * math.cos(math.radians(self.degree - 180))
self.y_speed += speed * math.sin(math.radians(self.degree - 180))
elif 180 > self.degree >= 90:
self.x_speed += speed * math.cos(math.radians(90 - self.degree - 90))
self.y_speed += speed * math.sin(math.radians(90 - self.degree - 90))
else:
self.x_speed += speed * math.cos(math.radians(self.degree))
self.y_speed -= speed * math.sin(math.radians(self.degree))
mark_2 = car('mark_2')
online_car = car('mark_2')
def online():
global connect_type
global online_car
if connect_type:
global server
global client
else:
global client
while True:
a = client.recv(1024).decode()
a = a.split(';')
for i in a[:-1]:
s = i.split(':')
try:
s = (float(s[0]), float(s[1]), float(s[2]))
online_car.set_coord((s[0], s[1]))
online_car.set_degree(int(s[2]))
except IndexError:
print(s)
for online_thread in [threading.Thread(target=online)]:
online_thread.start()
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == 119 and 'W' not in keys: # W
keys.append('W')
elif event.key == 97 and 'A' not in keys: # A
keys.append('A')
elif event.key == 115 and 'S' not in keys: # S
keys.append('S')
elif event.key == 100 and 'D' not in keys: # D
keys.append('D')
elif event.key == 32 and ' ' not in keys: # space
keys.append(' ')
elif event.type == pygame.KEYUP:
if event.key == 119: # W
keys.remove('W')
elif event.key == 97: # A
keys.remove('A')
elif event.key == 115: # S
keys.remove('S')
elif event.key == 100: # D
keys.remove('D')
if 'A' in keys:
mark_2.change_turn(-3)
if 'D' in keys:
mark_2.change_turn(3)
if 'W' in keys:
mark_2.change_speed(0.2)
if 'S' in keys:
mark_2.change_speed(-0.2)
if ' ' in keys:
mark_2.set_coord((0, 0))
keys.remove(' ')
mark_2.move()
main_display.fill((0, 40, 0))
mark_2.draw()
online_car.draw()
pygame.display.flip()
data = str(mark_2.get_coord()[0]) + ':' + str(mark_2.get_coord()[1]) + ':' + str(mark_2.get_degree()) + ';'
client.send(data.encode())
clock.tick(66)
pygame.quit()