Replace tabs with spaces.

master
voussoir 2021-09-20 20:06:32 -07:00
parent 5f0453369e
commit 46912f8963
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
1 changed files with 454 additions and 454 deletions

View File

@ -5,523 +5,523 @@ from tkinter import Tk, Label, Frame
import random import random
class dodgygame: class dodgygame:
def __init__(self): def __init__(self):
tkvar = Tk() tkvar = Tk()
tkvar.resizable(0,0) tkvar.resizable(0,0)
tkvar.wm_title('Dodgy') tkvar.wm_title('Dodgy')
tkvar.iconbitmap('Excl.ico') tkvar.iconbitmap('Excl.ico')
arenasize = 40 arenasize = 40
self.xpos = int((arenasize-1)/2) self.xpos = int((arenasize-1)/2)
self.ypos = int((arenasize-1)/2) self.ypos = int((arenasize-1)/2)
self.data = ['#'*arenasize, '#'*arenasize] self.data = ['#'*arenasize, '#'*arenasize]
for x in range(arenasize-2): for x in range(arenasize-2):
self.data[1:1] = ['#' + '.'*(arenasize-2) + '#'] self.data[1:1] = ['#' + '.'*(arenasize-2) + '#']
self.labelframe = Frame(tkvar) self.labelframe = Frame(tkvar)
self.labelframe.grid(row=1, column=0, columnspan=100) self.labelframe.grid(row=1, column=0, columnspan=100)
#Allows me to center the gui units #Allows me to center the gui units
self.datalabel = Label(tkvar, text='\n'.join(self.data), font=('Terminal', 10)) self.datalabel = Label(tkvar, text='\n'.join(self.data), font=('Terminal', 10))
self.datalabel.grid(row=0, column=0, columnspan=100) self.datalabel.grid(row=0, column=0, columnspan=100)
self.stepslabel = Label(self.labelframe, text='0', font=('Consolas', 10)) self.stepslabel = Label(self.labelframe, text='0', font=('Consolas', 10))
self.stepslabel.grid(row=0, column=0) self.stepslabel.grid(row=0, column=0)
self.collectlabel = Label(self.labelframe, text='0', font=('Consolas', 10), bg="#ccc") self.collectlabel = Label(self.labelframe, text='0', font=('Consolas', 10), bg="#ccc")
self.collectlabel.grid(row=0, column=1) self.collectlabel.grid(row=0, column=1)
self.bomblabel = Label(self.labelframe, text='0', font=('Consolas', 10)) self.bomblabel = Label(self.labelframe, text='0', font=('Consolas', 10))
self.bomblabel.grid(row=0, column=2) self.bomblabel.grid(row=0, column=2)
self.phantlabel = Label(self.labelframe, text='0',font=('Consolas', 10), bg="#ccc") self.phantlabel = Label(self.labelframe, text='0',font=('Consolas', 10), bg="#ccc")
self.phantlabel.grid(row=0, column=3) self.phantlabel.grid(row=0, column=3)
self.poslabel = Label(self.labelframe, text=str(self.xpos) + " " + str(self.ypos), font=('Consolas', 10)) self.poslabel = Label(self.labelframe, text=str(self.xpos) + " " + str(self.ypos), font=('Consolas', 10))
self.poslabel.grid(row=0, column=4) self.poslabel.grid(row=0, column=4)
self.helplabel = Label(tkvar, text="Press H for help ->", font=('Consolas', 8)) self.helplabel = Label(tkvar, text="Press H for help ->", font=('Consolas', 8))
self.helplabel.grid(row=2, column=0, columnspan=100) self.helplabel.grid(row=2, column=0, columnspan=100)
self.consolelabel = Label(tkvar, text="Welcome", font=("Terminal", 8), width=arenasize, height=4) self.consolelabel = Label(tkvar, text="Welcome", font=("Terminal", 8), width=arenasize, height=4)
self.consolelabeldata = [] self.consolelabeldata = []
self.consolelabel.configure(anchor="nw", justify="left") self.consolelabel.configure(anchor="nw", justify="left")
self.consolelabel.grid(row=3, column=0) self.consolelabel.grid(row=3, column=0)
tkvar.bind('w', lambda data=self.data: mfresh(ymove=-1)) tkvar.bind('w', lambda data=self.data: mfresh(ymove=-1))
tkvar.bind('<Up>', lambda data=self.data: mfresh(ymove=-1)) tkvar.bind('<Up>', lambda data=self.data: mfresh(ymove=-1))
tkvar.bind('s', lambda data=self.data: mfresh(ymove=1)) tkvar.bind('s', lambda data=self.data: mfresh(ymove=1))
tkvar.bind('<Down>', lambda data=self.data: mfresh(ymove=1)) tkvar.bind('<Down>', lambda data=self.data: mfresh(ymove=1))
tkvar.bind('a', lambda data=self.data: mfresh(xmove=-1)) tkvar.bind('a', lambda data=self.data: mfresh(xmove=-1))
tkvar.bind('<Left>', lambda data=self.data: mfresh(xmove=-1)) tkvar.bind('<Left>', lambda data=self.data: mfresh(xmove=-1))
tkvar.bind('d', lambda data=self.data: mfresh(xmove=1)) tkvar.bind('d', lambda data=self.data: mfresh(xmove=1))
tkvar.bind('<Right>', lambda data=self.data: mfresh(xmove=1)) tkvar.bind('<Right>', lambda data=self.data: mfresh(xmove=1))
#tkvar.bind('c', lambda data=self.data: spawncandy()) #tkvar.bind('c', lambda data=self.data: spawncandy())
tkvar.bind('j', lambda data=self.data: spawnbomb()) tkvar.bind('j', lambda data=self.data: spawnbomb())
tkvar.bind('z', lambda data=self.data: spawnbomb()) tkvar.bind('z', lambda data=self.data: spawnbomb())
tkvar.bind('k', lambda data=self.data: spawnphantom()) tkvar.bind('k', lambda data=self.data: spawnphantom())
tkvar.bind('x', lambda data=self.data: spawnphantom()) tkvar.bind('x', lambda data=self.data: spawnphantom())
tkvar.bind('r', lambda data=self.data: restart()) tkvar.bind('r', lambda data=self.data: restart())
tkvar.bind('h', lambda data=self.data: helpreel()) tkvar.bind('h', lambda data=self.data: helpreel())
tkvar.bind('<Control-w>', quit) tkvar.bind('<Control-w>', quit)
self.candylist = [] self.candylist = []
self.enemylist = [] self.enemylist = []
self.bomblist = [] self.bomblist = []
self.phantomlist = [] self.phantomlist = []
self.goldcandylist = [] self.goldcandylist = []
self.entlist = [] self.entlist = []
self.symbols = {'char':'H', 'wall':'#', 'floor':' '} self.symbols = {'char':'H', 'wall':'#', 'floor':' '}
self.stepstaken = 0 self.stepstaken = 0
self.isdeath = False self.isdeath = False
self.candyspawnrate = 4 self.candyspawnrate = 4
self.collections = 0 self.collections = 0
self.enemyspawnrate = 50 self.enemyspawnrate = 50
self.enemyspawnmindist = 9 self.enemyspawnmindist = 9
self.enemydocollide = False self.enemydocollide = False
self.bombs = 0 self.bombs = 0
self.bombcost = 4 self.bombcost = 4
self.phantomcost = 60 self.phantomcost = 60
self.goldcandyflashrate = 8 self.goldcandyflashrate = 8
self.goldcandyspawnrate = 40 self.goldcandyspawnrate = 40
self.goldcandyspawnrand = 4 self.goldcandyspawnrand = 4
self.helplabelindex = -1 self.helplabelindex = -1
self.helplabeltexts = [ self.helplabeltexts = [
"<WASD>=Movement <J>=Bomb <K>=Phantom <R>=Restart ->", "<WASD>=Movement <J>=Bomb <K>=Phantom <R>=Restart ->",
"<UDLR>=Movement <Z>=Bomb <X>=Phantom <R>=Restart ->", "<UDLR>=Movement <Z>=Bomb <X>=Phantom <R>=Restart ->",
"<LMB>=Movement <RMB>=Bomb <MMB>=Phantom/Restart ->", "<LMB>=Movement <RMB>=Bomb <MMB>=Phantom/Restart ->",
self.symbols['char'] + " = You ->", self.symbols['char'] + " = You ->",
enemy.symbol + " = Exclamator ->", enemy.symbol + " = Exclamator ->",
candy.symbol + " = Candy ->", candy.symbol + " = Candy ->",
bomb.symbol + " = Bomb ->", bomb.symbol + " = Bomb ->",
"Avoid the Exclamators ->", "Avoid the Exclamators ->",
"Collect candy to earn bombs ->", "Collect candy to earn bombs ->",
"Drop bombs to snare Exclamators ->", "Drop bombs to snare Exclamators ->",
"Deploy phantoms to distract Exclamators ->", "Deploy phantoms to distract Exclamators ->",
"Phantoms have a minimum cost of " + str(self.phantomcost) + " ->", "Phantoms have a minimum cost of " + str(self.phantomcost) + " ->",
"But deploying phantom will consume all candy ->", "But deploying phantom will consume all candy ->",
"More candy consumed = longer phantom lifespan ->", "More candy consumed = longer phantom lifespan ->",
"Enjoy •"] "Enjoy •"]
def mfresh(xmove=0, ymove=0): def mfresh(xmove=0, ymove=0):
if not self.isdeath: if not self.isdeath:
#print(self.xpos, self.ypos, "==> ", end="") #print(self.xpos, self.ypos, "==> ", end="")
hasmoved = False hasmoved = False
if xmove != 0: if xmove != 0:
if (self.xpos > 1 or xmove > 0) and (self.xpos < (arenasize -2) or xmove < 0): if (self.xpos > 1 or xmove > 0) and (self.xpos < (arenasize -2) or xmove < 0):
self.xpos += xmove self.xpos += xmove
if self.phantomlist != []: if self.phantomlist != []:
ph = self.phantomlist[0] ph = self.phantomlist[0]
ph.x -= xmove ph.x -= xmove
if ph.x < 1: if ph.x < 1:
ph.x = 1 ph.x = 1
if ph.x > (arenasize-2): if ph.x > (arenasize-2):
ph.x = arenasize-2 ph.x = arenasize-2
hasmoved = True hasmoved = True
if ymove != 0: if ymove != 0:
if (self.ypos > 1 or ymove > 0) and (self.ypos < (arenasize -2) or ymove < 0): if (self.ypos > 1 or ymove > 0) and (self.ypos < (arenasize -2) or ymove < 0):
self.ypos += ymove self.ypos += ymove
if self.phantomlist != []: if self.phantomlist != []:
ph = self.phantomlist[0] ph = self.phantomlist[0]
ph.y -= ymove ph.y -= ymove
if ph.y < 1: if ph.y < 1:
ph.y = 1 ph.y = 1
if ph.y > (arenasize-2): if ph.y > (arenasize-2):
ph.y = arenasize-2 ph.y = arenasize-2
hasmoved = True hasmoved = True
if hasmoved: if hasmoved:
if self.phantomlist != []: if self.phantomlist != []:
ph = self.phantomlist[0] ph = self.phantomlist[0]
ph.lifespan -= 1 ph.lifespan -= 1
if ph.lifespan <= 0: if ph.lifespan <= 0:
self.phantomlist.remove(ph) self.phantomlist.remove(ph)
self.entlist.remove(ph) self.entlist.remove(ph)
del ph del ph
#print(self.xpos, self.ypos, '|', self.stepstaken) #print(self.xpos, self.ypos, '|', self.stepstaken)
for candies in self.candylist: for candies in self.candylist:
if candies.x == self.xpos and candies.y == self.ypos: if candies.x == self.xpos and candies.y == self.ypos:
#print('Collection') #print('Collection')
collect(1) collect(1)
self.candylist.remove(candies) self.candylist.remove(candies)
self.entlist.remove(candies) self.entlist.remove(candies)
del candies del candies
for goldcandies in self.goldcandylist: for goldcandies in self.goldcandylist:
if goldcandies.x == self.xpos and goldcandies.y == self.ypos: if goldcandies.x == self.xpos and goldcandies.y == self.ypos:
tempvar = '[ ' + goldcandy.symbol + ' ] Got gold' tempvar = '[ ' + goldcandy.symbol + ' ] Got gold'
print(tempvar) print(tempvar)
printr(tempvar) printr(tempvar)
collect(5) collect(5)
self.goldcandylist.remove(goldcandies) self.goldcandylist.remove(goldcandies)
self.entlist.remove(goldcandies) self.entlist.remove(goldcandies)
for enemies in self.enemylist: for enemies in self.enemylist:
if enemies.x == self.xpos and enemies.y == self.ypos: if enemies.x == self.xpos and enemies.y == self.ypos:
tempvar = '[ ' + self.symbols['char'] + ' ] Death' tempvar = '[ ' + self.symbols['char'] + ' ] Death'
print(tempvar) print(tempvar)
printr(tempvar) printr(tempvar)
self.isdeath = True self.isdeath = True
self.datalabel.configure(fg='Red') self.datalabel.configure(fg='Red')
if not self.isdeath: if not self.isdeath:
if hasmoved: if hasmoved:
self.stepstaken += 1 self.stepstaken += 1
if self.stepstaken % self.candyspawnrate == 0: if self.stepstaken % self.candyspawnrate == 0:
spawncandy() spawncandy()
goldchance = random.randint(self.stepstaken-self.goldcandyspawnrand, self.stepstaken+self.goldcandyspawnrand) goldchance = random.randint(self.stepstaken-self.goldcandyspawnrand, self.stepstaken+self.goldcandyspawnrand)
#print(goldchance) #print(goldchance)
if goldchance % self.goldcandyspawnrate == 0: if goldchance % self.goldcandyspawnrate == 0:
spawngoldcandy() spawngoldcandy()
if self.stepstaken % self.enemyspawnrate == 0: if self.stepstaken % self.enemyspawnrate == 0:
spawnenemy() spawnenemy()
for x in range(self.stepstaken // 200): for x in range(self.stepstaken // 200):
spawnenemy() spawnenemy()
for en in self.enemylist: for en in self.enemylist:
oldx = en.x oldx = en.x
oldy = en.y oldy = en.y
if self.phantomlist == []: if self.phantomlist == []:
en.approach(self.xpos, self.ypos) en.approach(self.xpos, self.ypos)
else: else:
ph = self.phantomlist[0] ph = self.phantomlist[0]
en.approach(ph.x, ph.y) en.approach(ph.x, ph.y)
if self.enemydocollide: if self.enemydocollide:
for otheren in self.enemylist: for otheren in self.enemylist:
if en != otheren: if en != otheren:
if en.x == otheren.x and en.y == otheren.y: if en.x == otheren.x and en.y == otheren.y:
tempvar = '[ '+enemy.symbol+' ] Enemy collision at '+str(en.x)+' '+str(en.y) tempvar = '[ '+enemy.symbol+' ] Enemy collision at '+str(en.x)+' '+str(en.y)
print(tempvar) print(tempvar)
printr(tempvar) printr(tempvar)
en.x = oldx en.x = oldx
en.y = oldy en.y = oldy
#print(dist(en.x, en.y, self.xpos, self.ypos)) #print(dist(en.x, en.y, self.xpos, self.ypos))
if en.x < 1: if en.x < 1:
en.x = 1 en.x = 1
if en.x > (arenasize-2): if en.x > (arenasize-2):
en.x = arenasize-2 en.x = arenasize-2
if en.y < 1: if en.y < 1:
en.y = 1 en.y = 1
if en.y > (arenasize-2): if en.y > (arenasize-2):
en.y = arenasize-2 en.y = arenasize-2
mfresh() mfresh()
for goldcandies in self.goldcandylist: for goldcandies in self.goldcandylist:
goldcandies.tick(self.goldcandyflashrate) goldcandies.tick(self.goldcandyflashrate)
if goldcandies.lifespan <= 0: if goldcandies.lifespan <= 0:
self.goldcandylist.remove(goldcandies) self.goldcandylist.remove(goldcandies)
self.entlist.remove(goldcandies) self.entlist.remove(goldcandies)
del goldcandies del goldcandies
for bombs in self.bomblist: for bombs in self.bomblist:
for enemies in self.enemylist: for enemies in self.enemylist:
if enemies.x == bombs.x and enemies.y == bombs.y: if enemies.x == bombs.x and enemies.y == bombs.y:
self.bomblist.remove(bombs) self.bomblist.remove(bombs)
self.enemylist.remove(enemies) self.enemylist.remove(enemies)
self.entlist.remove(bombs) self.entlist.remove(bombs)
self.entlist.remove(enemies) self.entlist.remove(enemies)
tempvar = '[ ' + bomb.symbol + ' ] Bang' tempvar = '[ ' + bomb.symbol + ' ] Bang'
print(tempvar) print(tempvar)
printr(tempvar) printr(tempvar)
mfresh() mfresh()
self.data = [self.symbols['wall']*arenasize, self.symbols['wall']*arenasize] self.data = [self.symbols['wall']*arenasize, self.symbols['wall']*arenasize]
for x in range(arenasize-2): for x in range(arenasize-2):
self.data[1:1] = [self.symbols['wall'] + self.symbols['floor']*(arenasize-2) + self.symbols['wall']] self.data[1:1] = [self.symbols['wall'] + self.symbols['floor']*(arenasize-2) + self.symbols['wall']]
for ycoord in range(len(self.data)): for ycoord in range(len(self.data)):
yl = self.data[ycoord] yl = self.data[ycoord]
if ycoord == self.ypos and not self.isdeath: if ycoord == self.ypos and not self.isdeath:
#print(ycoord) #print(ycoord)
yl = yl[:self.xpos] + self.symbols['char'] + yl[self.xpos+1:] yl = yl[:self.xpos] + self.symbols['char'] + yl[self.xpos+1:]
#print(yl) #print(yl)
self.entlist.sort(key=lambda p: p.x) self.entlist.sort(key=lambda p: p.x)
for entities in self.entlist: for entities in self.entlist:
if entities.y == ycoord: if entities.y == ycoord:
yl = yl[:entities.x] + entities.symbol + yl[entities.x+1:] yl = yl[:entities.x] + entities.symbol + yl[entities.x+1:]
self.data[ycoord] = yl self.data[ycoord] = yl
self.datalabel.configure(text='\n'.join(self.data)) self.datalabel.configure(text='\n'.join(self.data))
self.stepslabel.configure(text="%04d"%(self.stepstaken) + " steps") self.stepslabel.configure(text="%04d"%(self.stepstaken) + " steps")
self.collectlabel.configure(text="%03d"%(self.collections) + " candy") self.collectlabel.configure(text="%03d"%(self.collections) + " candy")
self.bomblabel.configure(text="%02d"%(self.bombs) + " bombs") self.bomblabel.configure(text="%02d"%(self.bombs) + " bombs")
self.poslabel.configure(text="%02d"%(self.xpos) + " " + "%02d"%(self.ypos)) self.poslabel.configure(text="%02d"%(self.xpos) + " " + "%02d"%(self.ypos))
if self.collections >= self.phantomcost: if self.collections >= self.phantomcost:
self.phantlabel.configure(text='1 phantom') self.phantlabel.configure(text='1 phantom')
else: else:
self.phantlabel.configure(text='0 phantom') self.phantlabel.configure(text='0 phantom')
def printr(info): def printr(info):
self.consolelabeldata.append(str(info)) self.consolelabeldata.append(str(info))
self.consolelabeldata = self.consolelabeldata[-4:] self.consolelabeldata = self.consolelabeldata[-4:]
self.consolelabel.configure(text='\n'.join(self.consolelabeldata)) self.consolelabel.configure(text='\n'.join(self.consolelabeldata))
def translatemouse(event, middlemouse=False): def translatemouse(event, middlemouse=False):
event.x -= 9 event.x -= 9
event.y -= 9 event.y -= 9
#485 #485
event.x /= 8 event.x /= 8
event.y /= 12 event.y /= 12
#spawncandy(round(event.x), round(event.y)) #spawncandy(round(event.x), round(event.y))
#print(event.x, event.y) #print(event.x, event.y)
xdif = event.x - self.xpos xdif = event.x - self.xpos
ydif = event.y - self.ypos ydif = event.y - self.ypos
if abs(xdif) >= 0.5 or abs(ydif) >= 0.5: if abs(xdif) >= 0.5 or abs(ydif) >= 0.5:
if abs(xdif) >= abs(ydif): if abs(xdif) >= abs(ydif):
xdif /= abs(xdif) xdif /= abs(xdif)
xdif = int(xdif) xdif = int(xdif)
mfresh(xmove= xdif) mfresh(xmove= xdif)
else: else:
ydif /= abs(ydif) ydif /= abs(ydif)
ydif = int(ydif) ydif = int(ydif)
mfresh(ymove= ydif) mfresh(ymove= ydif)
def middlemouse(): def middlemouse():
if self.isdeath: if self.isdeath:
restart() restart()
else: else:
spawnphantom() spawnphantom()
tkvar.bind('<Button-1>', translatemouse) tkvar.bind('<Button-1>', translatemouse)
tkvar.bind('<Button-2>', lambda data=self.data: middlemouse()) tkvar.bind('<Button-2>', lambda data=self.data: middlemouse())
tkvar.bind('<Button-3>', lambda data=self.data: spawnbomb()) tkvar.bind('<Button-3>', lambda data=self.data: spawnbomb())
def helpreel(): def helpreel():
self.helplabelindex += 1 self.helplabelindex += 1
if self.helplabelindex > (len(self.helplabeltexts) - 1): if self.helplabelindex > (len(self.helplabeltexts) - 1):
self.helplabelindex = 0 self.helplabelindex = 0
self.helplabel.configure(text=self.helplabeltexts[self.helplabelindex]) self.helplabel.configure(text=self.helplabeltexts[self.helplabelindex])
def collect(score): def collect(score):
for x in range(score): for x in range(score):
self.collections += 1 self.collections += 1
if self.collections % self.bombcost == 0: if self.collections % self.bombcost == 0:
self.bombs += 1 self.bombs += 1
def spawnbomb(): def spawnbomb():
goodtogo = True goodtogo = True
for bombs in self.bomblist: for bombs in self.bomblist:
if bombs.x == self.xpos and bombs.y == self.ypos: if bombs.x == self.xpos and bombs.y == self.ypos:
goodtogo = False goodtogo = False
if goodtogo: if goodtogo:
if self.bombs > 0: if self.bombs > 0:
self.bombs -= 1 self.bombs -= 1
newbomb = bomb(self.xpos, self.ypos) newbomb = bomb(self.xpos, self.ypos)
self.bomblist.append(newbomb) self.bomblist.append(newbomb)
self.entlist.append(newbomb) self.entlist.append(newbomb)
mfresh() mfresh()
def spawnphantom(): def spawnphantom():
goodtogo = True goodtogo = True
if self.collections < self.phantomcost: if self.collections < self.phantomcost:
tempvar='[ ' + self.symbols['char'] + ' ] Not enough Candy. ' + str(self.collections) + '/' + str(self.phantomcost) + '. -' + str(self.phantomcost-self.collections) tempvar='[ ' + self.symbols['char'] + ' ] Not enough Candy. ' + str(self.collections) + '/' + str(self.phantomcost) + '. -' + str(self.phantomcost-self.collections)
print(tempvar) print(tempvar)
printr(tempvar) printr(tempvar)
goodtogo = False goodtogo = False
if self.phantomlist != []: if self.phantomlist != []:
goodtogo = False goodtogo = False
if goodtogo: if goodtogo:
life = 15 life = 15
self.collections = 0 self.collections = 0
life += round(self.collections / 3) life += round(self.collections / 3)
tempvar = '[ ' + self.symbols['char'] + ' ] New phantom with ' + str(life) + ' life' tempvar = '[ ' + self.symbols['char'] + ' ] New phantom with ' + str(life) + ' life'
print(tempvar) print(tempvar)
printr(tempvar) printr(tempvar)
newphantom = phantom(self.xpos, self.ypos, life) newphantom = phantom(self.xpos, self.ypos, life)
self.phantomlist.append(newphantom) self.phantomlist.append(newphantom)
self.entlist.append(newphantom) self.entlist.append(newphantom)
mfresh() mfresh()
def spawncandy(forcex=None, forcey=None): def spawncandy(forcex=None, forcey=None):
goodtogo = True goodtogo = True
if forcex == None or forcey == None: if forcex == None or forcey == None:
newx = random.randint(1, arenasize-2) newx = random.randint(1, arenasize-2)
newy = random.randint(1, arenasize-2) newy = random.randint(1, arenasize-2)
else: else:
newx = forcex newx = forcex
newy = forcey newy = forcey
if self.xpos == newx and self.ypos == newy: if self.xpos == newx and self.ypos == newy:
goodtogo = False goodtogo = False
if goodtogo: if goodtogo:
for candies in self.candylist: for candies in self.candylist:
if candies.x == newx and candies.y == newy: if candies.x == newx and candies.y == newy:
goodtogo = False goodtogo = False
if goodtogo: if goodtogo:
tempvar = '[ ' + candy.symbol + ' ] New candy at ' + str(newx)+' '+str(newy) tempvar = '[ ' + candy.symbol + ' ] New candy at ' + str(newx)+' '+str(newy)
print(tempvar) print(tempvar)
printr(tempvar) printr(tempvar)
newcan = candy(newx, newy) newcan = candy(newx, newy)
self.candylist.append(newcan) self.candylist.append(newcan)
self.entlist.append(newcan) self.entlist.append(newcan)
mfresh() mfresh()
def spawngoldcandy(forcex=None, forcey=None): def spawngoldcandy(forcex=None, forcey=None):
goodtogo = True goodtogo = True
if forcex == None or forcey == None: if forcex == None or forcey == None:
newx = random.randint(1, arenasize-2) newx = random.randint(1, arenasize-2)
newy = random.randint(1, arenasize-2) newy = random.randint(1, arenasize-2)
spawntries = 0 spawntries = 0
while (dist(self.xpos, self.ypos, newx, newy) < self.enemyspawnmindist): while (dist(self.xpos, self.ypos, newx, newy) < self.enemyspawnmindist):
newx = random.randint(1, arenasize-2) newx = random.randint(1, arenasize-2)
newy = random.randint(1, arenasize-2) newy = random.randint(1, arenasize-2)
spawntries += 1 spawntries += 1
#print('Rerolling from', newx, newy) #print('Rerolling from', newx, newy)
if spawntries == 20: if spawntries == 20:
#print('Could not spawn enemy') #print('Could not spawn enemy')
goodtogo = False goodtogo = False
break break
else: else:
newx = forcex newx = forcex
newy = forcey newy = forcey
if goodtogo: if goodtogo:
for entity in self.entlist: for entity in self.entlist:
if entity.x == newx and entity.y == newy: if entity.x == newx and entity.y == newy:
goodtogo = False goodtogo = False
if goodtogo: if goodtogo:
lifespan= dist(self.xpos, self.ypos, newx, newy) lifespan= dist(self.xpos, self.ypos, newx, newy)
lifespan *= 2 lifespan *= 2
tempvar = '[ ' + goldcandy.symbol + ' ] New gold candy' tempvar = '[ ' + goldcandy.symbol + ' ] New gold candy'
print(tempvar) print(tempvar)
printr(tempvar) printr(tempvar)
newcan = goldcandy(newx, newy, lifespan) newcan = goldcandy(newx, newy, lifespan)
self.goldcandylist.append(newcan) self.goldcandylist.append(newcan)
self.entlist.append(newcan) self.entlist.append(newcan)
mfresh() mfresh()
def spawnenemy(): def spawnenemy():
newx = random.randint(1, arenasize-2) newx = random.randint(1, arenasize-2)
newy = random.randint(1, arenasize-2) newy = random.randint(1, arenasize-2)
goodtogo = True goodtogo = True
spawntries = 0 spawntries = 0
while (dist(self.xpos, self.ypos, newx, newy) < self.enemyspawnmindist): while (dist(self.xpos, self.ypos, newx, newy) < self.enemyspawnmindist):
newx = random.randint(1, arenasize-2) newx = random.randint(1, arenasize-2)
newy = random.randint(1, arenasize-2) newy = random.randint(1, arenasize-2)
spawntries += 1 spawntries += 1
#print('Rerolling from', newx, newy) #print('Rerolling from', newx, newy)
if spawntries == 10: if spawntries == 10:
print('Could not spawn enemy') print('Could not spawn enemy')
goodtogo = False goodtogo = False
break break
if goodtogo: if goodtogo:
for ens in self.enemylist: for ens in self.enemylist:
if ens.x == newx and ens.y == newy: if ens.x == newx and ens.y == newy:
goodtogo = False goodtogo = False
if goodtogo: if goodtogo:
tempvar = '[ ' + enemy.symbol + ' ] New enemy at '+str(newx)+' '+str(newy) tempvar = '[ ' + enemy.symbol + ' ] New enemy at '+str(newx)+' '+str(newy)
print(tempvar) print(tempvar)
printr(tempvar) printr(tempvar)
newen = enemy(newx, newy, 1) newen = enemy(newx, newy, 1)
self.enemylist.append(newen) self.enemylist.append(newen)
self.entlist.append(newen) self.entlist.append(newen)
mfresh() mfresh()
def restart(): def restart():
tempvar = 'Resetting game.' tempvar = 'Resetting game.'
print(tempvar) print(tempvar)
printr(tempvar) printr(tempvar)
self.consolelabeldata = [] self.consolelabeldata = []
self.consolelabel.configure(text='') self.consolelabel.configure(text='')
self.xpos = int((arenasize-1)/2) self.xpos = int((arenasize-1)/2)
self.ypos = int((arenasize-1)/2) self.ypos = int((arenasize-1)/2)
while self.entlist != []: while self.entlist != []:
del self.entlist[0] del self.entlist[0]
self.candylist = [] self.candylist = []
self.enemylist = [] self.enemylist = []
self.bomblist = [] self.bomblist = []
self.phantomlist = [] self.phantomlist = []
self.entlist = [] self.entlist = []
self.stepstaken = 0 self.stepstaken = 0
self.collections = 0 self.collections = 0
self.bombs = 0 self.bombs = 0
self.isdeath = False self.isdeath = False
spawncandy() spawncandy()
self.datalabel.configure(fg='Black') self.datalabel.configure(fg='Black')
mfresh() mfresh()
def dist(xa, ya, xb, yb): def dist(xa, ya, xb, yb):
#distance formula #distance formula
result = (xa - xb) **2 result = (xa - xb) **2
result += (ya - yb) ** 2 result += (ya - yb) ** 2
result = result ** 0.5 result = result ** 0.5
result = int(result) result = int(result)
return result return result
mfresh() mfresh()
spawncandy() spawncandy()
tkvar.mainloop() tkvar.mainloop()
class candy: class candy:
symbol = "@" symbol = "@"
def __init__(self, x, y): def __init__(self, x, y):
self.x = x self.x = x
self.y = y self.y = y
self.symbol = candy.symbol self.symbol = candy.symbol
class enemy: class enemy:
symbol = "!" symbol = "!"
def __init__(self, x, y, anger): def __init__(self, x, y, anger):
self.symbol = enemy.symbol self.symbol = enemy.symbol
self.x = x self.x = x
self.y = y self.y = y
self.anger = anger self.anger = anger
self.movementx = 0 self.movementx = 0
self.movementy = 0 self.movementy = 0
def approach(self, targx, targy): def approach(self, targx, targy):
hasmoved = False hasmoved = False
xdif = abs(targx - self.x) xdif = abs(targx - self.x)
ydif = abs(targy - self.y) ydif = abs(targy - self.y)
if xdif > ydif: if xdif > ydif:
if targx != self.x and hasmoved == False: if targx != self.x and hasmoved == False:
self.movementx += int(((targx-self.x) / (abs(targx-self.x)))) self.movementx += int(((targx-self.x) / (abs(targx-self.x))))
if self.movementx > 1: if self.movementx > 1:
self.movementx = 1 self.movementx = 1
if self.movementx < -1: if self.movementx < -1:
self.movementx = -1 self.movementx = -1
else: else:
if targy != self.y and hasmoved == False: if targy != self.y and hasmoved == False:
self.movementy += int(((targy-self.y) / (abs(targy-self.y)))) self.movementy += int(((targy-self.y) / (abs(targy-self.y))))
if self.movementy > 1: if self.movementy > 1:
self.movementy = 1 self.movementy = 1
if self.movementy < -1: if self.movementy < -1:
self.movementy = -1 self.movementy = -1
if not hasmoved: if not hasmoved:
self.x += self.movementx self.x += self.movementx
if not hasmoved: if not hasmoved:
self.y += self.movementy self.y += self.movementy
class bomb: class bomb:
symbol = "X" symbol = "X"
def __init__(self, x, y): def __init__(self, x, y):
self.x = x self.x = x
self.y = y self.y = y
self.symbol = bomb.symbol self.symbol = bomb.symbol
class phantom: class phantom:
symbol = "H" symbol = "H"
def __init__(self, x, y, lifespan): def __init__(self, x, y, lifespan):
self.x = x self.x = x
self.y = y self.y = y
self.lifespan = lifespan self.lifespan = lifespan
self.symbol = phantom.symbol self.symbol = phantom.symbol
class goldcandy: class goldcandy:
symbol = "$" symbol = "$"
@ -537,4 +537,4 @@ class goldcandy:
else: else:
self.symbol = candy.symbol self.symbol = candy.symbol
t = dodgygame() t = dodgygame()