Skip to main content

PYTHON - PYGAME NOTES

PYTHON PYGAME NOTES

                    ----->  FOR YOU


Click for newer version of pygame notes (better)


BASIC  PYGAME  STARTING :-


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


BASIC REQUIRED CODE :-

There must be following code to get a screen
running = True
while running :
     for event in pygame.event.get():
         if event.type == QUIT:
            running = False
     pygame.display.update()

DRAWING WITH PYGAME

pygame.draw.rect(surface,(r,g,b),(x-coordinate,y-coordinate,width,height))

# Doing the same thing with pygame.Rect
rect = pygame.rect(x-coordinate,y-coordinate,width,height)
pygame.draw.rect(surface,(r,g,b),rect)

# RESIZING A RECTANGLE
rect.inflate(width , height) 
# if you give -ve width or height the rectangle 
# rectangle will become smaller and vice-versa

# ALSO NOTE THAT IF THE RECTANGLE IS ALREADY DRAWN # TO SEE THE CHANGE YOU NEED TO DRAW IT AGAIN

pygame.draw.circle(surface,(r,g,b),(x,y),radius,thickness)

The following mouse functions can are there in pygame
mouse_x , mouse_y = pygame.mouse.get_pos() 
# GIVES MOUSE POSITION

DEALING WITH IMAGES :-

To add an image to your program :-
# LOADING THE IMAGE  -- 1st step without which 
# nothing else will work 
image = pygame.image.load(path_of_image)
# AN ADDITIONAL PARAMETER
image = pygame.image.load(path_of_image).convert_alpha()
# CHANGING IMAGE SIZE
image = pygame.transform.scale(image,(width,height))
# DRAWING THE IMAGE ON SCREEN
surface_or_screen.blit(image,(x,y))

Comments

Popular posts from this blog

PYTHON - TKINTER NOTES

   PYTHON ---->  TKINTER NOTES Click for new version of tkinter notes IMPORTING TKINTER SHOW MORE import tkinter # ANOTHER WAY (USED IN THIS PAGE) from tkinter import * ADDITIONAL IMPORT STATEMENTS from tkinter.ttk import * NECESSARY CODE SHOW MORE root = Tk () root. mainloop () WIDGETS IN TKINTER SHOW MORE Label Text Frame Canvas myLabel = Label ( master , **options ) # TO KNOW ABOUT **options RUN # help(Label) # in the terminal myText = Text ( master , **options ) # TO KNOW ABOUT **options RUN # help(Text) # in the terminal myFrame = Frame ( master , **options ) # TO KNOW ABOUT **options RUN # help(Frame) # in the terminal myCanvas = Canvas ( master , **options ) # TO KNOW ABOUT **options RUN # help(Canvas) # in the terminal MENU IN TKINTER SHOW MORE 1.) CREATING A...