PyGame

Blank Pygame Template

import pygame

pygame.init()

win = pygame.display.set_mode((750, 500))

pygame.display.set_caption('Title')

run = True

while run:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            
pygame.quit()

How to draw objects

Moving Objects

Code (in progress)

import pygame as pg
import random
import time

pg.init()

###Window Size
display_width = 800
display_height = 600
gameDisplay = pg.display.set_mode((display_width,display_height))

###Window Title
pg.display.set_caption('My python game')

###Background Picture
bg = pg.image.load('bg.png')
bg = pg.transform.scale(bg, (display_width,display_height))

clock = pg.time.Clock()
white = (255,255,255)

###Pyguy Class
class player(object):
    
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.pyImg = pg.image.load('python.png')
        self.pyImg = pg.transform.scale(self.pyImg, (width,height))
        self.score = 0
        
    def draw(self, x, y):
    
        gameDisplay.blit(self.pyImg, (x,y))
        
    def get_rect(self):
        return pg.Rect(self.x, self.y, self.width, self.height)
        
###Codeguy Class
class code(object):
    
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.prtImg = pg.image.load('print.png')
        self.prtImg = pg.transform.scale(self.prtImg, (width,height))
        
    def draw(self, x, y):
        
        gameDisplay.blit(self.prtImg, (x,y))
        
    def get_rect(self):
        return pg.Rect(self.x, self.y, self.width, self.height)

#Redraws Screen
def redraw():
    gameDisplay.blit(bg, (0,0))
    codeguy.draw(codeguy.x, codeguy.y)
    pyguy.draw(pyguy.x, pyguy.y)
    font = pg.font.SysFont('Comic Sans MS', 30)
    text = font.render('Pygame', False, white)
    p1_score = font.render(str(pyguy.score), False, white)
    textRect = text.get_rect()
    p1Rect = p1_score.get_rect()
    textRect.center = (800 // 2, 75)
    p1Rect.center = (800//2, 150)
    gameDisplay.blit(text, textRect)
    gameDisplay.blit(p1_score, p1Rect)
    pg.display.update()
    

run = True


x_change = 0
y_change = 0
speed = 1


pyguy = player(100,100,50,30)
codeguy = code(display_width / 2, display_height / 2, 50, 30)

###Main Loop
while run:
    
    #Quit Event
    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False

        ###Key Press Movement
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_LEFT:
                x_change = -5 * speed
            elif event.key == pg.K_RIGHT:
                x_change = 5 * speed
            elif event.key == pg.K_UP:
                y_change = -5 * speed
            elif event.key == pg.K_DOWN:
                y_change = 5 * speed
            elif event.key == pg.K_SPACE:
                speed += 1
                
        ###Key Lift to stop movement       
        if event.type == pg.KEYUP:
            if event.key == pg.K_LEFT or event.key == pg.K_RIGHT:
                x_change = 0
            elif event.key == pg.K_UP or event.key == pg.K_DOWN:
                y_change = 0
                
    if pyguy.get_rect().colliderect(codeguy.get_rect()):
        codeguy.x = random.randint(25,775)
        codeguy.y = random.randint(25,475)
        pyguy.score += 1
        
    #Changes players position            
    pyguy.x += x_change
    pyguy.y += y_change

    #Redraw screen function     
    redraw()
    
    clock.tick(60)

#Close game once loop breaks
pg.quit()
quit()

Image Files