Skip to content

Commit fcac762

Browse files
games
1 parent a04993e commit fcac762

60 files changed

Lines changed: 4799 additions & 43 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Games/Canon.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# This game is from the official documentation of freegames
2+
# https://pypi.org/project/freegames/
3+
4+
# pip install freegames
5+
6+
# Tap on Screen to Fire
7+
8+
# import modules
9+
from random import randrange
10+
import turtle as t
11+
from freegames import vector
12+
13+
# Set window title, color and icon
14+
t.title("Canon Fire")
15+
root = t.Screen()._root
16+
root.iconbitmap("logo-ico.ico")
17+
t.bgcolor('#99ffbb')
18+
19+
ball = vector(-200, -200)
20+
speed = vector(0, 0)
21+
targets = []
22+
# Functions
23+
# Respond to screen tap
24+
def tap(x, y):
25+
if not inside(ball):
26+
ball.x = -199
27+
ball.y = -199
28+
speed.x = (x + 200) / 25
29+
speed.y = (y + 200) / 25
30+
31+
# Return True if xy within screen
32+
def inside(xy):
33+
return -200 < xy.x < 200 and -200 < xy.y < 200
34+
35+
# Draw ball and targets
36+
def draw():
37+
t.clear()
38+
39+
for target in targets:
40+
t.goto(target.x, target.y)
41+
t.dot(20, '#8000ff')
42+
43+
if inside(ball):
44+
t.goto(ball.x, ball.y)
45+
t.dot(6, '#cc0000')
46+
47+
t.update()
48+
49+
# Move ball and targets
50+
def move():
51+
if randrange(40) == 0:
52+
y = randrange(-150, 150)
53+
target = vector(200, y)
54+
targets.append(target)
55+
56+
for target in targets:
57+
target.x -= 0.5
58+
59+
if inside(ball):
60+
speed.y -= 0.35
61+
ball.move(speed)
62+
63+
dupe = targets.copy()
64+
targets.clear()
65+
66+
for target in dupe:
67+
if abs(target - ball) > 13:
68+
targets.append(target)
69+
70+
draw()
71+
72+
for target in targets:
73+
if not inside(target):
74+
return
75+
76+
t.ontimer(move, 50)
77+
78+
t.setup(420, 420, 370, 0)
79+
t.hideturtle()
80+
t.up()
81+
t.tracer(False)
82+
t.onscreenclick(tap)
83+
move()
84+
t.done()

