-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClasses.py
More file actions
107 lines (97 loc) · 4.26 KB
/
Copy pathClasses.py
File metadata and controls
107 lines (97 loc) · 4.26 KB
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
import socket
import pygame as p
import random as r
import math as m
import os
class player():
def __init__(self, x , y, width, height, color):
self.x = x
self.y = y
self.height = height
self.width = width
self.vel = 7
self.deg = 0
self.dim = p.Rect(self.x, self.y, self.width, self.height)
self.color=color
def movementcheck(self, tank, walls):
self.tank = tank
self.walls = walls
for wall in self.walls:
if self.tank.dim.colliderect(wall.dim):
return 'n'
def animate(self, win):
win.blit(p.image.load(os.path.join('Resources/Tanks Animations', f't{self.color}{self.deg}.png')).convert() , (self.x, self.y) )
class gun():
def __init__(self, x, y, radius, deg):
self.x = x
self.y = y
self.radius = radius
self.deg = deg
self.vel = 10
def bounce(self, bullet, walls):
self.bullet = bullet
self.walls = walls
for wall in walls:
p1 = (wall.dim[0], wall.dim[1])
p2 = (wall.dim[0] + wall.dim[2], wall.dim[1])
p3 = (wall.dim[0] + wall.dim[2], wall.dim[1] + wall.dim[3])
p4 = (wall.dim[0], wall.dim[1] + wall.dim[3])
lines = [(p1, p2, wall.dim[2]), (p3, p4, wall.dim[2]), (p1, p4, wall.dim[3]), (p2, p3, wall.dim[3])]
for line in lines:
AB = [line[1][0] - line[0][0], line[1][1] - line[0][1]]
BE = [self.bullet.x - line[1][0], self.bullet.y - line[1][1]]
AE = [self.bullet.x - line[0][0], self.bullet.y - line[0][1]]
AB_BE = AB[0] * BE[0] + AB[1] * BE[1]
AB_AE = AB[0] * AE[0] + AB[1] * AE[1]
distance = 0
if AB_BE > 0:
y = self.bullet.y - line[1][1]
x = self.bullet.x - line[1][0]
distance = m.sqrt(x*x + y*y)
elif AB_AE < 0:
y = self.bullet.y - line[0][1]
x = self.bullet.x - line[0][0]
distance = m.sqrt(x*x + y*y)
else:
x1 = AB[0]
y1 = AB[1]
x2 = AE[0]
y2 = AE[1]
mod = m.sqrt(x1 * x1 + y1 * y1);
distance = abs(x1 * y2 - y1 * x2) / mod
if distance <= self.radius:
if line[0][0] == line[1][0]:
self.bullet.deg = - self.bullet.deg
else :
self.bullet.deg = 180 - self.bullet.deg
return 'done'
def animate(self, win):
p.draw.circle(win, (0 ,0, 0), (self.x, self.y), self.radius)
class obstructions():
def __init__(self):
self.x = r.randint(0, 1436)
self.y = r.randint(0, 700)
self.width = r.randint(30, 64)
self.height = r.randint(30, 128)
self.dim = (self.x, self.y, self.width, self.height)
def animate(self, win):
p.draw.rect(win, (0, 0, 0), self.dim)
class Networking():
def __init__(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server = '192.168.1.105'
self.port = 5555
self.addr = (self.server, self.port)
self.answer = self.connect()
def connect(self):
try :
self.client.connect(self.addr)
return self.client.recv(2048).decode()
except:
print("error in sending")
def send(self, data):
try :
self.client.send(str.encode(data))
return self.client.recv(2048).decode()
except socket.error as s:
print(s)