-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
116 lines (98 loc) · 3.53 KB
/
game.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
import sys, pygame, random
import time
pygame.init()
size = width, height = 500, 500
screen = pygame.display.set_mode(size)
class LeftPaddle(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('./paddle.png')
self.image = pygame.transform.scale(self.image, (20, 100))
self.rect = self.image.get_rect()
self.rect.center = (15, 250)
self.totalLives = 5
class RightPaddle(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('./paddle.png')
self.image = pygame.transform.scale(self.image, (20, 100))
self.rect = self.image.get_rect()
self.rect.center = (485, 250)
self.totalLives = 5
class Ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('./circle.png')
self.image = pygame.transform.scale(self.image, (25, 25))
self.rect = self.image.get_rect()
self.rect.center = (255, 250)
leftpad = LeftPaddle()
rightpad = RightPaddle()
ball = Ball()
paddles = pygame.sprite.Group()
paddles.add(leftpad)
paddles.add(rightpad)
balls = pygame.sprite.Group()
balls.add(ball)
toQuit = 0
clock = pygame.time.Clock()
ballspeed = [2, 3]
while 1:
clock.tick(60)
screen.fill((0,0,0))
paddles.draw(screen)
balls.draw(screen)
myFont = pygame.font.SysFont("Times New Roman", 18)
numsLivesDraw = myFont.render(str(leftpad.totalLives), True, (250, 250, 250))
numsLivesDrawRight = myFont.render(str(rightpad.totalLives), True, (250, 250, 250))
screen.blit(numsLivesDraw, (30,30))
screen.blit(numsLivesDrawRight, (400,30))
if leftpad.totalLives <= 0 or rightpad.totalLives <= 0:
letter = pygame.font.SysFont("Times New Roman", 30)
message = 'Game Over'
gameOver = letter.render(message, True, (250, 250, 250))
screen.blit(gameOver, (150,100))
toQuit = 1
pygame.display.flip()
key = pygame.key.get_pressed()
if key[pygame.K_w]:
if leftpad.rect.top > 5:
leftpad.rect = leftpad.rect.move([0, -5])
if key[pygame.K_s]:
if leftpad.rect.bottom < 495:
leftpad.rect = leftpad.rect.move([0, 5])
if key[pygame.K_UP]:
if rightpad.rect.top > 5:
rightpad.rect = rightpad.rect.move([0, -5])
if key[pygame.K_DOWN]:
if rightpad.rect.bottom < 495:
rightpad.rect = rightpad.rect.move([0, 5])
if ball.rect.top <= 5:
ballspeed[1] = ballspeed[1] * -1
if ball.rect.bottom >= 495:
ballspeed[1] = ballspeed[1] * -1
if ball.rect.left <= 0:
leftpad.totalLives = leftpad.totalLives -1
ball.rect.center = (250, 250)
ballspeed = [2, 4]
time.sleep(1)
if ball.rect.right >= 500:
rightpad.totalLives = rightpad.totalLives -1
ball.rect.center = (250, 250)
ballspeed = [-2, 4]
time.sleep(1)
leftcollide = pygame.sprite.spritecollideany(leftpad, balls)
rightcollide = pygame.sprite.spritecollideany(rightpad, balls)
if leftcollide != None:
ballspeed[0] = ballspeed[0] * -1
ballspeed[0] = ballspeed[0] + 0.5
if rightcollide != None:
ballspeed[0] = ballspeed[0] * -1
ballspeed[0] = ballspeed[0] - 0.5
ball.rect = ball.rect.move(ballspeed)
if toQuit == 1:
time.sleep(5)
sys.exit()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()