Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from random import randrange as rnd, choice
- from PIL import ImageTk, Image
- import os, sys
- import time
- root = Tk()
- width = 900
- hight = 600
- padding = 50
- root.geometry(str(width)+'x'+str(hight)+'+100+100')
- canv = Canvas(bg='lightblue')
- canv.pack(fill=BOTH,expand=1)
- m = 30
- v = 4
- def pointInSegment(x,x1,x2):
- return x1 <= x <= x2
- def segmentInSegment(x1,x2,xx1,xx2):
- return pointInSegment(x1,xx1,xx2) or pointInSegment(x2,xx1,xx2) or \
- pointInSegment(xx1,x1,x2) or pointInSegment(xx2,x1,x2)
- def rectInRect(x1,y1,x2,y2,xx1,yy1,xx2,yy2):
- if segmentInSegment(x1,x2,xx1,xx2) and segmentInSegment(y1,y2,yy1,yy2):
- return True
- if segmentInSegment(xx1,xx2,x1,x2) and segmentInSegment(yy1,yy2,y1,y2):
- return True
- return False
- class Object():
- def __init__(self,x,y,color):
- self.x = x+m*0.5
- self.y = y+m*0.5
- self.rr = r = int(self.y - y0)//m #row
- self.cc = c = int(self.x - x0)//m #column
- self.r = m*0.4
- self.vx = 0
- self.vy = 0
- self.key = False
- self.m = None
- self.color = color
- self.command = 'stop'
- self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill=self.color)
- self.update()
- def create_wave(self):
- self.m = [[0 for c in range(len(a[0]))] for r in range(len(a))]
- m = self.m
- r = self.rr
- c = self.cc
- k = 1
- m[r][c] = k
- k += 1
- q = [(r,c)]
- while q:
- k += 1
- qq = []
- for r,c in q:
- for dr, dc in ((-1,0),(1,0),(0,-1),(0,1)):
- if a[r+dr][c+dc] == '0' and m[r+dr][c+dc] == 0:
- m[r+dr][c+dc] = k
- qq.append((r+dr,c+dc))
- q = qq
- root.after(300,self.create_wave)
- def update(self):
- self.rr = r = int(self.y - y0)//m #row
- self.cc = c = int(self.x - x0)//m #column
- xc = x0 + c*m +m//2
- yc = y0 + r*m +m//2
- if abs(self.x - xc) <= abs(self.vx) and abs(self.y - yc) <= abs(self.vy):
- #print(self.command)
- if self.vx > 0:
- if a[r][c+1] != '0':
- if not(a[r][c+1] == 'd' and self.key):
- self.command = 'stop'
- elif self.vx < 0:
- if a[r][c-1] != '0':
- if not(a[r][c-1] == 'd' and self.key):
- self.command = 'stop'
- elif self.vy < 0:
- if a[r-1][c] != '0':
- if not(a[r-1][c] == 'd' and self.key):
- self.command = 'stop'
- elif self.vy > 0:
- if a[r+1][c] != '0':
- if not(a[r+1][c] == 'd' and self.key):
- self.command = 'stop'
- if self.command == 'stop':
- self.vx = 0
- self.vy = 0
- self.y = yc
- self.x = xc
- elif self.command == 'left':
- if a[r][c-1] == '0' or (a[r][c-1] == 'd' and self.key):
- self.y = yc
- self.vx = -self.v
- self.vy = 0
- self.command = ''
- elif self.command == 'right':
- if a[r][c+1] == '0' or (a[r][c+1] == 'd' and self.key):
- self.y = yc
- self.vx = self.v
- self.vy = 0
- self.command = ''
- elif self.command == 'down':
- if a[r+1][c] == '0' or (a[r+1][c] == 'd' and self.key):
- self.x = xc
- self.vx = 0
- self.vy = self.v
- self.command = ''
- elif self.command == 'up':
- if a[r-1][c] == '0' or (a[r-1][c] == 'd' and self.key):
- self.x = xc
- self.vx = 0
- self.vy = -self.v
- self.command = ''
- ##print(self.points)
- self.x += self.vx
- self.y += self.vy
- canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
- root.after(30,self.update)
- class ControlPoint(Object):
- def __init__(self,x,y):
- super().__init__(x,y,'cyan')
- root.after(1000,self.create_wave)
- class Teleport():
- def __init__(self,x,y):
- self.x = x+m*0.5
- self.y = y+m*0.5
- self.r = m*0.4
- self.color = 'black'
- self.active = True
- self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill=self.color)
- self.update()
- def update(self):
- x1 = self.x - self.r
- x2 = self.x + self.r
- y1 = self.y - self.r
- y2 = self.y + self.r
- px1 = pacman.x - pacman.r
- px2 = pacman.x + pacman.r
- py1 = pacman.y - pacman.r
- py2 = pacman.y + pacman.r
- if self.active:
- if rectInRect(x1,y1,x2,y2,px1,py1,px2,py2):
- pacman.teleport(self)
- else:
- if not rectInRect(x1,y1,x2,y2,px1,py1,px2,py2):
- self.active = True
- root.after(30,self.update)
- class Enemy(Object):
- def __init__(self,x,y,color):
- self.mode = 'controlPoints'
- self.modeN = 1
- self.path = None
- super().__init__(x,y,color)
- self.v = 2
- root.after(100,self.auto)
- def isfree(self,dr,dc):
- return a[self.rr+dr][self.cc+dc] != '0'
- def create_path(self,m):
- tr = self.rr
- tc = self.cc
- res = []
- while m[tr][tc] > 1:
- min_m = m[tr][tc]
- r_m = tr
- c_m = tc
- for dr, dc in ((-1,0),(1,0),(0,-1),(0,1)):
- if m[tr+dr][tc+dc] < min_m and m[tr+dr][tc+dc] != 0:
- min_m = m[tr+dr][tc+dc]
- r_m = tr+dr
- c_m = tc+dc
- res.append((r_m,c_m))
- tr = r_m
- tc = c_m
- return res
- def auto(self):
- if pacman.m:
- path = self.create_path(pacman.m)
- if len(path) < 5:
- self.mode = 'pacman'
- self.v = 5
- else:
- self.mode = 'controlPoints'
- self.v = 2
- if self.mode == 'controlPoints':
- self.v = 2
- if self.rr == controlPoints[self.modeN].rr and self.cc == controlPoints[self.modeN].cc:
- self.modeN = rnd(len(controlPoints))
- if self.mode == 'pacman':
- if pacman.m:
- self.path = self.create_path(pacman.m)
- else:
- if controlPoints[self.modeN].m:
- self.path = self.create_path(controlPoints[self.modeN].m)
- if self.path:
- tr,tc = self.path[0]
- if tr > self.rr:
- self.command = 'down'
- if tr < self.rr:
- self.command = 'up'
- if tc > self.cc:
- self.command = 'right'
- if tc < self.cc:
- self.command = 'left'
- root.after(100,self.auto)
- class Pacman(Object):
- def __init__(self,x,y):
- self.points = 0
- self.v = 3
- self.info = canv.create_text(30,20,font='Arial 20', text = self.points)
- self.info_key = canv.create_text(100,20,font='Arial 20', text = '')
- super().__init__(x,y,'green')
- root.after(300,self.create_wave)
- def pickup_coin(self,coin):
- self.points += coin.value
- canv.itemconfig(self.info,text = self.points)
- coin.kill()
- def teleport(self,port):
- tt = teleports.copy()
- tt.remove(port)
- there = choice(tt)
- there.active = False
- self.x = there.x
- self.y = there.y
- def pickup_key(self,coin):
- self.key = True
- canv.itemconfig(self.info_key,text = 'KEY')
- key.kill()
- class Key():
- def __init__(self,x,y):
- self.x = x+m*0.5
- self.y = y+m*0.5
- self.r = m*0.2
- self.value = 1
- self.active = True
- self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill='orange',width=0)
- self.update()
- def update(self):
- if self.active:
- x1 = self.x - self.r
- x2 = self.x + self.r
- y1 = self.y - self.r
- y2 = self.y + self.r
- px1 = pacman.x - pacman.r
- px2 = pacman.x + pacman.r
- py1 = pacman.y - pacman.r
- py2 = pacman.y + pacman.r
- if rectInRect(x1,y1,x2,y2,px1,py1,px2,py2):
- pacman.pickup_key(self)
- root.after(30,self.update)
- def kill(self):
- canv.delete(self.id)
- self.active = False
- class Coin():
- def __init__(self,x,y):
- self.x = x+m*0.5
- self.y = y+m*0.5
- self.r = m*0.2
- self.value = 1
- self.active = True
- #self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill='yellow',width=0)
- self.update()
- self.imgs = []
- for i in range(1,2):
- tmp_img = Image.open('coin'+str(i)+'.png').resize((20,20))
- self.imgs.append(ImageTk.PhotoImage(image=tmp_img))
- self.id = canv.create_image(self.x,self.y, image = self.imgs[0])
- self.animationFrame = 0
- self.animationStep = 1
- self.animation()
- def animation(self):
- self.animationFrame += 1
- if self.animationFrame == len(self.imgs):
- self.animationFrame = 0
- canv.itemconfig(self.id,image=self.imgs[self.animationFrame])
- def update(self):
- if self.active:
- x1 = self.x - self.r
- x2 = self.x + self.r
- y1 = self.y - self.r
- y2 = self.y + self.r
- px1 = pacman.x - pacman.r
- px2 = pacman.x + pacman.r
- py1 = pacman.y - pacman.r
- py2 = pacman.y + pacman.r
- if rectInRect(x1,y1,x2,y2,px1,py1,px2,py2):
- pacman.pickup_coin(self)
- root.after(30,self.update)
- def kill(self):
- canv.delete(self.id)
- self.active = False
- def keyDown(event):
- if event.keycode == 37:
- if pacman.vx == v:
- pacman.command = 'stop'
- else:
- pacman.command = 'left'
- if event.keycode == 39:
- if pacman.vx == -v:
- pacman.command = 'stop'
- else:
- pacman.command = 'right'
- if event.keycode == 38:
- if pacman.vy == v:
- pacman.command = 'stop'
- else:
- pacman.command = 'up'
- if event.keycode == 40:
- if pacman.vy == -v:
- pacman.command = 'stop'
- else:
- pacman.command = 'down'
- f = open('map1.txt','r')
- x0 = padding
- y0 = padding
- x = x0
- y = y0
- a = []
- pacman = Pacman(-10,-10)
- enemies = []
- teleports = []
- controlPoints = []
- for line in f.readlines():
- a.append([])
- for z in line.strip():
- canv.create_rectangle(x,y,x+m,y+m)
- if z == '1' or z == 'd':
- a[-1].append(z)
- else:
- a[-1].append('0')
- if z == '1':
- canv.create_rectangle(x,y,x+m,y+m,fill='gray')
- elif z == 'p':
- pacman.x = x+m*0.5
- pacman.y = y+m*0.5
- elif z == 'c':
- Coin(x,y)
- elif z == 'e':
- enemies.append(Enemy(x,y,'red'))
- elif z == 't':
- teleports.append(Teleport(x,y))
- elif z == 'x':
- controlPoints.append(ControlPoint(x,y))
- elif z == 'k':
- key = Key(x,y)
- print(key)
- x += m
- y += m
- x = x0
- print('end of read file')
- def click(event):
- #print(pacman.m)
- path = enemies[0].create_path(pacman.m)
- for r,c in path:
- x = x0 + c*m + m/2
- y = y0 + r*m + m/2
- d = 4
- canv.create_rectangle(x-d,y-d,x+d,y+d,fill='orange')
- f.close()
- canv.tag_raise(pacman.id)
- root.bind('<Key>',keyDown)
- root.bind('<1>',click)
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement