B.E Cse Batchno 295
B.E Cse Batchno 295
SCHOOL OF COMPUTING
BONAFIDE CERTIFICATE
This is to certify that this Professional Training Report is the bonafide work of
PENMETSA YASASWI SATYA PAVAN VARMA(Reg.No.37110562) and
NIMMAKAYALAA PARDHA VENKATA SAI(Reg.No.37110511) who carried out
the project entitled as “SPACE INVADERS” under our supervision from JULY 2020
to AUGUST 2021.
Internal Guide
DR.G.MATHIVANAN ,M.TECH.,PHD.,
DATE:
PLACE: SIGNATURE OF THE
CANDIDATE
ACKNOWLEDGEMENT
➢ We are pleased to acknowledge our sincere thanks to Board of Management
of SATHYABAMA INSTITUTE OF SCIENCE AND TECHNOLOGY for their
kind encouragement in doing this project and for completing it successfully.
we are grateful to them.
➢ We would like to express our sincere and deep sense of gratitude to our
Project Guide DR.G.MATHIVANAN,M.TECH.,PHD for his amazing and
valuable guidance, suggestions and constant encouragement paved way for
the successful completion of our project work.
i
ii
iii
ABSTRACT
Computer gaming is a key component of the rapidly growing
entertainment industry. While building computer games has
typically been a commercial endeavor, we believe that designing
and constructing a computer game is also a useful activity for
educating students about geometric modeling and computer
graphics. In particular, students are exposed to the practical issues
surrounding topics such as geometric modeling, rendering, collision
detection, character animation and graphical design. Moreover,
building an advanced game provides students exposure to the
real-world side of software engineering that they are typically
shielded from in the standard computer class. In this paper, we
describe our experiences with teaching a computer science class
that focuses on designing and building the best game possible in
the course of a semester. The paper breaks down a typical game
into various components that are suited for individual student
projects and discusses the use of modern graphical design tools
such as Maya in building art for the game. We conclude with a
rough timeline for developing a game during the course of a
semester and review some of the lessons learned during the three
years we have taught the class.
iv
TABLE OF CONTENTS
TITLE
ABSTRACT i i
LIST OF FIGURES v
LIST OF TABLES v
1. INTRODUCTION
1.1 PYTHON PROGRAMMING 1
1.1.1 OVER VIEW OF PROJECT 1
1. 1. 2 FEATURES 1
1.1.3 APPLICATIONS 2
1.1.4 LITERATURE REVIEW 4
1.1.5 ENVIRONMENT SETUP 4
1.1.6 DISADVANTAGES 6
v
3 SYSTEM IMPLEMENTATION
3.1 REQUIREMENTS 13
3.2 SOFT WARE PLATFORM 13
3.3 GUI PROGRAMMING 14
3.4 GETTING STARTED WITH TKINTER 14
3.4.1 TKINTER WIDGETS 16
3.4.2 STANDARD ATTRIBUTES 18
3.4.3 GEOMETRY MANAGEMENT 18
3.5 SNAPSHOTS AND EXPLANATIONS 19
vi
LIST OF FIGURES
vii
LIST OF TABLES
viii
CHAPTER 1
INTRODUCTION
1.1 PYTHON PROGRAMMING
Python is a high-level, interpreted, interactive and
object-oriented scripting language. Python is designed to be highly readable. It uses
English keywords frequently whereas the other languages use punctuations. It has
fewer syntactical constructions than other languages. Python is Interpreted- Python
is processed at runtime by the interpreter. You do not need to compile your program
before executing it. This is as same as PERL and PHP.Python is interactive- You
can set at a Python prompt and interact with the interpreter directly to write your
programs. Python is Object-Oriented- Python supports Object-Oriented style or
technique of programming that encapsulates code within objects. Python is a
Beginner’s Language- Python is a great language for the beginner-level
programmers and supports the development of a wide range of applications from
simple text processing to WWW browsers to games.
1
Portable – Python can run on a wide variety of hardware platforms and has the
same interface on the platforms.
Extendable – You can add low level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more efficient.
Databases – Python provides interfaces to all major commercial databases.
GUI Programming – Python supports GUI applications that can be created and
ported to many system calls, libraries and windows systems, such as windows MFC,
Macintosh, and the X window system of Unix.
Scalable – Python provides a better structure and support for large programs than
shell scripting.
Apart from the above-mentioned features, Python has a big list of good features a
few are listed below –
• It supports functional and structed programming methods as well as OOP.
• It can be used as a scripting language or can be compiled to byte-code for
building large applications.
• It can be used as a scripting language or can be compiled to byte-code for
building large applications.
• It provides very high-level dynamic data types and supports dynamic type
checking.
• It supports automatic garbage collection.
• It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
1.1.3 APPLICATIONS
Business Applications
Python is also used to build ERP and e-commerce systems:
3
• Odoo is an all-in-one management software that offers a range of business
applications that form a complete suite of enterprise management
applications.
• Tryton is a three-tier high-level general-purpose application platform.
➢ Python 3 s available for Windows, Mac OS and most of the flavors of Linux
operating system. Even though Python 2 is available for many other OSs,
Python3 support either has not been made available for them or has been
dropped.
6
To know the Python Programming and to run the programs in Python we need
to know about Python completely. We have different types of topics in Python to
study, namely, Operators, Expressions, Control Statements, Data struce
Structures, Functions, Packages, Object Oriented Programming, Inheritance and
Polymorphism, Exception Handling, Standard Libraries, Testing.
2.2 OVERVIEW
Here are important environment variables, which are recognized by Python-
Table 2.1 Python Environment Variables
S. no: Variable & Description
1. PYTHONPATH
It has a role similar to PATH. This variable tells the Python interpreter
where to locate the module files imported into a program. It should
include the Python source library directory and the directories
containing Python source code. PYTHONPATH is sometimes present
by the Python installer.
2. PYTHONSTARTUP
It contains the path of an initialization file containing Python source
code. It is executed every time you start the interpreter. It is named as
.pythonrc.py in Unix and it contains commands that load utilities or
modify PYTHONPATH
3. PYTHONCASEOK
It is used in Windows to instruct Python to find the first case-insensitive
match in an import statement. Set this variable to any value to activate
it.
4. PYTHONHOME
It is an alternative module search path. It is a usually embedded in the
PYTHONSTARTUP or PYTHONPATH directories to make switching
module libraries easy.
8
python% script.py #Unix/Linux
or
C:>python script.py #Windows/DOS
10
item_two+\
item_three
The statements contained within the [],{}, or () brackets do not need to use
the line continuation character. For example –
days=[‘Monday’,’Tuesday’,’Wednesday’,’Thursday’,’Friday’]
You can also program your script in such a way that it should accept various
options. Command Line Arguments is an advanced topic
CHAPTER 3
SYSTEM IMPLEMENTATION
3.1 REQUIREMENTS
Processors:
12
• Intel core i5 processor 4300M at 2.60 GHz or 2.59 GHz (1 socket, 2 cores, 2
threads per core), 8GB of DRAM
• Intel Xeon processor E5-2698 v3 at 2.30 GHz (2 sockets, 16 cores each, 1
thread per core), 64 GB of DRAM
• Intel Xeon Phi processor 7210 at 1.30 GHz (1 socket, 64 cores, 4 threads per
core), 32 GB of DRAM, 16 GB of MCDRAM(flat mode enabled)
•
The tkinter module contains the classes for creating GUIs. The Tk class creates
a window for holding GUI widgets (i.e., visual components).
14
Consider the following program to understand Tkinter:
from tkinter import * # import all definitions from tkinter
window=Tk() #create a window
label=Label(window,text=”Welcome to Python”) #Create a label
button=Button(window,text=”Click Me”) #Create a button
label.pack() #place the
label in the window
button.pack() #place the
button in the window
window.mainloop() # create an event
loop
When u run the program, a label and a button appear in the Tkinter window,
as shown below
15
Tkinter GUI programming is event driven. After the user interface is displayed, the
program waits for user interactions such as mouse clicks and key presses. This is
specified in the following statement
window.mainloop()
The statement creates an event loop. The event loop processes events
continuously until you close the main window.
3.4.1 Tkinter Widgets:
Tkinter provides various controls, such as buttons, labels and text boxes
used in a GUI application. There controls are commonly called widgets.
There are currently 15 types of widgets in Tkinter. We present these widgets as
well as a brief description in the following table –
Table 3.1 Types of widgets in Tkinter
S.No: Operator & Description
1 Button:
The Button widget is used to display buttons in your application.
2 Canvas:
The Canvas widget is used to draw shapes, such as lines, ovals,
polygons and rectangles, in your application.
3 Checkbutton:
The Checkbutton widget is used to display a number of options as
checkboxes. The user can select multiple options at a time.
4 Entry:
The Entry widget is used to display a single- line text field for accepting
values from a user.
5 Frame:
The Frame widget is used as a container widget to organize other
widgets.
6 Label:
The Label widget is used to provide a single- line caption for other
widgets. It can also contain images.
7 Listbox:
The Listbox widget is used to provide a list of options to a user.
8 Menubutton:
16
The Menubutton widget is used to display menus in your application.
9 Menu:
The Menu widget is used to provide various commands to a user.
These commands are contained inside Menubutton.
10 Message:
The Message widget is used to display multiline text fields for
accepting values from a user.
11 Radiobutton:
The Radiobutton widget is used to display a number of options as radio
buttons. The user can select only one option at a time.
12 Scale:
The Scale widget is used to provide a slider widget.
13 Scrollbar:
The Scrollbar widget is used to add scrolling capability to various
widgets, such as list boxes.
14 Text:
The Text widget is used to display text in multiple lines.
15 Toplevel:
The Toplevel widget is used to provide a separate window container.
16 Spinbox:
The Spinbox widget is a variant of the standard Tkinter Entry widget,
which can be used to select from a fixed number of values.
17 PanedWindow:
A PanedWindow is a container widget that may contain any number of
panes, arranged horizontally or vertically.
18 LabelFrame:
A label frame is a simple container widget. Its primary purpose is to act
as a spacer or container for complex window layouts.
19 tkMessageBox:
This module is used to display message boxes in your applications.
17
Let us look at how some of their common attributes, such as sizes, colors and
fonts are specified.
• Dimensions
• Colors
• Fonts
• Anchors
• Relief styles
• Bitmaps
• Cursors
18
Fig 3.2 INITIALIZING
19
Fig 3.4 CODE FOR DEFINING FUNCTIONS
20
Fig 3.6 OUTPUT AS GAME ENDS
21
CHAPTER 4
SUMMARY AND CONCLUSION
4.1 SUMMARY
Just like how Python comes with several modules like random, math, or time that
provide additional functions for your programs, the Pygame framework includes
several modules with functions for drawing graphics, playing sounds, handling
mouse input, and other things
4.2 CONCLUSION
We conclude that Python is a easy programming language that can be used to
develop Games. This project is about creating 2d game called spave invaders in
python programming.Perhaps, the most import lesson is that following good
software design and coding practices is essential to the success of the game
22
APPENDIX
SOURCE CODE
1 import pygame
2 import random
3 import math
4 from pygame import mixer
5
6 # initializing pygame
7 pygame.init()
8
9 # creates the screen
10 screen = pygame.display.set_mode((800, 600))
11
12 # Title and Icon
13 pygame.display.set_caption("Galactic Invaders")
14 icon = pygame.image.load('Space Ship.png')
15 pygame.display.set_icon(icon)
16
17 # Background
18 background = pygame.image.load('Background.png')
19
20
21 # running = True
22 # Background sound
23 mixer.music.load('Background.wav')
24 mixer.music.play(-1)
25
26
27 # Player
28 playerImg = pygame.image.load('Player.png')
29 playerX = 780
30 playerY = 530
31 playerX_change = 0
32
33
34 # Enemy
35 EnemyImg = []
36 EnemyX = []
23
37 EnemyY = []
38 EnemyX_change = []
39 EnemyY_change = []
40 num_of_enemies = 6
41 for i in range(num_of_enemies):
42 EnemyImg.append(pygame.image.load('Enemy.png'))
43 EnemyX.append(random.randint(0, 736))
44 EnemyY.append(random.randint(0, 10))
45 EnemyX_change.append(2)
46 EnemyY_change.append(40)
47
48
49 # Bullet
50 # Ready- You cant see the bullet on the screen
51 # Fire- the bullet is moving
52 BulletImg = pygame.image.load('Bullet.png')
53 BulletX = 0
54 BulletY = 530
55 BulletX_change = 0
56 BulletY_change = 20
57 Bullet_state = "ready"
58
59
60 # game over sound
61 GameOver_sound = mixer.Sound('Gameover.wav')
62 game_state = 'ready'
63 # GameOver_sound.play()
64
65
66 # Score
67 score_value = 0
68 font = pygame.font.Font('freesansbold.ttf', 32)
69 textX = 10
70 textY = 10
71
72
73 # Game Over text
74 over_font = pygame.font.Font('freesansbold.ttf', 64)
24
75
76
77 def show_score(X, Y):
78 score = font.render("Score : " + str(score_value), True, (255, 255, 255))
79 screen.blit(score, (X, Y))
80
81
82 def game_over_text():
83 over_text = over_font.render("GAME OVER", True, (255, 255, 255))
84 screen.blit(over_text, (200, 250))
85
86
87 # function for player
88 def player(x, y):
89 screen.blit(playerImg, (x, y))
90
91
92 # function for Enemy
93 def Enemy(x, y, i):
94 screen.blit(EnemyImg[i], (x, y))
95
96
97 def fire_bullet(X, y):
98 global Bullet_state
99 Bullet_state = 'fire'
100 screen.blit(BulletImg, (X + 16, y + 10))
101
102
103 def game_over_sound():
104 global game_state
105 game_state = 'sing'
106 mixer.music.stop()
107 GameOver_sound.play()
108
109
110 def iscollision(EnemyX, EnemyY, BulletX, BulletY):
111 distance = math.sqrt((math.pow(BulletX - EnemyX, 2)) + (math.pow(BulletY -
112 EnemyY, 2)))
25
113 if distance < 26:
114 return True
115 else:
116 return False
117
118
119 # Game Loop
120 running = True
121 while running:
122 # RGB red green blue
123 screen.fill((0, 0, 0))
124
125 # Background image
126 screen.blit(background, (0, 0))
127
128 for event in pygame.event.get():
129 if event.type == pygame.QUIT:
130 running = False
131
132 # whenever a key is pressed key down is activated
133 if event.type == pygame.KEYDOWN:
134 if event.key == pygame.K_LEFT:
135 playerX_change = -5
136 if event.key == pygame.K_RIGHT:
137 playerX_change = 5
138 if event.key == pygame.K_SPACE:
139 if Bullet_state is 'ready':
140 Bullet_sound = mixer.Sound('Gunshot.wav')
141 Bullet_sound.play()
142 BulletX = playerX
143 fire_bullet(BulletX, BulletY)
144
145
146 # whenever a key is released key up is activated
147 if event.type == pygame.KEYUP:
148 if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
149 playerX_change = 0
150
26
151
152 # 5 = 5 + 0.1
153 # 5 = 5 + -0.1
154 playerX += playerX_change
155 if playerX <= 0:
156 playerX = 0
157 if playerX >= 736:
158 playerX = 736
159
160
161 # Enemy movements
162 for i in range(num_of_enemies):
163 # Game Over
164 if EnemyY[i] > 450:
165 for j in range(num_of_enemies):
166 EnemyY[j] = 1000
167 game_over_text()
168 break
169
170 EnemyX[i] += EnemyX_change[i]
171 if EnemyX[i] <= 0:
172 EnemyX_change[i] = 2
173 EnemyY[i] += EnemyY_change[i]
174 elif EnemyX[i] >= 736:
175 EnemyX_change[i] = -2
176 EnemyY[i] += EnemyY_change[i]
177 # Collision
178 collision = iscollision(EnemyX[i], EnemyY[i], BulletX, BulletY)
179 if collision:
180 Explosion_sound = mixer.Sound('Explosion.wav')
181 Explosion_sound.play()
182 BulletY = 480
183 Bullet_state = 'ready'
184 score_value += 1
185 EnemyX[i] = random.randint(0, 30)
186 EnemyY[i] = random.randint(0, 0)
187
188 Enemy(EnemyX[i], EnemyY[i], i)
27
189
190 # Bullet movements
191 if BulletY <= -20:
192 BulletY = 480
193 Bullet_state = 'ready'
194
195 if Bullet_state is "fire":
196 fire_bullet(BulletX, BulletY)
197 BulletY -= BulletY_change
198
199 # Player function
200 player(playerX, playerY)
201
202 show_score(textX, textY)
OUTPUT
203
28
204
29
REFERENCES
5. FOLEY, J., VAN DAM, A., FEINER, S., AND HUGHES, J. 1990.
Computer Graphics: Principles and Practice, 2nd Edition. Addison Wesley.
30