/
pong.py
146 lines (115 loc) · 4.18 KB
/
pong.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
from random import randint
import argparse
# Program input parameters
parser = argparse.ArgumentParser(description='Game parameters')
parser.add_argument('-w', dest='window', action='store_true',
default=False, help='Start game in windowed mode')
args = parser.parse_args()
# Initialize Pygame
pygame.init()
pygame.font.init()
fontLG = pygame.font.SysFont('Arial', 30)
fontSM = pygame.font.SysFont('Arial', 16)
clock = pygame.time.Clock()
# Globals
WHITE = (255, 255, 255)
ISDOWN = pygame.key.get_pressed()
BOOL = {True: 1, False: 0}
SPEED = 3
# Screen
pygame.display.set_caption("Pong")
# Open screen based on input parametrs
if args.window:
screen = pygame.display.set_mode((800, 480), 0, 32)
else:
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN, 32)
width, height = pygame.display.get_surface().get_size()
screen.fill((0,0,0))
# Objects
class Ball:
def __init__(self, x, y, vx, vy):
self.x = x
self.y = y
self.vx = vx
self.vy = vy
class Player:
def __init__(self, x, y, score):
self.x = x
self.y = y
self.score = score
ball = Ball(width/2, height/2, -SPEED, SPEED)
player1 = Player(10, height/2, 0)
player2 = Player(width - 30, height/2, 0)
def update():
global running
global SPEED
pygame.event.pump()
ISDOWN = pygame.key.get_pressed()
player1.y = player1.y + (BOOL[ISDOWN[pygame.K_a]] - BOOL[ISDOWN[pygame.K_q]]) * 7
player2.y = player2.y + (BOOL[ISDOWN[pygame.K_DOWN]] - BOOL[ISDOWN[pygame.K_UP]]) * 7
if player1.y < 100: player1.y = 100
if player1.y > (height - 60): player1.y = height - 60
if player2.y < 100: player2.y = 100
if player2.y > (height - 60): player2.y = height - 60
ball.x = ball.x + ball.vx
ball.y = ball.y + ball.vy
if abs(ball.y) >= (height - 20): ball.vy = -ball.vy
if abs(ball.y) <= 60: ball.vy = -ball.vy
if ball.x < 40 and ball.x > 20 and abs(player1.y - ball.y) < 60:
SPEED = SPEED + 0.2
ball.x = 40
ball.vx = SPEED
ball.vy = ball.vy * 0.5 + randint(-10, 10) / 20 * SPEED
if ball.x < -10:
ball.x = width/3 * 2
ball.y = height/2
SPEED = 3
ball.vx = -SPEED
ball.vy = -SPEED
player2.score = player2.score + 1
if ball.x > (width - 40) and ball.x < (width - 20) and abs(player2.y - ball.y) < 60:
SPEED = SPEED + 0.5
ball.x = (width - 40)
ball.vx = -SPEED
ball.vy = ball.vy * 0.8 + randint(-10, 10) / 10 * SPEED
if ball.x > width + 10:
ball.x = width/3
ball.y = height/2
SPEED = 3
ball.vx = SPEED
ball.vy = SPEED
player1.score = player1.score + 1
for event in pygame.event.get():
if event.type == QUIT:
running = False
if ISDOWN[pygame.K_ESCAPE]:
running = False
def draw():
title = fontLG.render("PONG", False, WHITE)
screen.blit(title, (width/2 - title.get_size()[0]/2, 10))
pygame.draw.line(screen, WHITE, (width/2, 40), (width/2, height))
pygame.draw.rect(screen, WHITE, [player1.x, player1.y - 50, 20, 100])
pygame.draw.rect(screen, WHITE, [player2.x, player2.y - 50, 20, 100])
pygame.draw.circle(screen, WHITE, [int(ball.x), int(ball.y)], 10)
score1 = fontLG.render(str(player1.score), False, WHITE)
screen.blit(score1, (10, 10))
instructions1 = fontSM.render("P1 Keys: Q and A", False, WHITE)
screen.blit(instructions1, (score1.get_size()[0] + 20, 18))
score2 = fontLG.render(str(player2.score), False, WHITE)
screen.blit(score2, (width - score2.get_size()[0] - 10, 10))
instructions2 = fontSM.render("P2 Keys: UP and DOWN", False, WHITE)
screen.blit(instructions2, (width - score2.get_size()[0] - 20 - instructions2.get_size()[0], 18))
instructions3 = fontSM.render("Press ESC to Quit", False, WHITE)
pos = ((width/2 - title.get_size()[0]/2) - (score1.get_size()[0] - instructions2.get_size()[0]))/2 - instructions3.get_size()[0]/2
screen.blit(instructions3, (pos, 18))
running = True
while running:
screen.fill((0,0,0))
update()
draw()
clock.tick(60)
pygame.display.flip()
pygame.quit()