/
game.py
executable file
·125 lines (108 loc) · 4.01 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
117
118
119
120
121
122
123
124
125
import pygame
import random
# import classes necessary to create the game
from ball import Ball
from bar import Bar
from text import Text
# possible game states
STATUS_PLAYING = 1
STATUS_LOSE = 2
STATUS_WIN = 3
#
# The game contains one Ball and one Bar. It moves them
# each frame. If they collide the game is over, and stops
# allowing motion.
#
# The Ball and Bar are drawn every frame. If the game is
# over, then a Text message is also drawn.
#
class Game:
# create all of the objects necessary for the game play and display
def __init__(self, width, height):
self.mWidth = width
self.mHeight = height
self.mBall = Ball(width/3, height/2, 10, (255, 255, 0))
self.mBars = []
self.mbotBars = []
self.spawnBar()
#self.spawnbotBar()
self.mCount = 0
self.mPoints = 0
#self.mBar = Bar(width/2, height/3, 100, 10, (255, 255, 0))
#self.mBar.accelerate(20)
self.mGameOver = False
self.mGameStatus = STATUS_PLAYING
self.mWinnerMessage = Text("You Win!", self.mWidth/2, self.mHeight/2)
self.mLoserMessage = Text("Sorry, try again.", self.mWidth/2, self.mHeight/2)
return
# accelerate the ball vertically by a specified amount
def bounceBall(self, amount):
self.mBall.bounce(amount)
return
def spawnBar(self):
h = random.randrange(100, 450, 25)
color = (random.randrange(1, 256), random.randrange(1, 256), random.randrange(1, 256))
bar = Bar(550, 0, 50, h, color)
bar.accelerate(20)
self.mBars.append(bar)
color2 = (random.randrange(1, 256), random.randrange(1, 256), random.randrange(1, 256))
randy = h + 100
botbar = Bar(550, randy, 50, 500-randy, color)
botbar.accelerate(20)
self.mbotBars.append(botbar)
return
# move all of the objects, according to the physics of this world
def evolve(self):
if self.mGameOver:
# game over, nothing will move
return
self.mPointsmessage = Text("Points = "+str(self.mPoints), 400, 400)
# move the Ball and Bar
self.mBall.evolve(self.mHeight)
for botbar in self.mbotBars:
botbar.evolve(self.mWidth)
for bar in self.mBars:
bar.evolve(self.mWidth)
if self.mBars[self.mCount].getX() < 150:
self.spawnBar()
#self.spawnbotBar()
self.mCount += 1
self.mPoints +=1
# check for collisions
if self.mBall.collidesWithBarToLose(bar):
print self.mPoints
self.mGameOver = True
self.mStatus = STATUS_LOSE
elif self.mBall.collidesWithBarToLose(botbar):
print self.mPoints
self.mGameOver = True
self.mStatus = STATUS_LOSE
elif self.mBall.hitground():
print self.mPoints
self.mGameOver = True
self.mStatus = STATUS_LOSE
elif self.mBall.hitroof():
print self.mPoints
self.mGameOver = True
self.mStatus = STATUS_LOSE
return
# redraw the whole screen
def draw(self, surface):
# make the background, be drawing a solid colored rectangle
rect = pygame.Rect(0, 0, self.mWidth, self.mHeight)
pygame.draw.rect(surface, (169,169,169), rect, 0)
self.mPointsmessage.draw(surface)
# draw the Ball and Bar
self.mBall.draw(surface)
for bar in self.mBars:
bar.draw(surface)
for botbar in self.mbotBars:
botbar.draw(surface)
#botbar.draw(surface)
if self.mGameOver:
# draw end of game message
if self.mStatus == STATUS_LOSE:
self.mLoserMessage.draw(surface)
elif self.mStatus == STATUS_WIN:
self.mWinnerMessage.draw(surface)
return