Criando um joguinho
Esse tutorial mostra como programar um joguinho simples com a linguagem python.Para seguir esse tutorial é preciso que o python esteja instalado.
Além disso o módulo pygame também é utilizado.
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((840,210))
pygame.display.set_caption('Endless Runner')
clock = pygame.time.Clock()
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill((200,200,250))
pygame.draw.rect(gameDisplay, (110,110,110), [0,170,840,40])
pygame.display.update()
clock.tick(120)
pygame.quit()
quit()
Esse código é a base do tutorial. Salve e execute o arquivo para visualizar o cenário do jogo.
gameDisplay.blit(pygame.image.load('hero.png'), (74,104))
gameDisplay.blit(pygame.image.load('obstaculo.png'), (774,134))
gameDisplay.blit(pygame.image.load('obstaculo.png'), (744,134))
gameDisplay.blit(pygame.image.load('obstaculo.png'), (714,134))
obs1=974
obs2=744
obs3=714
E dentro do loop:
if obs1>-30: obs1-=2
else: obs1=850
if obs2>-30: obs2-=2
else: obs2=850
if obs3>-30: obs3-=3
else: obs3=850
gameDisplay.blit(pygame.image.load('obstaculo.png'), (obs1,134))
gameDisplay.blit(pygame.image.load('obstaculo.png'), (obs2,134))
gameDisplay.blit(pygame.image.load('obstaculo.png'), (obs3,134))
altura=0
Implementa as condições de subida e descida:
if altura == 0 and event.type == pygame.KEYDOWN: altura=4
if altura%3==1: altura+=3
if altura%3==0: altura-=3
if altura>100: altura=99
if altura<1: altura=0
gameDisplay.blit(pygame.image.load('hero.png'), (74,104-altura))
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((840,210))
pygame.display.set_caption('Endless Runner')
clock = pygame.time.Clock()
crashed = False
obs1=974
obs2=744
obs3=714
altura=0
gameover=False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill((200,200,250))
pygame.draw.rect(gameDisplay, (110,110,110), [0,170,840,40])
if altura == 0 and event.type == pygame.KEYDOWN: altura=4
if altura%3==1: altura+=3
if altura%3==0: altura-=3
if altura>100: altura=99
if altura<1: altura=0
gameDisplay.blit(pygame.image.load('hero.png'), (74,104-altura))
if altura<20 and obs1>40 and obs1<94: gameover=True
if altura<20 and obs2>40 and obs2<94: gameover=True
if altura<20 and obs3>40 and obs3<94: gameover=True
if gameover:
obs1=974
obs2=744
obs3=714
gameover=False
if obs1>-30: obs1-=2
else: obs1=850
if obs2>-30: obs2-=2
else: obs2=850
if obs3>-30: obs3-=3
else: obs3=850
gameDisplay.blit(pygame.image.load('obstaculo.png'), (obs1,134))
gameDisplay.blit(pygame.image.load('obstaculo.png'), (obs2,134))
gameDisplay.blit(pygame.image.load('obstaculo.png'), (obs3,134))
pygame.display.update()
clock.tick(120)
pygame.quit()
quit()