Games/Catch-The-Ball.py

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# import modules
2+
from tkinter import Tk, Button, Label
3+
from tkinter import Canvas
4+
from random import randint
5+
6+
# defining Tk from Tkinter
7+
tk = Tk()
8+
# Set window title and icon
9+
tk.title("Catch-The-Ball")
10+
tk.iconbitmap("logo-ico.ico")
11+
tk.resizable(False, False)
12+
13+
# For defining the canvas
14+
canvas = Canvas(tk, width=600, height=600, bg='#ffff99')
15+
canvas.pack()
16+
17+
# Variable for the vertical distance travelled by ball
18+
limit = 0
19+
20+
# Variable for horizontal distance of bar from x-axis
21+
dist = 5
22+
23+
# Variable for score
24+
score = 0
25+
26+
# Class for the Creating and moving ball
27+
class Ball:
28+
# for creation of ball on the canvas
29+
def __init__(self, canvas, x1, y1, x2, y2):
30+
self.x1 = x1
31+
self.y1 = y1
32+
self.x2 = x2
33+
self.y2 = y2
34+
self.canvas = canvas
35+
# for creation of ball object
36+
self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2,
37+
fill="red", tags='dot1')
38+
39+
# for moving the ball
40+
def move_ball(self):
41+
42+
# defining offset
43+
offset = 10
44+
global limit
45+
46+
# checking if ball lands ground or bar
47+
if limit >= 510:
48+
global dist, score, next
49+
50+
# checking that ball falls on the bar
51+
if (dist - offset <= self.x1 and
52+
dist + 40 + offset >= self.x2):
53+
54+
# incrementing the score
55+
score += 10
56+
57+
# dissappear the ball
58+
canvas.delete('dot1')
59+
60+
# calling the function for again
61+
# creation of ball object
62+
ball_set()
63+
64+
else:
65+
# dissappear the ball
66+
canvas.delete('dot1')
67+
bar.delete_bar(self)
68+
69+
# display the score
70+
score_board()
71+
return
72+
73+
# incrementing the vertical distance
74+
# travelled by ball by deltay
75+
limit += 1
76+
77+
# moving the ball in vertical direction
78+
# by taking x=0 and y=deltay
79+
self.canvas.move(self.ball, 0, 1)
80+
81+
# for continuous moving of ball again call move_ball
82+
self.canvas.after(10, self.move_ball)
83+
84+
# class for creating and moving bar
85+
86+
class bar:
87+
# method for creating bar
88+
def __init__(self, canvas, x1, y1, x2, y2):
89+
self.x1 = x1
90+
self.y1 = y1
91+
self.x2 = x2
92+
self.y2 = y2
93+
self.canvas = canvas
94+
95+
# for creating bar using create_rectangle
96+
self.rod = canvas.create_rectangle(self.x1, self.y1, self.x2, self.y2,
97+
fill="#0000ff", tags='dot2')
98+
99+
# method for moving the bar
100+
def move_bar(self, num):
101+
global dist
102+
# checking the forward or backward button
103+
if (num == 1):
104+
105+
# moving the bar in forward direction by
106+
# taking x-axis positive distance and
107+
# taking vertical distance y=0
108+
self.canvas.move(self.rod, 20, 0)
109+
110+
# incrementing the distance of bar from x-axis
111+
dist += 20
112+
else:
113+
114+
# moving the bar in backward direction by taking x-axis
115+
# negative distance and taking vertical distance y=0
116+
self.canvas.move(self.rod, -20, 0)
117+
118+
# decrementing the distance of bar from x-axis
119+
dist -= 20
120+
121+
def delete_bar(self):
122+
canvas.delete('dot2')
123+
124+
# Function to define the dimensions of the ball
125+
126+
127+
def ball_set():
128+
global limit
129+
limit = 0
130+
131+
# for random x-axis distance from
132+
# where the ball starts to fall
133+
value = randint(0, 570)
134+
135+
# define the dimensions of the ball
136+
ball1 = Ball(canvas, value, 20, value + 30, 50)
137+
138+
# call function for moving of the ball
139+
ball1.move_ball()
140+
141+
142+
# Function for displaying the score
143+
# after getting over of the game
144+
def score_board():
145+
root2 = Tk()
146+
root2.title("Catch the ball Game")
147+
root2.resizable(False, False)
148+
canvas2 = Canvas(root2, width=300, height=300)
149+
canvas2.pack()
150+
151+
w = Label(canvas2, text="\nOOPS...GAME IS OVER\n\nYOUR SCORE = "
152+
+ str(score) + "\n\n")
153+
w.pack()
154+
155+
button3 = Button(canvas2, text="PLAY AGAIN", bg="#ffb31a",
156+
command=lambda: play_again(root2))
157+
button3.pack()
158+
159+
button4 = Button(canvas2, text="EXIT", bg="#ffb31a",
160+
command=lambda: exit_handler(root2))
161+
button4.pack()
162+
163+
164+
# Function for handling the play again request
165+
def play_again(root2):
166+
root2.destroy()
167+
main()
168+
169+
170+
# Function for handling exit request
171+
def exit_handler(root2):
172+
root2.destroy()
173+
tk.destroy()
174+
175+
176+
# Main function
177+
def main():
178+
global score, dist
179+
score = 0
180+
dist = 0
181+
182+
# defining the dimensions of bar
183+
bar1 = bar(canvas, 5, 560, 45, 575)
184+
185+
# defining the text,colour of buttons and
186+
# also define the action after click on
187+
# the button by calling suitable methods
188+
button = Button(canvas, text="==>", bg="#00ff00",
189+
command=lambda: bar1.move_bar(1))
190+
191+
# placing the buttons at suitable location on the canvas
192+
button.place(x=300, y=580)
193+
194+
button2 = Button(canvas, text="<==", bg="#00ff00",
195+
command=lambda: bar1.move_bar(0))
196+
button2.place(x=260, y=580)
197+
198+
# calling the function for defining
199+
# the dimensions of ball
200+
ball_set()
201+
tk.mainloop()
202+
203+
204+
# Main code
205+
if (__name__ == "__main__"):
206+
main()

0 commit comments

Comments
 (0)