forked from IrvKalb/Object-Oriented-Python-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonsterExample.py
More file actions
34 lines (25 loc) · 1.1 KB
/
Copy pathMonsterExample.py
File metadata and controls
34 lines (25 loc) · 1.1 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
# Main program for creating monsters
import random
class Monster():
def __init__(self, nRows, nCols, maxSpeed):
self.nRows = nRows # save away
self.nCols = nCols # save away
self.myRow = random.randrange(self.nRows) #chooses a random row
self.myCol = random.randrange(self.nCols) #chooses a random col
self.mySpeedX = random.randrange(-maxSpeed, maxSpeed) + 1 # chooses an X speed
self.mySpeedY = random.randrange(-maxSpeed, maxSpeed + 1) # chooses a Y speed
# Set other instance variables like health, power, etc.
def move(self):
self.myRow = (self.myRow + self.mySpeedY) % self.nRows
self.myCol = (self.myCol + self.mySpeedX) % self.nCols
N_MONSTERS = 20
N_ROWS = 100 # could be any size
N_COLS = 200 # could be any size
MAX_SPEED = 4
monsterList = [] # start with an empty list
for i in range(N_MONSTERS):
oMonster = Monster(N_ROWS, N_COLS, MAX_SPEED) # create a Monster
monsterList.append(oMonster) # add Monster to our list
# Later, when playing the game ...
for oMonster in monsterList:
oMonster.move()