-
Notifications
You must be signed in to change notification settings - Fork 0
/
hugbo_3.py
143 lines (131 loc) · 5.03 KB
/
hugbo_3.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
from __future__ import annotations
import time
import random
from tkinter import *
print("Player1 controls:\n\t\ta->left\n\t\td->right\n\t\ts->stop the paddle")
print("Player2 controls:\n\t\tarrow-left->left\n\t\tarrow-right->right\n\t\tarrow-down->stop the paddle")
print("First to score 3 wins!")
class Paddle:
def __init__(self, canvas: Canvas, color: str, height: int, player: int) -> None:
self.canvas = canvas
self.height = height
self.id = canvas.create_rectangle(0,0, 100, 15, fill=color)
self.canvas.move(self.id, (w/2)-50, height)
self.xspeed = 0
self.player=player
if player==1:
self.canvas.bind_all('<KeyPress-Left>', self.move_left)
self.canvas.bind_all('<KeyPress-Right>', self.move_right)
self.canvas.bind_all('<KeyPress-Down>', self.stop)
elif player==2:
self.canvas.bind_all('<KeyPress-a>', self.move_left)
self.canvas.bind_all('<KeyPress-d>', self.move_right)
self.canvas.bind_all('<KeyPress-s>', self.stop)
def draw(self) -> None:
self.canvas.move(self.id, self.xspeed, 0)
pos = self.canvas.coords(self.id)
if pos[0] <= 0:
self.xspeed = 0
if pos[2] >= w:
self.xspeed = 0
def move_left(self, evt: Event) -> None:
self.xspeed = -2
def move_right(self, evt: Event) -> None:
self.xspeed = 2
def stop(self, evt: Event) -> None:
self.xspeed = 0
class Ball:
def __init__(self, canvas: Canvas, color: str, size: int, paddle: Paddle, paddle2: Paddle) -> None:
self.canvas = canvas
self.id = canvas.create_rectangle(10, 10, size, size, fill=color)
self.canvas.move(self.id, 360, w/2)
self.xspeed = random.randrange(-3,3)
self.yspeed = random.randrange(-2,2)
self.paddle = paddle
self.paddle2 = paddle2
while(self.yspeed==0):
self.yspeed = random.randrange(-1,1)
self.hit_topbottom = False
self.hit_bottom = False
self.hit_top = False
def draw(self) -> None:
self.canvas.move(self.id, self.xspeed, self.yspeed)
pos = self.canvas.coords(self.id)
if pos[1] <= 0:
self.hit_topbottom = True
self.hit_top=True
if pos[3] >= h:
self.hit_topbottom = True
self.hit_bottom = True
if pos[0] <= 0:
self.xspeed = 2
if pos[2] >= w:
self.xspeed = -2
if pos[1]>h/2-250 and self.hit_paddle(pos,paddle) == True:
self.yspeed = -2
self.xspeed = random.randrange(-2,2)
if pos[1]<h/2+250 and self.hit_paddle2(pos,paddle2) == True:
self.yspeed = 2
self.xspeed = random.randrange(-2,2)
def hit_paddle(self, pos: list[int], paddle: Paddle) -> bool:
paddle_pos = self.canvas.coords(self.paddle.id)
if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
return True
return False
def hit_paddle2(self, pos: list[int], paddle2: Paddle) -> bool:
paddle2_pos = self.canvas.coords(self.paddle2.id)
if pos[2] >= paddle2_pos[0] and pos[0] <= paddle2_pos[2]:
if pos[1] >= paddle2_pos[1] and pos[1] <= paddle2_pos[3]:
return True
return False
score=0
score2=0
while(True):
tk = Tk()
tk.title("hugbo_3")
w=480
h=640
canvas = Canvas(tk, width=w, height=h, bd=0, bg='black')
canvas.pack()
tk.update()
paddle = Paddle(canvas, 'white',h-15,1)
paddle2= Paddle(canvas, 'white',0,2)
ball = Ball(canvas, 'white', 25, paddle, paddle2)
canvas.create_line(0,h/2+2,w,h/2+2,fill="white",width=4, dash=(2, 4))
label = canvas.create_text(5, h/2-105, anchor=NW, text=score,fill="white", font=("Ani",50))
label = canvas.create_text(5, h/2, anchor=NW, text=score2,fill="white",font=("Ani",50))
while ball.hit_topbottom == False:
ball.draw()
paddle.draw()
paddle2.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)
if ball.hit_top==True:
score=score+1
ball.hit_topbottom == False
ball.hit_top==False
elif ball.hit_bottom==True:
score2=score2+1
ball.hit_topbottom == False
ball.hit_bottom==False
if score==3:
go_label = canvas.create_text(w/2,h/2,text="Player1 WON!",font=("Cantarell Ultra-Bold",40), fill="White")
ball.hit_topbottom == True
tk.update()
time.sleep(10)
tk.destroy
break
elif score2==3:
go_label = canvas.create_text(w/2,h/2,text="Player2 WON!",font=("Cantarell Ultra-Bold",40), fill="White")
tk.update()
time.sleep(10)
tk.destroy
break
else:
go_label = canvas.create_text(w/2,h/2,text="Score!",font=("Cantarell Ultra-Bold",40), fill="White")
tk.update()
time.sleep(2)
ball.hit_topbottom=False
tk.destroy()