-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
169 lines (166 loc) · 5.02 KB
/
game.py
File metadata and controls
169 lines (166 loc) · 5.02 KB
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
from pygame import *
import math
import spells
class Sprite:
def __init__(self, xpos, ypos, filename):
self.x = xpos
self.y = ypos
self.bitmap = image.load(filename)
self.bitmap.set_colorkey((0,0,0))
def set_position(self, xpos, ypos):
self.x = xpos
self.y = ypos
def render(self):
global screen
screen.blit(self.bitmap, (self.x, self.y))
class Mob:
def __init__(self,newGame):
self.game = newGame
self.radius = 100
self.damage = 100
self.x=20
self.y=400
self.health=10000
self.states=[]
def addState(self,a):
states.append(a)
def run(self):
a=0
for i in self.states:
if(not i.run()):
del self.states[a]
a+=1
if not (self.health>0):
raise "You killed the mob!!!"
print "WIN"
print 4/0
if (self.game.player.x-self.x)**2+(self.game.player.y-self.y)**2<=self.radius**2:
self.game.player.health -= self.damage
return self.health>0
class Player:
def __init__(self):
self.x=20
self.y=400
self.energy=50000
self.health=10000
self.states=[]
def run(self):
self.energy+=100
self.health+=0
if self.health < 0 or self.energy < 0:
raise "You died."
print "DIE"
print 4/0
return False
class Spell:
def __init__(self,newGame):
self.game=newGame
self.x=self.game.player.x
self.y=self.game.player.y
self.xvelocity=0
self.yvelocity=0
def setSpell(self,newUserSpell):
self.userSpell=newUserSpell
def run(self):
self.x+=self.xvelocity
self.y+=self.yvelocity
self.alive = self.userSpell.run()
return self.alive
def getNouns(self):
return self.game.nouns
def getPlayerX(self):
return self.game.player.x
def getPlayerY(self):
return self.game.player.y
def doDamageWithinRadius(self,damage,radius):
if (self.game.mob.x-self.x)**2+(self.game.mob.y-self.y)**2<=radius**2:
self.game.mob.health-=damage
self.game.player.energy-=radius+damage
def healWithinRadius(self,heal,radius):
if (self.game.player.x-self.x)**2+(self.game.player.y-self.y)**2<=radius**2:
self.game.player.health+=heal
self.game.player.energy-=radius+heal
def changeVelocity(self,xChangeChange,yChangeChange):
self.xvelocity+=xChangeChange
self.yvelocity+=yChangeChange
self.game.player.energy-=xChangeChange+yChangeChange
class Game:
def __init__(self):
init()
global screen
key.set_repeat(1, 1)
display.set_caption('SrcError')
self.playerSprite = Sprite(20, 400, 'player.png')
self.mobSprite = Sprite(20, 400, 'mob.png')
self.player=Player()
self.mob=Mob(self)
self.nouns=[self.player,self.mob]
self.spellList=self.getSpells()
self.activeSpells=[]
def cast(self,index):
a=Spell(self)
b=self.spellList[index](a)
a.setSpell(b)
self.nouns.append(a)
self.activeSpells.append([Sprite(self.playerSprite.x,self.playerSprite.y,'spell.png'),a])
def getSpells(self):
ret=[]
for i in spells.names:
ret.append(eval("spells."+i))
return ret
def run(self):
global screen
backdrop = image.load('backdrop.bmp')
screen.blit(backdrop, (0, 0))
for ourevent in event.get():
if ourevent.type == QUIT:
quit = 1
if ourevent.type == KEYDOWN:
if ourevent.key == K_RIGHT:
self.playerSprite.x += 20
self.player.x += 20
if ourevent.key == K_LEFT:
self.playerSprite.x -=20
self.player.x -= 20
if ourevent.key == K_DOWN:
self.playerSprite.y +=20
self.player.y += 20
if ourevent.key == K_UP:
self.playerSprite.y -=20
self.player.y -= 20
if ourevent.key == K_1:
self.cast(0)
if ourevent.key == K_2:
self.cast(1)
if ourevent.key == K_3:
self.cast(2)
a=0
for i in self.nouns:
if(not i.run()):
del self.nouns[a]
self.player.run()
if True:
self.mobSprite.x-=int(0.02*(self.mob.x-self.player.x))
self.mobSprite.y-=int(0.02*(self.mob.y-self.player.y))
self.mob.x=self.mobSprite.x
self.mob.y=self.mobSprite.y
iterer=0
for i in self.activeSpells:
k=i[1].run()
if(not k):
del self.activeSpells[iterer]
break
i[0].x=i[1].x
i[0].y=i[1].y
i[0].render()
iterer+=1
a+=1
self.mobSprite.render()
self.playerSprite.render()
print ["Energy: " + str(self.player.energy), "Health: " + str(self.player.health), "Mob Health: " + str(self.mob.health)]
display.update()
time.delay(5)
screen = display.set_mode((768,768))
a=Game()
for i in range(100000):
a.run()