Skip to content

Instantly share code, notes, and snippets.

@masci
Created May 16, 2012 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masci/2714271 to your computer and use it in GitHub Desktop.
Save masci/2714271 to your computer and use it in GitHub Desktop.
Pygame pong
# Per funzionare il programma ha bisogno della libreria pygame
#
# Le istruzioni per installare pygame sono a questo indirizzo:
# http://pygame.org/download.shtml
#
import pygame, random
pygame.init()
random.seed()
screen = pygame.display.set_mode((400, 400))
run = True
while run:
# loop del gioco
screen.fill((0,0,0))
#---Place 3
pygame.display.flip()
for event in pygame.event.get():
if event.type == QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
run = False
pygame.quit()
import pygame, random
pygame.init()
random.seed()
screen = pygame.display.set_mode((400, 400))
font = pygame.font.SysFont("courier", 72)
altezza_barra_s = 200
altezza_barra_d = 200
palla_x = 200
palla_y = 200
punteggio_s = 0
punteggio_d = 0
def disegna_barra_s(altezza):
barra = pygame.Surface((10,50))
barra.fill(pygame.Color('white'))
screen.blit(barra, (10,altezza-25))
def disegna_barra_d(altezza):
barra = pygame.Surface((10,50))
barra.fill(pygame.Color('white'))
screen.blit(barra, (380, altezza-25))
def disegna_palla(x,y):
palla = pygame.Surface((10,10))
palla.fill(pygame.Color('white'))
screen.blit(palla, (x-5,y-5))
def disegna_centrocampo():
pygame.draw.line(screen, pygame.Color('white'), (200,0), (200,400))
def disegna_punteggio_s(punti):
text = font.render(str(punti), True, pygame.Color('white'))
screen.blit(text, (100 - text.get_width() // 2, 10))
def disegna_punteggio_d(punti):
text = font.render(str(punti), True, pygame.Color('white'))
screen.blit(text, (300 - text.get_width() // 2, 10))
run = True
while run:
# loop del gioco
screen.fill((0,0,0))
disegna_barra_sx(200)
disegna_barra_dx(200)
disegna_palla(200,200)
disegna_centrocampo()
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
run = False
pygame.quit()
import pygame, random
pygame.init()
pygame.key.set_repeat(50,50)
random.seed()
screen = pygame.display.set_mode((400, 400))
font = pygame.font.SysFont("courier", 72)
altezza_barra_s = 200
altezza_barra_d = 200
palla_x = 200
palla_y = 200
punteggio_s = 0
punteggio_d = 0
def disegna_barra_s(altezza):
barra = pygame.Surface((10,50))
barra.fill(pygame.Color('white'))
screen.blit(barra, (10,altezza-25))
def disegna_barra_d(altezza):
barra = pygame.Surface((10,50))
barra.fill(pygame.Color('white'))
screen.blit(barra, (380, altezza-25))
def disegna_palla(x,y):
palla = pygame.Surface((10,10))
palla.fill(pygame.Color('white'))
screen.blit(palla, (x-5,y-5))
def disegna_centrocampo():
pygame.draw.line(screen, pygame.Color('white'), (200,0), (200,400))
def disegna_punteggio_s(punti):
text = font.render(str(punti), True, pygame.Color('white'))
screen.blit(text, (100 - text.get_width() // 2, 10))
def disegna_punteggio_d(punti):
text = font.render(str(punti), True, pygame.Color('white'))
screen.blit(text, (300 - text.get_width() // 2, 10))
def barra_s_su():
global altezza_barra_s
if altezza_barra_s > 0:
altezza_barra_s -= 5
if altezza_barra_s < 0:
altezza_barra_s = 0
def barra_s_giu():
global altezza_barra_s
if altezza_barra_s < 400:
altezza_barra_s += 5
if altezza_barra_s > 400:
altezza_barra_s = 400
def barra_d_su():
global altezza_barra_d
if altezza_barra_d > 0:
altezza_barra_d -= 5
if altezza_barra_d < 0:
altezza_barra_d = 0
def barra_d_giu():
global altezza_barra_d
if altezza_barra_d < 400:
altezza_barra_d += 5
if altezza_barra_d > 400:
altezza_barra_d = 400
run = True
while run:
# loop del gioco
screen.fill((0,0,0))
disegna_barra_s(altezza_barra_s)
disegna_barra_d(altezza_barra_d)
disegna_palla(palla_x,palla_y)
disegna_centrocampo()
disegna_punteggio_s(punteggio_s)
disegna_punteggio_d(punteggio_d)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
run = False
if keys[pygame.K_w]:
barra_s_su()
if keys[pygame.K_s]:
barra_s_giu()
if keys[pygame.K_UP]:
barra_d_su()
if keys[pygame.K_DOWN]:
barra_d_giu()
pygame.quit()
import pygame, random
pygame.init()
pygame.key.set_repeat(10,10)
random.seed()
screen = pygame.display.set_mode((400, 400))
font = pygame.font.SysFont("courier", 72)
clock = pygame.time.Clock()
altezza_barra_s = 200
altezza_barra_d = 200
palla_x = 200
palla_y = 200
velocita_x = random.randint(1,5)
velocita_y = random.randint(1,5)
punteggio_s = 0
punteggio_d = 0
def disegna_barra_s(altezza):
barra = pygame.Surface((10,50))
barra.fill(pygame.Color('white'))
screen.blit(barra, (10,altezza-25))
def disegna_barra_d(altezza):
barra = pygame.Surface((10,50))
barra.fill(pygame.Color('white'))
screen.blit(barra, (380, altezza-25))
def disegna_palla(x,y):
palla = pygame.Surface((10,10))
palla.fill(pygame.Color('white'))
screen.blit(palla, (x-5,y-5))
def disegna_centrocampo():
pygame.draw.line(screen, pygame.Color('white'), (200,0), (200,400))
def disegna_punteggio_s(punti):
text = font.render(str(punti), True, pygame.Color('white'))
screen.blit(text, (100 - text.get_width() // 2, 10))
def disegna_punteggio_d(punti):
text = font.render(str(punti), True, pygame.Color('white'))
screen.blit(text, (300 - text.get_width() // 2, 10))
def barra_s_su():
global altezza_barra_s
if altezza_barra_s-25 > 0:
altezza_barra_s -= 5
if altezza_barra_s-25 < 0:
altezza_barra_s = 0
def barra_s_giu():
global altezza_barra_s
if altezza_barra_s+25 < 400:
altezza_barra_s += 5
if altezza_barra_s+25 > 400:
altezza_barra_s = 400
def barra_d_su():
global altezza_barra_d
if altezza_barra_d-25 > 0:
altezza_barra_d -= 5
if altezza_barra_d-25 < 0:
altezza_barra_d = 0
def barra_d_giu():
global altezza_barra_d
if altezza_barra_d+25 < 400:
altezza_barra_d += 5
if altezza_barra_d+25 > 400:
altezza_barra_d = 400
def muovi_palla():
global palla_x, palla_y, velocita_x, velocita_y
global punteggio_s, punteggio_d
global altezza_barra_d, altezza_barra_s
if palla_x <=0:
punteggio_d += 1
palla_x = palla_y = 200
velocita_x = -random.randint(3,5)
velocita_y = -random.randint(3,5)
elif palla_x >= 400:
punteggio_s += 1
palla_x = palla_y = 200
velocita_x = random.randint(3,5)
velocita_y = random.randint(3,5)
elif palla_y <= 0:
palla_y = 0
velocita_y = -velocita_y
elif palla_y >= 400:
palla_y = 400
velocita_y = -velocita_y
elif palla_x <= 20 and altezza_barra_s-25 < palla_y < altezza_barra_s+25:
velocita_x = -velocita_x
velocita_y = -velocita_y
elif palla_x >= 380 and altezza_barra_d-25 < palla_y < altezza_barra_d+25:
velocita_x = -velocita_x
velocita_y = -velocita_y
palla_x += velocita_x
palla_y += velocita_y
run = True
while run:
# loop del gioco
clock.tick(40)
screen.fill((0,0,0))
disegna_barra_s(altezza_barra_s)
disegna_barra_d(altezza_barra_d)
disegna_palla(palla_x,palla_y)
disegna_centrocampo()
disegna_punteggio_s(punteggio_s)
disegna_punteggio_d(punteggio_d)
muovi_palla()
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
run = False
if keys[pygame.K_w]:
barra_s_su()
if keys[pygame.K_s]:
barra_s_giu()
if keys[pygame.K_UP]:
barra_d_su()
if keys[pygame.K_DOWN]:
barra_d_giu()
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment