-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient22.py
More file actions
203 lines (159 loc) · 5.02 KB
/
client22.py
File metadata and controls
203 lines (159 loc) · 5.02 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
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
import socket
import errno
import sys
import turtle
t1 = turtle.Turtle()
t1.penup()
t1.goto(-350, 0)
t1.shape('square')
t1.color('blue')
t1.speed(0)
t1.shapesize(stretch_wid=4, stretch_len=1)
screen = turtle.Screen()
screen.setup(800, 800)
message = ""
screen.bgcolor('black')
my_username = screen.textinput('Name', 'Enter your name')
tt = turtle.Turtle()
tt.ht()
tt.color('brown')
tt.speed(0)
tt.goto(0, 400)
tt.goto(0, -400)
t2 = turtle.Turtle()
t2.penup()
t2.speed(0)
t2.goto(350, 0)
t2.shape('square')
t2.color('red')
t2.shapesize(stretch_wid=4, stretch_len=1)
t3 = turtle.Turtle()
t3.shape('circle')
t3.color('white')
t3.penup()
t3.direction = 1.5
t3.isUp = 1
scoreA = turtle.Turtle()
scoreA.score = 0
scoreA.penup()
scoreA.ht()
scoreA.goto(-150, 350)
scoreA.speed(0)
scoreB = turtle.Turtle()
scoreB.score = 0
scoreB.speed(0)
scoreB.penup()
scoreB.ht()
scoreB.goto(150, 350)
scoreB.color('White')
scoreA.color('White')
scoreA.write(scoreA.score, font=("Arial", 18, "normal"))
scoreB.write(scoreB.score, font=("Arial", 18, "normal"))
def handleUp():
global message
t1.goto(-350, t1.ycor() + 20)
message = 'Up'
def handleUp2():
global message
t1.goto(-350, t1.ycor() - 20)
message = 'Down'
def move_t2_up():
t2.goto(350, t2.ycor() + 20)
def move_t2_down():
t2.goto(350, t2.ycor() - 20)
move_ball1 = False
move_ball2 = False
def setBall():
global move_ball2, message
move_ball2 = True
message = 'Enter'
turtle.listen()
turtle.onkey(handleUp, 'Up')
turtle.onkey(handleUp2, 'Down')
turtle.onkey(setBall, 'Return')
header_length = 20
ip = '192.168.0.105'
# ip = '127.0.0.1'
port = 9999
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((ip, port))
client_socket.setblocking(False)
username = my_username.encode('utf-8')
username_header = f"{len(username):<{header_length}}".encode('utf-8')
client_socket.send(username_header + username)
def moveball():
global message, move_ball1, move_ball2
if not move_ball1 and not move_ball2:
return
if t3.xcor() > 370 or t3.xcor() < -370:
if t3.xcor() > 370:
scoreA.score += 1
scoreA.clear()
scoreA.write(str(scoreA.score), font=("Arial", 18, "normal"))
if t3.xcor() < -370:
scoreB.score += 1
scoreB.clear()
scoreB.write(str(scoreB.score), font=("Arial", 18, "normal"))
if scoreA.score == 7:
screen.clear()
message1 = 'WinA'
message1 = message1.encode('utf-8')
message1_header = f'{len(message1) :< {header_length}}'.encode('utf-8')
client_socket.send(message1_header + message1)
import win
if scoreB.score == 7:
screen.clear()
message1 = 'WinB'
message1 = message1.encode('utf-8')
message1_header = f'{len(message1) :< {header_length}}'.encode('utf-8')
client_socket.send(message1_header + message1)
import lost
t3.ht()
t3.goto(0, 0)
t3.showturtle()
move_ball1 = False
move_ball2 = False
if t3.ycor() > 370 or t3.ycor() < -370:
t3.isUp = -t3.isUp
if t1.ycor() + 40 > t3.ycor() > t1.ycor() - 40 and -340 > t3.xcor() > -350:
t3.direction = -t3.direction
if t2.ycor() + 40 > t3.ycor() > t2.ycor() - 40 and 340 < t3.xcor() < 350:
t3.direction = -t3.direction
t3.goto(t3.xcor() + 1.2 * t3.direction, t3.ycor() + 0.8 * t3.isUp)
while True:
message = ""
moveball()
screen.update()
if message:
message = message.encode('utf-8')
message_header = f'{len(message) :< {header_length}}'.encode('utf-8')
client_socket.send(message_header + message)
try:
while True:
username_header = client_socket.recv(header_length)
if not len(username_header):
print('Connection closed by server')
sys.exit()
username_length = int(username_header.decode('utf-8').strip())
username = client_socket.recv(username_length).decode('utf-8')
message_header = client_socket.recv(header_length)
message_length = int(message_header.decode('utf-8').strip())
message = client_socket.recv(message_length).decode('utf-8')
print(f'{username} > {message}')
if message == 'Enter':
move_ball1 = True
if message == 'Up':
move_t2_up()
if message == 'Down':
move_t2_down()
# message = ""
screen.update()
except IOError as ioe:
if ioe.errno != errno.EAGAIN and ioe.errno != errno.EWOULDBLOCK:
print('Reading error', str(ioe))
sys.exit()
continue
except Exception as e:
print(('General error', str(e)))
pass
screen.update()