-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpaddlewhack.py
167 lines (132 loc) · 5.68 KB
/
paddlewhack.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
from tkinter import *
import time
import random
# define the Ball
class Ball:
def __init__(self, canvas, paddle, color): # need to draw on a canvas, and in a given colour
self.canvas = canvas
self.paddle = paddle # also need to know where the paddle is
self.id = canvas.create_oval(10, 10, 25, 25, fill=color) # coordinates: top-l, bot-r
self.canvas.move(self.id, 245, 100); # move 245 px right, 100 down
self.x = random.randint(-3, 3) # how many pixels will we move right initially?
self.y = random.randint(-3, 3) # how many pixels will we move down initially?
self.canvas_height = self.canvas.winfo_height() # get height of canvas
self.canvas_width = self.canvas.winfo_width() # get width of canvas
self.hit_bottom = False
def draw(self):
self.canvas.move(self.id, self.x, self.y) # move x pixel right, y pixel down
pos = self.canvas.coords(self.id) # get current x and y coordinates of ourself (the ball)
if pos[1] <= 0: # if top of the ball hits top of screen
self.y = reverse(self.y) # ... reverse up/down direction
if pos[3] >= self.canvas_height: # if bottom of ball hits bottom of screen
self.y = reverse(self.y) # ... reverse up/down direction
if pos[0] <= 0: # if left side of the ball hits left screen border
self.x = reverse(self.x) # ... reverse left/right direction
if pos[2] >= self.canvas_width: # if right side of ball hits right screen border
self.x = reverse(self.x) # ... reverse left/right direction
if self.hit_paddle():
self.y = reverse(self.y)
if pos[3] >= self.canvas_height:
self.hit_bottom = True
def hit_paddle(self):
paddle_pos = self.canvas.coords(self.paddle.id)
ball_pos = self.canvas.coords(self.id)
if ball_pos[2] >= paddle_pos[0] and ball_pos[0] <= paddle_pos[2]:
if ball_pos[3] >= paddle_pos[1] and ball_pos[3] <= paddle_pos[3]:
return True
return False
# define the Paddle
class Paddle:
def __init__(self, canvas, color): # like ball, need to draw on a canvas, and in a given colour
self.canvas = canvas
self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
self.canvas.move(self.id, 200, 300)
self.x = 0
self.canvas_width = self.canvas.winfo_width()
self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
self.canvas.bind_all('<KeyPress-Right>', self.turn_right)
def draw(self):
self.canvas.move(self.id, self.x, 0)
pos = self.canvas.coords(self.id)
if pos[0] <= 0 or pos[2] >= self.canvas_width:
self.x = 0;
def turn_left(self, event):
self.x = -2
def turn_right(self, event):
self.x = 2
# a helper function to reverse a direction
def reverse(direction):
return direction * -1
class Game:
'''
The game class controls overall game flow
'''
def __init__(self, canvas, app):
'''
initialise game:
Create objects that should live on the game canvas
Add a flag to control start/end of game
'''
self.canvas = canvas
self.app = app
self.running = False
self.canvas.bind_all('<KeyPress-s>', self.start)
self.canvas.bind_all('<KeyPress-q>', self.quit)
self.canvas.bind_all('<KeyPress-p>', self.pause)
self.menu = "P A D D L E W H A C K\n" + \
"---------------------\n" + \
"s - start game\n" + \
"p - pause/resume\n" + \
"q - quit"
self.menu_text = self.canvas.create_text(250, 200, text=self.menu)
def start(self, evt):
if self.running:
pass
else:
self.canvas.delete("all")
self.paddle = Paddle(canvas, 'blue') # create a blue paddle
self.ball = Ball(canvas, self.paddle, 'red') # create a red ball
self.running = True
def quit(self, evt):
# only quit if the game is not running
if self.running:
pass
else:
self.app.destroy()
def pause(self, evt):
self.running = not self.running
if self.running:
self.canvas.delete(self.menu_text)
else:
pause_text = "P A U S E D\n" + \
"---------------------\n" + \
"p - resume\n" + \
"q - quit"
self.menu_text = self.canvas.create_text(250, 200, text=pause_text)
def draw(self):
'''
draw everything, then check whether game has ended
'''
self.ball.draw()
self.paddle.draw()
if self.ball.hit_bottom:
self.running = False
self.menu_text = self.canvas.create_text(250, 200, text=self.menu)
# this is where the game script begins
app = Tk()
app.title("Paddle Whack")
app.resizable(0, 0) # make window a fixed size
app.wm_attributes("-topmost", 1) # bring window to front
# create a canvas 500 by 400 pixels, with no border
canvas = Canvas(app, width=500, height=400,
bd=0, highlightthickness=0)
canvas.pack() # tell canvas to size itself
app.update() # without this the canvas height and width is not set correctly
game = Game(canvas, app)
# use our own game loop
while True:
if game.running:
game.draw()
app.update_idletasks()
app.update()
time.sleep(0.01)