Repository navigation
Expand file tree
/
Copy pathtkinter1.py
More file actions
328 lines (258 loc) · 8.45 KB
/
Copy pathtkinter1.py
File metadata and controls
328 lines (258 loc) · 8.45 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# function that start the GUI and keep window open until you close it .
"""
Canvas() - Drawing Area
The Canvas widget is where you draw shapes like the player, enemies, or ball.
"""
# 2. Canvas() – Game drawing area
"""
root = tk.Tk() # Create the main window
canvas = tk.Canvas(root, width=300, height=200, bg="lightblue")
canvas.pack()
root.mainloop() # Always last - keeps the window open
"""
# 3. create_rectangle() – Draw a rectangle (paddle or block)
"""import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=300)
canvas.pack()
paddle = canvas.create_rectangle(150, 250, 250, 260, fill="blue")
root.mainloop()
"""
# 4. create_oval() – Draw a circle/ball :
"""import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=300)
canvas.pack()
ball = canvas.create_oval(70,70, 50, 50, fill="red")
root.mainloop()
"""
# 5. move() – Move an object (used in animation) :
"""import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=300)
canvas.pack()
ball = canvas.create_oval(50, 50, 70, 70, fill="red")
canvas.move(ball,30, 20) # Move ball by (x=20, y=10)
root.mainloop()
"""
#6. bind() – Control objects with keys (left/right arrow keys)
"""import tkinter as tk
def move_left(event):
canvas.move(paddle, -20, 0)
def move_right(event):
canvas.move(paddle, 20, 0)
root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=300)
canvas.pack()
paddle = canvas.create_rectangle(150, 250, 250, 260, fill="blue")
root.bind("<Left>", move_left)
root.bind("<Right>", move_right)
root.mainloop()
"""
# 7. after() – Create a simple game loop for animation
"""import tkinter as tk
def move_ball():
canvas.move(ball, 2, 2)
root.after(50, move_ball) # Repeat after 50 milliseconds
root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=300)
canvas.pack()
ball = canvas.create_oval(50, 50, 70, 70, fill="red")
move_ball() # Start animation
root.mainloop()
"""
# 8. delete - remove from canvas
"""import tkinter as tk
def remove_ball():
canvas.delete(ball)
root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=300)
canvas.pack()
ball = canvas.create_oval(50, 50, 70, 70, fill="red")
root.after(2000, remove_ball) # Remove ball after 2 seconds
root.mainloop()
"""
import tkinter as tk
# Move paddle left
def move_left(event):
canvas.move(paddle, -25, 0)
# Move paddle right
def move_right(event):
canvas.move(paddle, 25, 0)
cnt=0
# Ball movement logic
def move_ball():
global dx, dy , cnt
canvas.move(ball, dx, dy)
pos = canvas.coords(ball)
paddle_pos = canvas.coords(paddle)
# Bounce off left and right walls
if pos[0] <= 0 or pos[2] >= 400:
dx = -dx
# Bounce off top wall
if pos[1] <= 0:
dy = -dy
# Bounce off paddle
if pos[3] >= paddle_pos[1] and pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
dy = -1.1*dy
cnt+=1
canvas.itemconfig(score_text, text=f"Score: {cnt}")
# Game over if ball touches bottom
if pos[3] >= 300:
canvas.create_text(200, 150, text="Game Over", font=("Arial", 24), fill="red")
else:
root.after(20, move_ball) # Continue moving
# Create window and canvas
root = tk.Tk()
root.title("Mini Bouncing Ball Game")
canvas = tk.Canvas(root, width=400, height=300, bg="lightyellow")
canvas.pack()
# Create paddle and ball
paddle = canvas.create_rectangle(150, 270, 250, 280, fill="blue")
ball = canvas.create_oval(190, 150, 210, 170, fill="red")
# Ball speed
dx = 4
dy = -4
score_text = canvas.create_text(50, 20, text="Score: 0", font=("Arial", 14), fill="black")
# Bind key events
root.bind("<Left>", move_left)
root.bind("<Right>", move_right)
# Start the game
move_ball()
root.mainloop()
#class obj :
"""
import tkinter as tk
class PaddleGame:
def __init__(self, root):
self.root = root
self.root.title("Paddle Game with Score")
self.canvas = tk.Canvas(self.root, width=400, height=300, bg="lightyellow")
self.canvas.pack()
# Create paddle and ball
self.paddle = self.canvas.create_rectangle(150, 270, 250, 280, fill="blue")
self.ball = self.canvas.create_oval(190, 150, 210, 170, fill="red")
# Ball speed
self.dx = 5
self.dy = -5
# Score
self.score = 0
self.score_text = self.canvas.create_text(50, 20, text="Score: 0", font=("Arial", 12), fill="black")
# Key bindings
self.root.bind("<Left>", self.move_left)
self.root.bind("<Right>", self.move_right)
# Start game loop
self.move_ball()
def move_left(self, event):
self.canvas.move(self.paddle, -20, 0)
def move_right(self, event):
self.canvas.move(self.paddle, 20, 0)
def move_ball(self):
self.canvas.move(self.ball, self.dx, self.dy)
ball_pos = self.canvas.coords(self.ball)
paddle_pos = self.canvas.coords(self.paddle)
# Bounce off walls
if ball_pos[0] <= 0 or ball_pos[2] >= 400:
self.dx = -self.dx
if ball_pos[1] <= 0:
self.dy = -self.dy
# Paddle hit detection
if ball_pos[3] >= paddle_pos[1] and paddle_pos[0] < ball_pos[2] and ball_pos[0] < paddle_pos[2]:
self.dy = -self.dy
self.score += 1
self.canvas.itemconfig(self.score_text, text="Score: " + str(self.score))
# Game over
if ball_pos[3] >= 300:
self.canvas.create_text(200, 150, text="Game Over", font=("Arial", 20), fill="red")
else:
self.root.after(20, self.move_ball)
# Run the game
root = tk.Tk()
game = PaddleGame(root)
root.mainloop()
"""
# snake game :
"""import tkinter as tk
import random
# Initialize the game variables
width = 400
height = 400
cell_size = 20
snake = [(100, 100), (80, 100), (60, 100)] # List of (x, y) coordinates
direction = "Right" # Initial direction
score = 0
# Create the main window
root = tk.Tk()
root.title("Snake Game")
# Create canvas for drawing the snake and food
canvas = tk.Canvas(root, width=width, height=height, bg="black")
canvas.pack()
# Draw initial snake and food
def draw_snake():
global snake
canvas.delete("snake") # Remove old snake
for x, y in snake:
canvas.create_rectangle(x, y, x + cell_size, y + cell_size, fill="green", tags="snake")
def create_food():
global food
while True:
x = random.randint(0, (width - cell_size) // cell_size) * cell_size
y = random.randint(0, (height - cell_size) // cell_size) * cell_size
if (x, y) not in snake:
break
food = (x, y)
canvas.create_oval(x, y, x + cell_size, y + cell_size, fill="red", tags="food")
def change_direction(event):
global direction
key = event.keysym
opposites = {"Up": "Down", "Down": "Up", "Left": "Right", "Right": "Left"}
if key != opposites.get(direction): # Prevent reversing direction
direction = key
# Move the snake
def move_snake():
global snake, direction, score
head_x, head_y = snake[0]
# Move the head
if direction == "Up":
head_y -= cell_size
elif direction == "Down":
head_y += cell_size
elif direction == "Left":
head_x -= cell_size
elif direction == "Right":
head_x += cell_size
new_head = (head_x, head_y)
snake = [new_head] + snake[:-1] # Add new head and remove tail
# Collision detection
if new_head in snake[1:] or head_x < 0 or head_x >= width or head_y < 0 or head_y >= height:
canvas.create_text(width // 2, height // 2, text="Game Over", fill="white", font=("Arial", 20))
return
# Check if the snake eats food
if new_head == food:
snake.append(snake[-1]) # Grow the snake
score += 1
canvas.itemconfig(score_text, text="Score: " + str(score))
create_food() # Create new food
draw_snake()
root.after(100, move_snake) # Repeat this function after 100 ms
# Initialize food and score
create_food()
score_text = canvas.create_text(10, 10, fill="white", text="Score: 0", anchor="nw", font=("Arial", 12))
# Bind keys
root.bind("<Left>", change_direction)
root.bind("<Right>", change_direction)
root.bind("<Up>", change_direction)
root.bind("<Down>", change_direction)
# Start the game loop
move_snake()
# Run the main loop
root.mainloop()
"""
# from tkinter import *
# from tkinter import ttk
# root = Tk()
# frm = ttk.Frame(root, padding=10)
# frm.grid()
# ttk.Label(frm, text="Hello World!").grid(column=0, row=0)
# ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0)
# root.mainloop()