WDWD 1 W 11111111 Aa 1111111 Wwa
WDWD 1 W 11111111 Aa 1111111 Wwa
WDWD 1 W 11111111 Aa 1111111 Wwa
CONTENTS
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Scope in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Local Scope. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Global Scope. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Nonlocal Scope. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Creating the Game “Bomb” . . . . . . . . . . . . . . . . . . . . . . . . 14
Introduction
Clicker games stand out among the variety of
video game genres.
The objective of such a game is to click as fast as you
can on a certain object. At that, the faster the player
clicks, the more points he gets.
The mobile game CivCrafter is among popular clicker
games. Here the player mines resources and hires workers
(Figure 1).
Figure 1
3
Lesson # 2
Figure 2
4
Creating the Game “Bomb”
Scope in Python
To begin with, let's learn the concept of scope often used
in programming.
This topic is very important because if you do not
know this, your code will be full of errors (Figure 3)!
Figure 3
5
Lesson # 2
Figure 4
6
Creating the Game “Bomb”
Figure 5
7
Lesson # 2
Local Scope
The local scope is defined inside the function.
Let's create a function that will output names of space
shuttles. Set its name space_shuttle() and declare two
variables inside it. When the first function is called, these
names must be output to the console:
def space_shuttle():
# local
name1, name2, name3 = 'Atlantis',
' Challenger', 'Columbia'
print(name1, name2, name3)
space_shuttle()
8
Creating the Game “Bomb”
Global Scope
Variables created outside a function are global. More-
over, they can be accessed even inside a function.
Let's create variables in which aircraft numbers will be
stored and call such a variable inside the function space_
shuttle():
def space_shuttle():
# local
name1, name2, name3 = 'Atlantis',
'Challenger', 'Columbia'
print('Name: ', name1, name2, name3,
'\nOrbiter Vehicle: ', ov1, ov2, ov3)
# global
ov1, ov2, ov3 = 'OV-104', 'OV-099', 'OV-102'
space_shuttle()
9
Lesson # 2
Figure 6
10
Creating the Game “Bomb”
# global
mission = 'Humans in Space'
space_mission()
print('\nMission: ', mission,
'\nID: ', id(mission))
11
Lesson # 2
12
Creating the Game “Bomb”
Nonlocal Scope
Sometimes, you should create one more function
inside another function and override a variable inside
the former. The nonlocal keyword was added for cases
exactly like this.
Let's create a function and a variable with the comet
name in it. Create one more function inside it and override
this name.
def comet_name1():
name = "Halley's Comet"
def comet_name2():
nonlocal name
name = '81P/Wild'
comet_name2()
return name
13
Lesson # 2
Figure 7
14
Creating the Game “Bomb”
Figure 8
15
Lesson # 2
def update_display():
def update_bomb():
def update_point():
def click():
def is_alive():
16
Creating the Game “Bomb”
Create the main window of the game. Set its name and
size:
root = Tk()
root.title('Bang Bang')
root.geometry('500x550')
Figure 9
17
Lesson # 2
Figure 10
Great!
To design the game, we need an image of a bomb. There is
a PhotoImage() class in the tkinter module, and it displays
images. This class can read only .gif, .pgm, or .ppm image
formats.
Create an img folder in the project folder and add three
images in it (Figure 11):
Figure 11
18
Creating the Game “Bomb”
Figure 12
19
Lesson # 2
click_button.pack()
Figure 13
You can bind the widget and a certain event using the
bind() method. The event in our game is pressing the Enter
key because the game begins after it is pressed.
20
Creating the Game “Bomb”
root.mainloop()
21
Lesson # 2
update_score()
update_display()
press_return = False
22
Creating the Game “Bomb”
23
Lesson # 2
Figure 14
24
Creating the Game “Bomb”
After the time is up, you can press Enter again and replay
the game.
Figure 15
25
Lesson # 2
Figure 16
26
Lesson # 2
Creating the Game “Bomb”
© STEP IT Academy
www.itstep.org
All rights to protected pictures, audio, and video belong to their authors or legal owners.
Fragments of works are used exclusively in illustration purposes to the extent justified by
the purpose as part of an educational process and for educational purposes in accordance
with Article 1273 Sec. 4 of the Civil Code of the Russian Federation and Articles 21 and 23
of the Law of Ukraine “On Copyright and Related Rights”. The extent and method of cited
works are in conformity with the standards, do not conflict with a normal exploitation of
the work, and do not prejudice the legitimate interests of the authors and rightholders.
Cited fragments of works can be replaced with alternative, non-protected analogs, and
as such correspond the criteria of fair use.
All rights reserved. Any reproduction, in whole or in part, is prohibited. Agreement of the
use of works and their fragments is carried out with the authors and other right owners.
Materials from this document can be used only with resource link.
Liability for unauthorized copying and commercial use of materials is defined according
to the current legislation of Ukraine.