Professional Documents
Culture Documents
Programming A Pygame Platform
Programming A Pygame Platform
by NYSM
import pygame
BLACK = (0,0,0)
RED = (255,0,0)
BLUE = (0,0,255)
WHITE = (255,255,255)
WIDTH = 500
HEIGHT = 500
BD = 50
FPS = 60
class Player():
def draw(self):
pygame.draw.rect(self.s, BLACK, (self.x, self.y, BD, BD))
class Block():
def draw(self):
pygame.draw.rect(self.s, RED, (self.x, self.y, BD, BD))
pygame.init()
running = True
player = Player(screen, 0, 0)
block = Block(screen, 100 , 100)
inverso = True
sposta = 5
while running:
xvar = 0
yvar = 0
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
xvar = sposta
if keys[pygame.K_LEFT]:
xvar = -sposta
if keys[pygame.K_UP]:
yvar = -sposta
if keys[pygame.K_DOWN]:
yvar = sposta
if keys[pygame.K_m]:
inverso = not inverso
if keys[pygame.K_c]:
sposta/=1.5
if keys[pygame.K_v]:
sposta*=1.5
if inverso == False:
xvar=-xvar
yvar=-yvar
player.move(xvar, yvar)
if player.collide(block):
block.move(xvar,yvar)
if player.out_of_bounds(sposta,sposta) or player.out_of_bounds(-sposta,-
sposta):
screen.fill(BLUE)
else:
screen.fill(WHITE)
player.draw()
block.draw()
pygame.display.update()
clock.tick(FPS)
pygame.quit()