Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ovpc.py
blob: 58b11de954afaec392d0a39ea662e540b2a4eb87 (plain)
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
#!/usr/bin/env python
#
#    One Velociraptor per Child
#
#    Copyright 2009 Joel Stanley <joel@jms.id.au>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import pyglet

from pyglet.window import key
from random import randrange, random
import math
import time
import glob
from pyglet.gl import *

window = pyglet.window.Window(800, 400, caption='OVPC')
keys = pyglet.window.key.KeyStateHandler()
window.push_handlers(keys)

batch = pyglet.graphics.Batch()

pyglet.resource.path = ['res']
pyglet.resource.reindex()

window.set_icon(pyglet.resource.image("ovpc.png"))

child_death_files = glob.glob1("res", "child-death[012]*.png")
child_death_files.sort()
child_death = pyglet.image.Animation.from_image_sequence(
	[pyglet.resource.image(x) for x in child_death_files],
	0.1, False)

raptor_anim_files = glob.glob1("res", "raptor[123].png")
raptor_anim_files.sort()
raptor_anim = pyglet.image.Animation.from_image_sequence(
	[pyglet.resource.image(x) for x in raptor_anim_files],
	0.1, True)

raptor_static = pyglet.resource.image('raptor1.png')

pack = []
child = pyglet.sprite.Sprite(pyglet.resource.image('child.png'), batch=batch)

raptor_eating = -1

@child.event
def on_animation_end():
	global pack, raptor_eating
	if raptor_eating != -1:
		pack[raptor_eating].image = raptor_anim
		raptor_eating = -1

score = 0

t = pyglet.text.Label("Score: %.1f" % score, color=(255,0,0,255), batch=batch, font_size=16)
t.x = 10
t.y = window.height-40

def update(dt):
	global child, raptor_eating, score, t

	t.begin_update()
	if type(score) in (int, float, long):
		if len(pack)-2 < score:
			pack.append(pyglet.sprite.Sprite(raptor_anim, batch=batch))
			reset_enemy(pack[-1])

		score += dt
		t.text = "Score: %.1f" % score
	else:
		t.text = score
	t.end_update()


	if keys[key.ESCAPE]:
		import sys
		sys.exit(0)

	if keys[key.ENTER] and child.image is child_death:
		score = 0
		child.image = pyglet.resource.image('child.png')
		child.x = child.y = 0
		if raptor_eating != -1:
			pack[raptor_eating].image = raptor_anim
			raptor_eating = -1

	if keys[key.UP] and child.image is not child_death:
		child.y += dt * 100
	if keys[key.DOWN] and child.image is not child_death:
		child.y -= dt * 100
	if keys[key.LEFT] and child.image is not child_death:
		child.x -= dt * 100
	if keys[key.RIGHT] and child.image is not child_death:
		child.x += dt * 100

	if child.x > window.width:
		child.x = window.width
	if child.x < 0:
		child.x = 0

	if child.y > window.height - child.height:
		child.y = window.height - child.height
	if child.y < 0:
		child.y = 0

	for i, raptor in enumerate(pack):
		if raptor.x < (0 - raptor.width):
			reset_enemy(raptor)

		raptor.x -= dt * 150
		raptor.y += dt * randrange(-100, 100)

		if raptor.x < child.x:
			continue

		if raptor.y+raptor.height*0.5 < child.y:
			continue

		if raptor.y-raptor.height*0.5 > child.y+child.height*0.5:
			continue

		if (raptor.x-child.x) < (child.width+raptor.width)*0.5-10:
			if raptor_eating == i:
				raptor.x += dt * 150

			if child.image is child_death:
				continue

			score = "You died! Score: %.1f" % score
			child.image = child_death
			raptor.image = raptor_static
			raptor_eating = i


@window.event
def on_draw():
	window.clear()
	glClearColor(1,1,1,1)
	batch.draw()

def reset_enemy(raptor):
	raptor.scale = 0.5
	raptor.x = window.width + randrange(0,window.width*2)
	raptor.y = window.height * random()

child.scale = 0.5
pyglet.clock.schedule(update)
pyglet.app.run()