-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComSciPartnerGame.py
116 lines (106 loc) · 3.31 KB
/
ComSciPartnerGame.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
#########################################
# File Name: brickBreakerStarter.py
# Description: Starter code for Brick Breaker game
# Author: ICS2O
# Date: 08/11/2017
#########################################
import pygame
pygame.init()
WIDTH = 800
HEIGHT= 600
gameWindow=pygame.display.set_mode((WIDTH,HEIGHT))
TOP = 0
BOTTOM = HEIGHT
LEFT = 0
RIGHT = WIDTH
GREEN = ( 0,255, 0)
BLUE = ( 0, 0,128)
WHITE = (255,255,255)
BLACK = ( 0, 0, 0)
outline = 0
#---------------------------------------#
# functions #
#---------------------------------------#
def redrawGameWindow():
gameWindow.fill(BLACK)
pygame.draw.circle(gameWindow, WHITE, (ballX, ballY), ballR, outline)
pygame.draw.rect(gameWindow, GREEN, (paddleX, paddleY, paddleW, paddleH), outline)
pygame.draw.rect(gameWindow, GREEN, (paddleX2, paddleY2, paddleW2, paddleH2), outline)
pygame.display.update()
#---------------------------------------#
# the main program begins here #
#---------------------------------------#
print "Hit ESC to end the program."
#---------------------------------------#
# ball properties #
#---------------------------------------#
ballR = 15
ballX = WIDTH/2
ballY = 2*ballR
speedX = 1
speedY = 1
#---------------------------------------#
# paddle properties #
#---------------------------------------#
#player one uses ws, left side
paddleW = 20
paddleH = 120
paddleX = 0
paddleY = (HEIGHT-120)/2
paddleShift = 2
#player2 uses up down, right
paddleW2 = 20
paddleH2 = 120
paddleX2 = 780
paddleY2 = (HEIGHT-120)/2
paddleShift2 = 2
#----------------------------------------#
inPlay = True
while inPlay:
redrawGameWindow()
pygame.time.delay(2)
# check for key eventsw
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
inPlay = False
if keys[pygame.K_UP]:
if paddleY2 - paddleShift2 <0:
paddleY2 = 0
else:
paddleY2 = paddleY2 - paddleShift2
if keys[pygame.K_DOWN]:
if paddleY2+paddleH2 + paddleShift2 >HEIGHT:
paddleY2 = HEIGHT-paddleH
else:
paddleY2 = paddleY2 + paddleShift2
if keys[pygame.K_w]:
if paddleY - paddleShift <0:
paddleY = 0
else:
paddleY = paddleY - paddleShift
if keys[pygame.K_s]:
if paddleY+paddleH + paddleShift>HEIGHT:
paddleY = HEIGHT-paddleH
else:
paddleY = paddleY + paddleShift
# bounce the ball from the paddle
if ballX-ballR <= paddleX+paddleW and ballY <= paddleY+paddleH and ballY >= paddleY:
speedX = -speedX
# bounce the ball from the second Paddle
if ballX+ballR >= paddleX2 and ballY <= paddleY2+paddleH2 and ballY >= paddleY2:
speedX = -speedX
# bounce the ball from left, right, and top borders
if ballY>=BOTTOM or ballY<=0:
speedY = -speedY
# move the ball
ballX = ballX + speedX
ballY = ballY + speedY
if ballX-ballR <=LEFT:
print "Game Over!"
inPlay = False
if ballX+ballR >=RIGHT:
print "Game Over!"
inPlay = False
#---------------------------------------#
pygame.quit()