A downloadable project

Did you ever wanted to make a own custom Module in Python? Here is how:


It's an easy process:


First of all,  i will begin this Tutorial and hope that you have a little bit of understanding how Python works.


Python installment

So for this tutorial you need Python. Once in the installer don't forget to check the "Add to PATH" box.

Then you download other modules that may help you in the process of coding your module(in the terminal with:  pip install modulename)


I will use PyGame for graphics.

Do you have an Text editor because you're not using Leo I? Well, let's begin!


Setup

First make a folder and call it however you like.

Then add 1 File that is called like your module and .py somewhat like: myModule.py.


Open the file in a text editor of your choise.


And now we can ACTUALLY begin!!!


First (yes i love that word) you are going to type: import pygame(or whatever module you choose)

-------------------------------------------------------------------------------

import pygame

-------------------------------------------------------------------------------

than you are going to make a function with the keyword: def and then the name of the Module line like:

-------------------------------------------------------------------------------

import pygame

def MyWindow():

-------------------------------------------------------------------------------

And now you are just going to add the things this function has to do for example, mine is going to open a new window where you can adjust the size and the Title:

-------------------------------------------------------------------------------

import pygame

def MyWindow(WIDTH, HEIGHT, NAME):

         screen = pygame.display.set_mode((WIDTH, HEIGHT))

         pygame.display.set_caption(NAME)


-------------------------------------------------------------------------------


Now if you want to make a function with a while loop in it, you have to seperate it so our code should look like this:


-------------------------------------------------------------------------------


import pygame

def MyWindow(WIDTH, HEIGHT, NAME):

         screen = pygame.display.set_mode((WIDTH, HEIGHT))

         pygame.display.set_caption(NAME)

def MyWindowLoop()

         while True:

                for event in pygame.event.get():

                            if event.type == pygame.QUIT:

                                        pygame.quit()


-------------------------------------------------------------------------------


Now when you create a new file in the same directory as the MyModule.py and import it like : from MyModule import *, you should have full access to your module. Here is an example of what we did now:


from myModule.py import *


MyWindow(800, 600, "MyWindow")

MyWindowLoop()

-------------------------------------------------------------------------------



If you have questions please comment them :-)


Tutorial by @FoxtasticCode on itch.io


saucecode


Have a great day!

~ FoxtasticCode


Leave a comment

Log in with itch.io to leave a comment.