forked from pmargani/pyglTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
191 lines (152 loc) · 5.05 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# master change
import pyglet
from pyglet.gl import *
from pyglet.window import key
from pyglet.window import mouse
import random
# Game states
PREPLAY = 0
INPLAY = 1
POSTPLAY = 2
gameState = PREPLAY
score = 0
# set up the window
wH = wW = 800
window = pyglet.window.Window(width = wW, height = wH)
framesPerSecond = 50.
# set up a point
# place it in a random location in the left hand side of the screen
pX = random.random() * wW/2
pY = random.random() * wH
# give the point a constant speed
# but in a random direction
speed = 50
vX = random.choice([1,-1]) * speed # pixels / second
vY = random.choice([1,-1]) * speed # pixels / second
# setup a 'pong player': a tall rectangle
# first the position of the center of the pong
pongX = (wW * 3) // 4
pongY = wH // 2
# now the coordinates of the lines relative to the center
pongLines = [(-10,-50),(-10,50),(10,50),(10,-50)]
# combine the two to calculate where the lines should be on the screen
pongPos = [(pongX + x, pongY + y) for x, y in pongLines]
# the pong has vertical velocity when the right keys are pressed
pongSpeed = 50
pongV = 0
# Below are are helper functions for drawing points, lines, shapes
# and text using pyglet
def drawPoint(x, y):
"draws a point at x, y"
pyglet.graphics.draw(1, pyglet.gl.GL_POINTS, ('v2f', (x, y)))
def drawPolygonLines(xys):
"draws a shape outline using the given x, y points of the lines"
l = len(xys)
pts = []
for x, y in xys:
pts.append(x)
pts.append(y)
pyglet.graphics.draw(l, pyglet.gl.GL_LINE_LOOP, ('v2f', tuple(pts)))
def drawPolygon(xys):
"draws a shape using the given x, y points of the sides"
l = len(xys)
pts = []
for x, y in xys:
pts.append(x)
pts.append(y)
pyglet.graphics.draw(l, pyglet.gl.GL_POLYGON, ('v2f', tuple(pts)))
def drawLabel(value, x, y):
"draws the given text at the given x, y position"
label = pyglet.text.Label(value,
font_name='Times New Roman',
font_size=10, x=x, y=y,
anchor_x='left', anchor_y='center')
label.draw()
def pointInRectangle(x, y, rec):
"is the given point inside the given lines of a rectangle?"
# since we know these are the points for a rectangle, we know
# we can simplyfy things
x0 = rec[0][0]
x1 = rec[3][0]
y0 = rec[0][1]
y1 = rec[1][1]
# is the point within these coords?
return x > x0 and x < x1 and y > y0 and y < y1
def update(dt):
"This is called at regular intervals by the pyglet game"
if gameState == INPLAY:
updateInPlay(dt)
def updateInPlay(dt):
"How the game is updated while in play."
# update method needs to know about these variables
global pX, vX
global pY, vY
global pongX, pongY, pongV, pongLines, pongPos
global gameState, score
# update the points position using it's velocity
pX = pX + (vX * dt)
pY = pY + (vY * dt)
# update position of the pong
pongY = pongY + (pongV * dt)
# so now the pong's lines on the screen need to be updated
pongPos = [(pongX + x, pongY + y) for x, y in pongLines]
# bounce the ball off walls!
if pX < 0:
vX = -1 * vX
if pY < 0 or pY > wH:
vY = -1 * vY
# bounce the ball off the pong
# this is worth a point!
if pointInRectangle(pX, pY, pongPos):
print "hit!!!"
# if it's inside the pong
vX = -1 * vX
score += 1
# if the ball goes off the right side of screen: GAME OVER!
if pX > wW:
print 'GAME OVER!'
gameState = POSTPLAY
@window.event
def on_key_press(symbol, modifiers):
global pongV, gameState
print "on_key_press: ", symbol
if gameState == INPLAY:
if symbol == key.UP:
pongV = pongSpeed
if symbol == key.DOWN:
pongV = -pongSpeed
elif gameState == PREPLAY and symbol == key.ENTER:
# start playing!
gameState = INPLAY
@window.event
def on_key_release(symbol, modifiers):
global pongV
print "on_key_release: ", symbol
if gameState == INPLAY:
if symbol == key.UP or symbol == key.DOWN:
pongV = 0
@window.event
def on_draw():
"Called by pyglet when it's time to draw the screen"
# first we have to clear everything
window.clear()
# then redraw everything!
if gameState == INPLAY:
# draw the point
drawPoint(pX, pY)
# draw the pong
drawPolygonLines(pongPos)
# draw the score
drawLabel("SCORE: %d" % score, 10, 10)
elif gameState == PREPLAY:
# draw the start message
drawLabel("Press ENTER to start game.", wW//2, wH//2)
elif gameState == POSTPLAY:
# draw the game over message
drawLabel("Game Over Dude.", wW//2, wH//2)
drawLabel("Final Score: %d" % score, wW//2, (wH//2) - 25)
# How fast do we want to update our game?
pyglet.clock.schedule_interval(update, 1./framesPerSecond)
# Run the game! This is where our program will be sitting. The @window.event methods are callbacks
pyglet.app.run()
print "Exiting Game."