-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient2.py
More file actions
84 lines (67 loc) · 2.46 KB
/
client2.py
File metadata and controls
84 lines (67 loc) · 2.46 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
import pygame
from network import Network
import pickle
pygame.font.init()
from player import Player
width = 800
height = 800
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Client")
class Button:
def __init__(self, text, x, y, color):
self.text = text
self.x = x
self.y = y
self.color = color
self.width = 150
self.height = 100
def draw(self, win):
pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height))
font = pygame.font.SysFont("comicsans", 40)
text = font.render(self.text, 1, (255, 255, 255))
win.blit(text, (self.x + round(self.width/2) - round(text.get_width()/2), self.y + round(self.height/2) - round(text.get_height()/2))) # center the texts
def click(self, pos):
x1 = pos[0]
y1 = pos[1]
if self.x <= x1 <= self.x + self.width and self.y <= y1 <= self.y + self.height: # to check if the mouse is in the button when click
return True
else:
return False
def redrawWindow(win, game, p):
win.fill((128, 128, 128))
pass
btns = [Button("Rock", 50, 500, (0, 0, 0)), Button("Scissors", 250, 500, (255, 0, 0)), Button("Paper", 450, 500, (0, 255, 0))]
def main():
run = True
clock = pygame.time.Clock()
n = Network()
player = int(n.getP()) # to get player number
print("You are player: ", player)
while run:
clock.tick(60)
try:
game = n.send("get")
except:
run = False
print("Could not get the game.")
break
if game.bothWent():
redrawWindow(win, game, player)
pygame.time.delay(500)
try:
game = n.send("reset")
except:
run = False
print("Couldn't get game")
break
font = pygame.font.SysFont("comicsans", 90)
if (game.winner() == 1 and player == 1) or (game.winner() == 0 and player == 0):
text = font.render("You Won!", 1, (255, 0, 0))
elif game.winner() == -1:
text = font.render("Tie Game!", 1, (255, 0, 0))
else:
text = font.render("You Lost...", 1, (255, 0, 0))
win.blit(text, (width / 2 - text.get_width() / 2, height / 2 - text.get_height() / 2)) # text align center
pygame.display.update()
pygame.time.delay(2000) # 2 seconds
main()