You are on page 1of 12

7/2/2021 Mario Game In Python With Source Code | FREE | 2020

Mario Game In Python With Source Code x


By angel jude suarez - August 11, 2020

Mario Game In Python With Source Code


The Mario Game In Python is written in Python, This Mario Game Code In Python is
design in Graphical User Interface (GUI) that uses PyGame library. Talking about the
gameplay, it’s a single player game, where the player (Mario) has to dodge fireballs coming
out from the dragon. Each level comes with more difficulties, the area gets smaller and
smaller as soon as there’s an increment in level. In this Super Mario Python Tutorial you
can learn on How To Make Super Mario Game In Python.

A Mario Game Program In Python simple and clean GUI is provided for easy gameplay.
The gameplay design is so simple that the user won’t find it difficult to use and understand.
Different images are used in the development of this game project, the gaming
environment is just like the Mario game.

Watch the video here to see the full running super mario game in python.

Anyway if you want level up your knowledge in programming especially games in python,
try this new article I’ve made for you Code For Game in Python: Python Game Projects With
Source Code

This Super Mario In Python also includes a downloadable Mario Game In Python source
code for free, just find the downloadable source code below and click to start downloading.

https://itsourcecode.com/free-projects/python-projects/mario-game-in-python-with-source-code/ 1/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020

To start creating a Mario Game In Python, make sure that you have PyCharm IDE
installed in your computer.

Steps on how to create a Mario Game In Python

Mario Game In Python With Source Code

https://itsourcecode.com/free-projects/python-projects/mario-game-in-python-with-source-code/ 2/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020

Step 1: Create a project name. x


First open Pycharm IDE and then create a “project name” after creating a project
name click the “create” button.

Step 2: Create a python file.


Second after creating a project name, “right click” your project name and then click
“new” after that click the “python file“.

Step 3: Name your python file.


Third after creating a python file, Name your python file after that click “enter“.

Step 4: The actual code.


You are free to copy the code given below and download the full source code below.

https://itsourcecode.com/free-projects/python-projects/mario-game-in-python-with-source-code/ 3/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020

x
The Code Given Below Is For Importing Modules
1 import pygame
2 import sys

The code given which is importing all modules.

The Code Given Below Is For The Starting Game Of Mario


1 def start_game():
2     canvas.fill(BLACK)
3     start_img = pygame.image.load('start.png')
4     start_img_rect = start_img.get_rect()
5     start_img_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
6     canvas.blit(start_img, start_img_rect)
7     while True:
8         for event in pygame.event.get():
9             if event.type == pygame.QUIT:
10                 pygame.quit()
11                 sys.exit()
12             if event.type == pygame.KEYDOWN:
13                 if event.key == pygame.K_ESCAPE:
14                     pygame.quit()
15                     sys.exit()
16                 game_loop()
17         pygame.display.update()

In this module which is the starting game of super mario.

This Will Be The Output

The Code Given Below Is For The Level Of The Game


1 def check_level(SCORE):
2     global LEVEL
3     if SCORE in range(0, 10):
4         cactus_img_rect.bottom = 50
5         fire_img_rect.top = WINDOW_HEIGHT - 50
6         LEVEL = 1
7     elif SCORE in range(10, 20):
8         cactus_img_rect.bottom = 100
9         fire_img_rect.top = WINDOW_HEIGHT - 100
10         LEVEL = 2
11     elif SCORE in range(20, 30):
12         cactus_img_rect.bottom = 150
13         fire_img_rect.top = WINDOW_HEIGHT - 150
14         LEVEL = 3

https://itsourcecode.com/free-projects/python-projects/mario-game-in-python-with-source-code/ 4/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020
15     elif SCORE > 30: x
16         cactus_img_rect.bottom = 200
17         fire_img_rect.top = WINDOW_HEIGHT - 200
18         LEVEL = 4

In this module which is the level of the game if you pass the challenge of the given level.

The Code Given Below Is For The Main Module Of The


Game
1 def game_loop():
2     while True:
3         global dragon
4         dragon = Dragon()
5         flames = Flames()
6         mario = Mario()
7         add_new_flame_counter = 0
8         global SCORE
9         SCORE = 0
10         global  HIGH_SCORE
11         flames_list = []
12         pygame.mixer.music.load('mario_theme.wav')
13         pygame.mixer.music.play(-1, 0.0)
14         while True:
15             canvas.fill(BLACK)
16             check_level(SCORE)
17             dragon.update()
18             add_new_flame_counter += 1
19  
20             if add_new_flame_counter == ADD_NEW_FLAME_RATE:
21                 add_new_flame_counter = 0
22                 new_flame = Flames()
23                 flames_list.append(new_flame)
24             for f in flames_list:
25                 if f.flames_img_rect.left <= 0:
26                     flames_list.remove(f)
27                     SCORE += 1
28                 f.update()
29  
30             for event in pygame.event.get():
31                 if event.type == pygame.QUIT:
32                     pygame.quit()
33                     sys.exit()
34                 if event.type == pygame.KEYDOWN:
35                     if event.key == pygame.K_UP:
36                         mario.up = True
37                         mario.down = False
38                     elif event.key == pygame.K_DOWN:
39                         mario.down = True
40                         mario.up = False
41                 if event.type == pygame.KEYUP:
42                     if event.key == pygame.K_UP:
43                         mario.up = False
44                         mario.down = True
45                     elif event.key == pygame.K_DOWN:
46                         mario.down = True
47                         mario.up = False
48  
49             score_font = font.render('Score:'+str(SCORE), True, GREEN)
50             score_font_rect = score_font.get_rect()
51             score_font_rect.center = (200, cactus_img_rect.bottom + score_font_rect.height/2)
52             canvas.blit(score_font, score_font_rect)
53  
54             level_font = font.render('Level:'+str(LEVEL), True, GREEN)
55             level_font_rect = level_font.get_rect()
56             level_font_rect.center = (500, cactus_img_rect.bottom + score_font_rect.height/2)
57             canvas.blit(level_font, level_font_rect)
58  
59             top_score_font = font.render('Top Score:'+str(topscore.high_score),True,GREEN)
60             top_score_font_rect = top_score_font.get_rect()
61             top_score_font_rect.center = (800, cactus_img_rect.bottom + score_font_rect.heigh
62             canvas.blit(top_score_font, top_score_font_rect)
63  
64             canvas.blit(cactus_img, cactus_img_rect)
65             canvas.blit(fire_img, fire_img_rect)

https://itsourcecode.com/free-projects/python-projects/mario-game-in-python-with-source-code/ 5/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020
66             mario.update() x
67             for f in flames_list:
68                 if f.flames_img_rect.colliderect(mario.mario_img_rect):
69                     game_over()
70                     if SCORE > mario.mario_score:
71                         mario.mario_score = SCORE
72             pygame.display.update()
73             CLOCK.tick(FPS)
74  

In This Module which is the main module of the game that consist of boolean and other loop
and conditions.

The Code Given Below Is For The Interface of The Game


Over
1 def game_over():
2     pygame.mixer.music.stop()
3     music = pygame.mixer.Sound('mario_dies.wav')
4     music.play()
5     topscore.top_score(SCORE)
6     game_over_img = pygame.image.load('end.png')
7     game_over_img_rect = game_over_img.get_rect()
8     game_over_img_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
9     canvas.blit(game_over_img, game_over_img_rect)
10     while True:
11         for event in pygame.event.get():
12             if event.type == pygame.QUIT:
13                 pygame.quit()
14                 sys.exit()
15             if event.type == pygame.KEYDOWN:
16                 if event.key == pygame.K_ESCAPE:
17                     pygame.quit()
18                     sys.exit()
19                 music.stop()
20                 game_loop()
21         pygame.display.update()

In This Module which is the interface of the game over.

This Will Be The Output

https://itsourcecode.com/free-projects/python-projects/mario-game-in-python-with-source-code/ 6/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020

Mario-Game-Program-In-Python-Output-1

Complete Source Code


1 import pygame
2 import sys
3 pygame.init()
4  
5 WINDOW_WIDTH = 1200
6 WINDOW_HEIGHT = 600
7 FPS = 20
8 BLACK = (0, 0, 0)
9 GREEN = (0, 255, 0)
10 ADD_NEW_FLAME_RATE = 25
11 cactus_img = pygame.image.load('cactus_bricks.png')
12 cactus_img_rect = cactus_img.get_rect()
13 cactus_img_rect.left = 0
14 fire_img = pygame.image.load('fire_bricks.png')
15 fire_img_rect = fire_img.get_rect()
16 fire_img_rect.left = 0
17 CLOCK = pygame.time.Clock()
18 font = pygame.font.SysFont('forte', 20)
19  
20 canvas = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
21 pygame.display.set_caption('Mario')
22  
23  
24 class Topscore:
25     def __init__(self):
26         self.high_score = 0
27     def top_score(self, score):
28         if score > self.high_score:
29             self.high_score = score
30         return self.high_score
31  
32 topscore = Topscore()
33  
34  
35 class Dragon:
36     dragon_velocity = 10
37  
38     def __init__(self):
39         self.dragon_img = pygame.image.load('dragon.png')
40         self.dragon_img_rect = self.dragon_img.get_rect()
41         self.dragon_img_rect.width -= 10
42         self.dragon_img_rect.height -= 10
43         self.dragon_img_rect.top = WINDOW_HEIGHT/2
44         self.dragon_img_rect.right = WINDOW_WIDTH
45         self.up = True
46         self.down = False
47  
48     def update(self):
49         canvas.blit(self.dragon_img, self.dragon_img_rect)
50         if self.dragon_img_rect.top <= cactus_img_rect.bottom:
51             self.up = False
52             self.down = True
53         elif self.dragon_img_rect.bottom >= fire_img_rect.top:
54             self.up = True
55             self.down = False

https://itsourcecode.com/free-projects/python-projects/mario-game-in-python-with-source-code/ 7/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020
56
57
 
        if self.up:
x
58             self.dragon_img_rect.top -= self.dragon_velocity
59         elif self.down:
60             self.dragon_img_rect.top += self.dragon_velocity
61  
62  
63 class Flames:
64     flames_velocity = 20
65  
66     def __init__(self):
67         self.flames = pygame.image.load('fireball.png')
68         self.flames_img = pygame.transform.scale(self.flames, (20, 20))
69         self.flames_img_rect = self.flames_img.get_rect()
70         self.flames_img_rect.right = dragon.dragon_img_rect.left
71         self.flames_img_rect.top = dragon.dragon_img_rect.top + 30
72  
73  
74     def update(self):
75         canvas.blit(self.flames_img, self.flames_img_rect)
76  
77         if self.flames_img_rect.left > 0:
78             self.flames_img_rect.left -= self.flames_velocity
79  
80  
81 class Mario:
82     velocity = 10
83  
84     def __init__(self):
85         self.mario_img = pygame.image.load('maryo.png')
86         self.mario_img_rect = self.mario_img.get_rect()
87         self.mario_img_rect.left = 20
88         self.mario_img_rect.top = WINDOW_HEIGHT/2 - 100
89         self.down = True
90         self.up = False
91  
92     def update(self):
93         canvas.blit(self.mario_img, self.mario_img_rect)
94         if self.mario_img_rect.top <= cactus_img_rect.bottom:
95             game_over()
96             if SCORE > self.mario_score:
97                 self.mario_score = SCORE
98         if self.mario_img_rect.bottom >= fire_img_rect.top:
99             game_over()
100             if SCORE > self.mario_score:
101                 self.mario_score = SCORE
102         if self.up:
103             self.mario_img_rect.top -= 10
104         if self.down:
105             self.mario_img_rect.bottom += 10
106  
107  
108 def game_over():
109     pygame.mixer.music.stop()
110     music = pygame.mixer.Sound('mario_dies.wav')
111     music.play()
112     topscore.top_score(SCORE)
113     game_over_img = pygame.image.load('end.png')
114     game_over_img_rect = game_over_img.get_rect()
115     game_over_img_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
116     canvas.blit(game_over_img, game_over_img_rect)
117     while True:
118         for event in pygame.event.get():
119             if event.type == pygame.QUIT:
120                 pygame.quit()
121                 sys.exit()
122             if event.type == pygame.KEYDOWN:
123                 if event.key == pygame.K_ESCAPE:
124                     pygame.quit()
125                     sys.exit()
126                 music.stop()
127                 game_loop()
128         pygame.display.update()
129  
130  
131 def start_game():
132     canvas.fill(BLACK)
133     start_img = pygame.image.load('start.png')
134     start_img_rect = start_img.get_rect()
135     start_img_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
136     canvas.blit(start_img, start_img_rect)

https://itsourcecode.com/free-projects/python-projects/mario-game-in-python-with-source-code/ 8/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020
137
138
    while True:
        for event in pygame.event.get():
x
139             if event.type == pygame.QUIT:
140                 pygame.quit()
141                 sys.exit()
142             if event.type == pygame.KEYDOWN:
143                 if event.key == pygame.K_ESCAPE:
144                     pygame.quit()
145                     sys.exit()
146                 game_loop()
147         pygame.display.update()
148  
149  
150 def check_level(SCORE):
151     global LEVEL
152     if SCORE in range(0, 10):
153         cactus_img_rect.bottom = 50
154         fire_img_rect.top = WINDOW_HEIGHT - 50
155         LEVEL = 1
156     elif SCORE in range(10, 20):
157         cactus_img_rect.bottom = 100
158         fire_img_rect.top = WINDOW_HEIGHT - 100
159         LEVEL = 2
160     elif SCORE in range(20, 30):
161         cactus_img_rect.bottom = 150
162         fire_img_rect.top = WINDOW_HEIGHT - 150
163         LEVEL = 3
164     elif SCORE > 30:
165         cactus_img_rect.bottom = 200
166         fire_img_rect.top = WINDOW_HEIGHT - 200
167         LEVEL = 4
168  
169  
170  
171  
172  
173 def game_loop():
174     while True:
175         global dragon
176         dragon = Dragon()
177         flames = Flames()
178         mario = Mario()
179         add_new_flame_counter = 0
180         global SCORE
181         SCORE = 0
182         global  HIGH_SCORE
183         flames_list = []
184         pygame.mixer.music.load('mario_theme.wav')
185         pygame.mixer.music.play(-1, 0.0)
186         while True:
187             canvas.fill(BLACK)
188             check_level(SCORE)
189             dragon.update()
190             add_new_flame_counter += 1
191  
192             if add_new_flame_counter == ADD_NEW_FLAME_RATE:
193                 add_new_flame_counter = 0
194                 new_flame = Flames()
195                 flames_list.append(new_flame)
196             for f in flames_list:
197                 if f.flames_img_rect.left <= 0:
198                     flames_list.remove(f)
199                     SCORE += 1
200                 f.update()
201  
202             for event in pygame.event.get():
203                 if event.type == pygame.QUIT:
204                     pygame.quit()
205                     sys.exit()
206                 if event.type == pygame.KEYDOWN:
207                     if event.key == pygame.K_UP:
208                         mario.up = True
209                         mario.down = False
210                     elif event.key == pygame.K_DOWN:
211                         mario.down = True
212                         mario.up = False
213                 if event.type == pygame.KEYUP:
214                     if event.key == pygame.K_UP:
215                         mario.up = False
216                         mario.down = True
217                     elif event.key == pygame.K_DOWN:

https://itsourcecode.com/free-projects/python-projects/mario-game-in-python-with-source-code/ 9/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020
218                         mario.down = True x
219                         mario.up = False
220  
221             score_font = font.render('Score:'+str(SCORE), True, GREEN)
222             score_font_rect = score_font.get_rect()
223             score_font_rect.center = (200, cactus_img_rect.bottom + score_font_rect.height/2
224             canvas.blit(score_font, score_font_rect)
225  
226             level_font = font.render('Level:'+str(LEVEL), True, GREEN)
227             level_font_rect = level_font.get_rect()
228             level_font_rect.center = (500, cactus_img_rect.bottom + score_font_rect.height/2
229             canvas.blit(level_font, level_font_rect)
230  
231             top_score_font = font.render('Top Score:'+str(topscore.high_score),True,GREEN)
232             top_score_font_rect = top_score_font.get_rect()
233             top_score_font_rect.center = (800, cactus_img_rect.bottom + score_font_rect.heig
234             canvas.blit(top_score_font, top_score_font_rect)
235  
236             canvas.blit(cactus_img, cactus_img_rect)
237             canvas.blit(fire_img, fire_img_rect)
238             mario.update()
239             for f in flames_list:
240                 if f.flames_img_rect.colliderect(mario.mario_img_rect):
241                     game_over()
242                     if SCORE > mario.mario_score:
243                         mario.mario_score = SCORE
244             pygame.display.update()
245             CLOCK.tick(FPS)
246  
247  
248 start_game()
249  
250  
251  

Run Quick Virus Scan for secure Download


Run Quick Scan for secure Download

Downloadable Source Code

DOWNLOAD

I have here the list of Best Python Project with Source code free to download for free, I
hope this can help you a lot.

Summary
The Mario Game In Python is written in python programming language, Python is very
smooth to research the syntax emphasizes readability and it is able to reduces time
ingesting in developing.Also in this tutorial is the simplest way for the beginners or the
student to enhance their logical skills in programming.

Related Articles

https://itsourcecode.com/free-projects/python-projects/mario-game-in-python-with-source-code/ 10/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020

Hangman Game In Python With Source Code x


Aircraft War Game in Python with Source Code

Snake Game In Python Code

How to Make Bouncing Ball Game in Python with Source Code

How to Create Rock-Paper-Scissor Game in Python

Inquiries
If you have any questions or suggestions about Mario Game In Python , please feel free
to leave a comment below.

Share this:

 Twitter  Facebook

Like this:

Loading...

Best Python Projects for Best Python Projects With Code For Game in Python:
Beginners Source Code 2021 Python Game Projects With
June 18, 2020 July 9, 2020 Source Code
In "Python Projects" In "Python Projects" August 13, 2020
In "Python Projects"

angel jude suarez


https://itsourcecode.com/free-projects/python-projects/mario-game-in-python-with-source-code/ 11/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020
Hello programmers, I'm Angel Jude R. Suarez, a student and a programmer of different programming x
languages like Python, Java, JavaScript, PHP, C, C++, Vb.net, and MySQL. also a writer of
itsourcecode.com.

https://itsourcecode.com/free-projects/python-projects/mario-game-in-python-with-source-code/ 12/12

You might also like