Super Mario Game Code in Python

100% found this document useful (1 vote)
2K views12 pages
This document discusses and provides source code for a Mario game created in Python. It includes descriptions and code for the game's starting screen, level increments based on score, and ma…

Uploaded by

Stylianos Kontos
  • Introduction
  • Setting Up Your Environment
  • Code Explanation
  • Complete Source Code
  • Summary and Additional Resources

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.

[Link] 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

[Link] 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.

[Link] 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     [Link](BLACK)
3     start_img = [Link]('[Link]')
4     start_img_rect = start_img.get_rect()
5     start_img_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
6     [Link](start_img, start_img_rect)
7     while True:
8         for event in [Link]():
9             if [Link] == [Link]:
10                 [Link]()
11                 [Link]()
12             if [Link] == [Link]:
13                 if [Link] == pygame.K_ESCAPE:
14                     [Link]()
15                     [Link]()
16                 game_loop()
17         [Link]()

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

[Link] 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         [Link]('mario_theme.wav')
13         [Link](-1, 0.0)
14         while True:
15             [Link](BLACK)
16             check_level(SCORE)
17             [Link]()
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                 [Link]()
29  
30             for event in [Link]():
31                 if [Link] == [Link]:
32                     [Link]()
33                     [Link]()
34                 if [Link] == [Link]:
35                     if [Link] == pygame.K_UP:
36                         [Link] = True
37                         [Link] = False
38                     elif [Link] == pygame.K_DOWN:
39                         [Link] = True
40                         [Link] = False
41                 if [Link] == [Link]:
42                     if [Link] == pygame.K_UP:
43                         [Link] = False
44                         [Link] = True
45                     elif [Link] == pygame.K_DOWN:
46                         [Link] = True
47                         [Link] = False
48  
49             score_font = [Link]('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             [Link](score_font, score_font_rect)
53  
54             level_font = [Link]('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             [Link](level_font, level_font_rect)
58  
59             top_score_font = [Link]('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             [Link](top_score_font, top_score_font_rect)
63  
64             [Link](cactus_img, cactus_img_rect)
65             [Link](fire_img, fire_img_rect)

[Link] 5/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020
66             [Link]() 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             [Link]()
73             [Link](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     [Link]()
3     music = [Link]('mario_dies.wav')
4     [Link]()
5     topscore.top_score(SCORE)
6     game_over_img = [Link]('[Link]')
7     game_over_img_rect = game_over_img.get_rect()
8     game_over_img_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
9     [Link](game_over_img, game_over_img_rect)
10     while True:
11         for event in [Link]():
12             if [Link] == [Link]:
13                 [Link]()
14                 [Link]()
15             if [Link] == [Link]:
16                 if [Link] == pygame.K_ESCAPE:
17                     [Link]()
18                     [Link]()
19                 [Link]()
20                 game_loop()
21         [Link]()

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

This Will Be The Output

[Link] 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 [Link]()
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 = [Link]('cactus_bricks.png')
12 cactus_img_rect = cactus_img.get_rect()
13 cactus_img_rect.left = 0
14 fire_img = [Link]('fire_bricks.png')
15 fire_img_rect = fire_img.get_rect()
16 fire_img_rect.left = 0
17 CLOCK = [Link]()
18 font = [Link]('forte', 20)
19  
20 canvas = [Link].set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
21 [Link].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 = [Link]('[Link]')
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         [Link] = True
46         [Link] = False
47  
48     def update(self):
49         [Link](self.dragon_img, self.dragon_img_rect)
50         if self.dragon_img_rect.top <= cactus_img_rect.bottom:
51             [Link] = False
52             [Link] = True
53         elif self.dragon_img_rect.bottom >= fire_img_rect.top:
54             [Link] = True
55             [Link] = False

[Link] 7/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020
56
57
 
        if [Link]:
x
58             self.dragon_img_rect.top -= self.dragon_velocity
59         elif [Link]:
60             self.dragon_img_rect.top += self.dragon_velocity
61  
62  
63 class Flames:
64     flames_velocity = 20
65  
66     def __init__(self):
67         [Link] = [Link]('[Link]')
68         self.flames_img = [Link]([Link], (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         [Link](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 = [Link]('[Link]')
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         [Link] = True
90         [Link] = False
91  
92     def update(self):
93         [Link](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 [Link]:
103             self.mario_img_rect.top -= 10
104         if [Link]:
105             self.mario_img_rect.bottom += 10
106  
107  
108 def game_over():
109     [Link]()
110     music = [Link]('mario_dies.wav')
111     [Link]()
112     topscore.top_score(SCORE)
113     game_over_img = [Link]('[Link]')
114     game_over_img_rect = game_over_img.get_rect()
115     game_over_img_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
116     [Link](game_over_img, game_over_img_rect)
117     while True:
118         for event in [Link]():
119             if [Link] == [Link]:
120                 [Link]()
121                 [Link]()
122             if [Link] == [Link]:
123                 if [Link] == pygame.K_ESCAPE:
124                     [Link]()
125                     [Link]()
126                 [Link]()
127                 game_loop()
128         [Link]()
129  
130  
131 def start_game():
132     [Link](BLACK)
133     start_img = [Link]('[Link]')
134     start_img_rect = start_img.get_rect()
135     start_img_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
136     [Link](start_img, start_img_rect)

[Link] 8/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020
137
138
    while True:
        for event in [Link]():
x
139             if [Link] == [Link]:
140                 [Link]()
141                 [Link]()
142             if [Link] == [Link]:
143                 if [Link] == pygame.K_ESCAPE:
144                     [Link]()
145                     [Link]()
146                 game_loop()
147         [Link]()
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         [Link]('mario_theme.wav')
185         [Link](-1, 0.0)
186         while True:
187             [Link](BLACK)
188             check_level(SCORE)
189             [Link]()
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                 [Link]()
201  
202             for event in [Link]():
203                 if [Link] == [Link]:
204                     [Link]()
205                     [Link]()
206                 if [Link] == [Link]:
207                     if [Link] == pygame.K_UP:
208                         [Link] = True
209                         [Link] = False
210                     elif [Link] == pygame.K_DOWN:
211                         [Link] = True
212                         [Link] = False
213                 if [Link] == [Link]:
214                     if [Link] == pygame.K_UP:
215                         [Link] = False
216                         [Link] = True
217                     elif [Link] == pygame.K_DOWN:

[Link] 9/12
7/2/2021 Mario Game In Python With Source Code | FREE | 2020
218                         [Link] = True x
219                         [Link] = False
220  
221             score_font = [Link]('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             [Link](score_font, score_font_rect)
225  
226             level_font = [Link]('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             [Link](level_font, level_font_rect)
230  
231             top_score_font = [Link]('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             [Link](top_score_font, top_score_font_rect)
235  
236             [Link](cactus_img, cactus_img_rect)
237             [Link](fire_img, fire_img_rect)
238             [Link]()
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             [Link]()
245             [Link](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 [Link] in this tutorial is the simplest way for the beginners or the
student to enhance their logical skills in programming.

Related Articles

[Link] 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


[Link] 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++, [Link], and MySQL. also a writer of
[Link].

[Link] 12/12

7/2/2021
Mario Game In P (http://www.pygame.org/)ython With Source Code | FREE | 2020
https://itsourcecode.com/free-projects/
7/2/2021
Mario Game  (https://code-projects.org/simple-mario-game-in-python-with-source-code/)In Python With Source Code | FR
7/2/2021
Mario Game In Python With Source Code | FREE | 2020
https://itsourcecode.com/free-projects/python-projects/mario-gam
7/2/2021
Mario Game In Python With Source Code | FREE | 2020
https://itsourcecode.com/free-projects/python-projects/mario-gam
7/2/2021
Mario Game In Python With Source Code | FREE | 2020
https://itsourcecode.com/free-projects/python-projects/mario-gam
7/2/2021
Mario Game In Python With Source Code | FREE | 2020
https://itsourcecode.com/free-projects/python-projects/mario-gam
7/2/2021
Mario Game In Python With Source Code | FREE | 2020
https://itsourcecode.com/free-projects/python-projects/mario-gam
7/2/2021
Mario Game In Python With Source Code | FREE | 2020
https://itsourcecode.com/free-projects/python-projects/mario-gam
7/2/2021
Mario Game In Python With Source Code | FREE | 2020
https://itsourcecode.com/free-projects/python-projects/mario-gam
7/2/2021
Mario Game In Python With Source Code | FREE | 2020
https://itsourcecode.com/free-projects/python-projects/mario-gam

You might also like