<i>
</i>import pygame
from pygame.locals import *
import sys, os
import random
from math import *
pygame.init()
Screen = (320,240)
pygame.display.set_caption("Ping")
icon = pygame.Surface((1,1)); icon.set_alpha(0); pygame.display.set_icon(icon)
Surface = pygame.display.set_mode(Screen)
pygame.mouse.set_visible(False)
Ping = pygame.mixer.Sound('Ping.wav')
Click = pygame.mixer.Sound('Click.wav')
Ping.set_volume(0.1)
Click.set_volume(0.1)
Font = pygame.font.Font(None,22)
Font2 = pygame.font.Font(None,18)
CirclesInTheAir = 1
class Circle:
def __init__(self,x,y):
self.x = x
self.y = y
angle = random.randint(0,360)
self.speedx = 0.7*cos(radians(angle))
self.speedy = 0.7*sin(radians(angle))
self.placesbeen = []
self.radius = 2
self.add = 0
self.stopped = False
self.stoppedtime = 0
Circles = []
#for x in xrange(CirclesInTheAir):
# Circles.append(Circle(Screen[0]/2,Screen[1]/2))
class Paddle:
def __init__(self,x,y,size,player):
self.rect = [x,y,10,size]
self.player = player
Paddles = []
Paddles.append(Paddle(10,Screen[1]/2,50,1))
Paddles.append(Paddle(310,Screen[1]/2,50,2))
P1Score = 0
P2Score = 0
def GetInput():
key = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == QUIT or key[K_a]:
pygame.mouse.set_visible(True)
pygame.quit(); sys.exit()
if key[K_UP]:
for p in Paddles:
if p.player == 2: p.rect[1] += 3
if key[K_DOWN]:
for p in Paddles:
if p.player == 2: p.rect[1] -= 3
if key[K_w]:
for p in Paddles:
if p.player == 1: p.rect[1] += 3
if key[K_s]:
for p in Paddles:
if p.player == 1: p.rect[1] -= 3
def Update():
for c in Circles:
if c.add == 0:
c.placesbeen.append([c.x,c.y])
c.placesbeen.reverse()
c.placesbeen = c.placesbeen[:25]
c.placesbeen.reverse()
c.add = 15
else:
c.add -= 1
if c.stoppedtime == 37:
Circles.remove(c)
continue
if len(Circles) < CirclesInTheAir:
Circles.append(Circle(Screen[0]/2,Screen[1]/2))
def Move():
for c in Circles:
c.x += c.speedx
c.y += c.speedy
Update()
def CollisionDetect():
global P1Score, P2Score
for c in Circles:
if c.x <= 0-c.radius:
if not c.stopped:
c.stopped = True
c.speedx = 0
c.speedy = 0
P2Score += 1
else:
c.stoppedtime += 1
if c.y <0>= Screen[0]+c.radius:
if not c.stopped:
c.stopped = True
c.speedx = 0
c.speedy = 0
P1Score += 1
else:
c.stoppedtime += 1
if c.y >= Screen[1]:
c.y = Screen[1]
c.speedy *= -1
Click.play(0)
for p in Paddles:
if p.rect[1]-(p.rect[3]/2) <0> Screen[1]:
p.rect[1] = Screen[1] - (p.rect[3]/2)
for p in Paddles:
r = PygameRectFromRect(p.rect)
for c in Circles:
xdiff = c.x - p.rect[0]
ydiff = c.y - p.rect[1]
if abs(xdiff) <= (p.rect[2]/2) + c.radius and abs(ydiff) <p> 0:#right side
c.x = r[0] + r[2] + c.radius
if xdiff < 0:#left side
c.x = r[0] - c.radius
c.speedx *= 1.1
c.speedy *= 1.1
def PygameRectFromRect(r):
tl = ( r[0]-(r[2]/2), (r[1]+(r[3]/2)) )
dim = ( r[2], r[3] )
r2 = (tl[0],tl[1],dim[0],dim[1])
return r2
def IntegerisePoint(point):
returnpoint = [int(round(point[0])),int(round(point[1]))]
return returnpoint
info_text = Font.render("Ping",True,(255,255,255))
info_text_draw_pos = ((Screen[0]/2)-(info_text.get_width()/2),10)
def Draw():
Surface.fill((0,0,0))
for c in Circles:
light = 0
for p in c.placesbeen:
point = IntegerisePoint((p[0],Screen[1]-p[1]))
pygame.draw.circle(Surface,(light,0,0),point,c.radius)
light += 2
point = IntegerisePoint((c.x,Screen[1]-c.y))
pygame.draw.circle(Surface,(255,255,255),point,c.radius)
for p in Paddles:
r = PygameRectFromRect(p.rect)
pygame.draw.rect(Surface,(255,255,255),(r[0],Screen[1]-r[1],r[2],r[3]),0)
Surface.blit(info_text,info_text_draw_pos)
p1_score_text = Font2.render("Player 1, Score "+str(P1Score),True,(255,255,255))
p2_score_text = Font2.render("Player 2, Score "+str(P2Score),True,(255,255,255))
Surface.blit(p1_score_text,(0+20,20))
Surface.blit(p2_score_text,(Screen[0]-p2_score_text.get_width()-20,20))
pygame.display.flip()
def main():
while True:
GetInput()
Move()
CollisionDetect()
Draw()
if __name__ == '__main__': main()