Advertisement
boris-vlasenko

Pacman

Mar 13th, 2018
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.85 KB | None | 0 0
  1. from tkinter import *
  2. from random import randrange as rnd, choice
  3. from PIL import ImageTk, Image
  4. import os, sys
  5. import time
  6.  
  7. root = Tk()
  8. width = 900
  9. hight = 600
  10. padding = 50
  11. root.geometry(str(width)+'x'+str(hight)+'+100+100')
  12. canv = Canvas(bg='lightblue')
  13. canv.pack(fill=BOTH,expand=1)
  14.  
  15. m = 30
  16. v = 4
  17.  
  18. def pointInSegment(x,x1,x2):
  19.     return x1 <= x <= x2
  20.  
  21. def segmentInSegment(x1,x2,xx1,xx2):
  22.     return pointInSegment(x1,xx1,xx2) or pointInSegment(x2,xx1,xx2) or \
  23.     pointInSegment(xx1,x1,x2) or pointInSegment(xx2,x1,x2)
  24.  
  25. def rectInRect(x1,y1,x2,y2,xx1,yy1,xx2,yy2):
  26.     if segmentInSegment(x1,x2,xx1,xx2) and segmentInSegment(y1,y2,yy1,yy2):
  27.         return True
  28.     if segmentInSegment(xx1,xx2,x1,x2) and segmentInSegment(yy1,yy2,y1,y2):
  29.         return True
  30.     return False
  31.  
  32. class Object():
  33.     def __init__(self,x,y,color):
  34.         self.x = x+m*0.5
  35.         self.y = y+m*0.5
  36.         self.rr = r = int(self.y - y0)//m #row
  37.         self.cc = c = int(self.x - x0)//m #column
  38.         self.r = m*0.4
  39.         self.vx = 0
  40.         self.vy = 0
  41.         self.key = False
  42.         self.m = None
  43.         self.color = color
  44.         self.command = 'stop'
  45.         self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill=self.color)
  46.         self.update()
  47.        
  48.        
  49.     def create_wave(self):
  50.         self.m = [[0 for c in range(len(a[0]))] for r in range(len(a))]
  51.         m = self.m
  52.         r = self.rr
  53.         c = self.cc
  54.         k = 1
  55.         m[r][c] = k
  56.         k += 1
  57.        
  58.         q = [(r,c)]
  59.         while q:
  60.             k += 1
  61.             qq = []
  62.             for r,c in q:
  63.                 for dr, dc in ((-1,0),(1,0),(0,-1),(0,1)):
  64.                     if a[r+dr][c+dc] == '0' and m[r+dr][c+dc] == 0:
  65.                         m[r+dr][c+dc] = k
  66.                         qq.append((r+dr,c+dc))
  67.             q = qq
  68.        
  69.         root.after(300,self.create_wave)
  70.                
  71.        
  72.     def update(self):
  73.         self.rr = r = int(self.y - y0)//m #row
  74.         self.cc = c = int(self.x - x0)//m #column
  75.        
  76.         xc = x0 + c*m +m//2
  77.         yc = y0 + r*m +m//2
  78.         if abs(self.x - xc) <= abs(self.vx) and  abs(self.y - yc) <= abs(self.vy):
  79.             #print(self.command)
  80.             if self.vx > 0:
  81.                 if a[r][c+1] != '0':
  82.                     if not(a[r][c+1] ==  'd' and self.key):
  83.                         self.command = 'stop'
  84.             elif self.vx < 0:
  85.                 if a[r][c-1] != '0':
  86.                     if not(a[r][c-1] ==  'd' and self.key):
  87.                         self.command = 'stop'
  88.             elif self.vy < 0:
  89.                 if a[r-1][c] != '0':
  90.                     if not(a[r-1][c] ==  'd' and self.key):
  91.                         self.command = 'stop'
  92.             elif self.vy > 0:
  93.                 if a[r+1][c] != '0':
  94.                     if not(a[r+1][c] ==  'd' and self.key):
  95.                         self.command = 'stop'              
  96.             if self.command == 'stop':
  97.                 self.vx = 0
  98.                 self.vy = 0
  99.                 self.y = yc
  100.                 self.x = xc
  101.             elif self.command == 'left':
  102.                 if a[r][c-1] == '0' or (a[r][c-1] ==  'd' and self.key):
  103.                     self.y = yc
  104.                     self.vx = -self.v
  105.                     self.vy = 0
  106.                     self.command = ''
  107.             elif self.command == 'right':
  108.                 if a[r][c+1] == '0'  or (a[r][c+1] ==  'd' and self.key):
  109.                     self.y = yc
  110.                     self.vx = self.v
  111.                     self.vy = 0
  112.                     self.command = ''
  113.             elif self.command == 'down':
  114.                 if a[r+1][c] == '0' or (a[r+1][c] ==  'd' and self.key):
  115.                     self.x = xc
  116.                     self.vx = 0
  117.                     self.vy = self.v
  118.                     self.command = ''
  119.             elif self.command == 'up':
  120.                 if a[r-1][c] == '0'  or (a[r-1][c] ==  'd' and self.key):
  121.                     self.x = xc
  122.                     self.vx = 0
  123.                     self.vy = -self.v
  124.                     self.command = ''
  125.             ##print(self.points)
  126.        
  127.         self.x += self.vx
  128.         self.y += self.vy
  129.         canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
  130.         root.after(30,self.update)
  131.        
  132. class ControlPoint(Object):
  133.     def __init__(self,x,y):
  134.         super().__init__(x,y,'cyan')
  135.         root.after(1000,self.create_wave)
  136.    
  137.    
  138.        
  139.  
  140. class Teleport():
  141.     def __init__(self,x,y):
  142.         self.x = x+m*0.5
  143.         self.y = y+m*0.5
  144.         self.r = m*0.4
  145.         self.color = 'black'
  146.         self.active = True
  147.         self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill=self.color)
  148.         self.update()
  149.    
  150.     def update(self):  
  151.         x1 = self.x - self.r
  152.         x2 = self.x + self.r
  153.         y1 = self.y - self.r
  154.         y2 = self.y + self.r
  155.  
  156.         px1 = pacman.x - pacman.r
  157.         px2 = pacman.x + pacman.r
  158.         py1 = pacman.y - pacman.r
  159.         py2 = pacman.y + pacman.r
  160.  
  161.         if self.active:
  162.             if rectInRect(x1,y1,x2,y2,px1,py1,px2,py2):
  163.                 pacman.teleport(self)
  164.         else:
  165.             if not rectInRect(x1,y1,x2,y2,px1,py1,px2,py2):
  166.                 self.active = True
  167.         root.after(30,self.update)
  168.            
  169.  
  170. class Enemy(Object):
  171.     def __init__(self,x,y,color):
  172.         self.mode = 'controlPoints'
  173.         self.modeN = 1
  174.         self.path = None
  175.         super().__init__(x,y,color)
  176.         self.v = 2
  177.         root.after(100,self.auto)
  178.        
  179.     def isfree(self,dr,dc):
  180.         return a[self.rr+dr][self.cc+dc] != '0'
  181.        
  182.     def create_path(self,m):
  183.         tr = self.rr
  184.         tc = self.cc
  185.         res = []
  186.         while m[tr][tc] > 1:
  187.             min_m = m[tr][tc]
  188.             r_m = tr
  189.             c_m = tc
  190.             for dr, dc in ((-1,0),(1,0),(0,-1),(0,1)):
  191.                 if  m[tr+dr][tc+dc] < min_m and m[tr+dr][tc+dc] != 0:
  192.                     min_m = m[tr+dr][tc+dc]
  193.                     r_m = tr+dr
  194.                     c_m = tc+dc
  195.             res.append((r_m,c_m))
  196.             tr = r_m
  197.             tc = c_m
  198.         return res
  199.        
  200.     def auto(self):
  201.         if pacman.m:
  202.             path = self.create_path(pacman.m)
  203.             if len(path) < 5:
  204.                 self.mode = 'pacman'
  205.                 self.v = 5
  206.             else:
  207.                 self.mode = 'controlPoints'
  208.                 self.v = 2
  209.                
  210.         if self.mode == 'controlPoints':
  211.             self.v = 2
  212.             if self.rr == controlPoints[self.modeN].rr and self.cc == controlPoints[self.modeN].cc:
  213.                 self.modeN = rnd(len(controlPoints))
  214.                
  215.         if self.mode == 'pacman':
  216.             if pacman.m:
  217.                 self.path = self.create_path(pacman.m)
  218.         else:
  219.             if controlPoints[self.modeN].m:
  220.                 self.path = self.create_path(controlPoints[self.modeN].m)
  221.         if self.path:
  222.             tr,tc = self.path[0]
  223.            
  224.             if tr > self.rr:
  225.                 self.command = 'down'
  226.             if tr < self.rr:
  227.                 self.command = 'up'
  228.             if tc > self.cc:
  229.                 self.command = 'right'
  230.             if tc < self.cc:
  231.                 self.command = 'left'
  232.            
  233.        
  234.         root.after(100,self.auto)
  235.        
  236. class Pacman(Object):
  237.     def __init__(self,x,y):
  238.         self.points = 0
  239.         self.v = 3
  240.         self.info = canv.create_text(30,20,font='Arial 20', text = self.points)
  241.         self.info_key = canv.create_text(100,20,font='Arial 20', text = '')
  242.         super().__init__(x,y,'green')
  243.        
  244.         root.after(300,self.create_wave)
  245.        
  246.     def pickup_coin(self,coin):
  247.         self.points += coin.value
  248.         canv.itemconfig(self.info,text = self.points)
  249.         coin.kill()
  250.        
  251.     def teleport(self,port):
  252.         tt = teleports.copy()
  253.         tt.remove(port)
  254.         there = choice(tt)
  255.         there.active = False
  256.         self.x = there.x
  257.         self.y = there.y
  258.        
  259.     def pickup_key(self,coin):
  260.         self.key = True
  261.         canv.itemconfig(self.info_key,text = 'KEY')
  262.         key.kill() 
  263.        
  264. class Key():   
  265.     def __init__(self,x,y):
  266.         self.x = x+m*0.5
  267.         self.y = y+m*0.5
  268.         self.r = m*0.2
  269.         self.value = 1
  270.         self.active = True
  271.         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)
  272.         self.update()
  273.    
  274.     def update(self):  
  275.         if self.active:
  276.             x1 = self.x - self.r
  277.             x2 = self.x + self.r
  278.             y1 = self.y - self.r
  279.             y2 = self.y + self.r
  280.  
  281.             px1 = pacman.x - pacman.r
  282.             px2 = pacman.x + pacman.r
  283.             py1 = pacman.y - pacman.r
  284.             py2 = pacman.y + pacman.r
  285.  
  286.             if rectInRect(x1,y1,x2,y2,px1,py1,px2,py2):
  287.                 pacman.pickup_key(self)
  288.             root.after(30,self.update) 
  289.        
  290.     def kill(self):
  291.         canv.delete(self.id)
  292.         self.active = False
  293.        
  294.        
  295. class Coin():
  296.     def __init__(self,x,y):
  297.         self.x = x+m*0.5
  298.         self.y = y+m*0.5
  299.         self.r = m*0.2
  300.         self.value = 1
  301.         self.active = True
  302.         #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)
  303.         self.update()
  304.         self.imgs = []
  305.         for i in range(1,2):
  306.             tmp_img = Image.open('coin'+str(i)+'.png').resize((20,20))
  307.             self.imgs.append(ImageTk.PhotoImage(image=tmp_img))
  308.         self.id = canv.create_image(self.x,self.y, image = self.imgs[0])
  309.         self.animationFrame = 0
  310.         self.animationStep = 1
  311.         self.animation()
  312.        
  313.     def animation(self):
  314.        
  315.         self.animationFrame += 1
  316.         if self.animationFrame == len(self.imgs):
  317.             self.animationFrame = 0
  318.            
  319.         canv.itemconfig(self.id,image=self.imgs[self.animationFrame])
  320.        
  321.     def update(self):  
  322.         if self.active:
  323.             x1 = self.x - self.r
  324.             x2 = self.x + self.r
  325.             y1 = self.y - self.r
  326.             y2 = self.y + self.r
  327.  
  328.             px1 = pacman.x - pacman.r
  329.             px2 = pacman.x + pacman.r
  330.             py1 = pacman.y - pacman.r
  331.             py2 = pacman.y + pacman.r
  332.  
  333.             if rectInRect(x1,y1,x2,y2,px1,py1,px2,py2):
  334.                 pacman.pickup_coin(self)
  335.             root.after(30,self.update)
  336.        
  337.     def kill(self):
  338.         canv.delete(self.id)
  339.         self.active = False
  340.  
  341. def keyDown(event):
  342.  
  343.     if event.keycode == 37:
  344.         if pacman.vx == v:
  345.             pacman.command = 'stop'
  346.         else:
  347.             pacman.command = 'left'
  348.        
  349.     if event.keycode == 39:
  350.         if pacman.vx == -v:
  351.             pacman.command = 'stop'
  352.         else:
  353.             pacman.command = 'right'
  354.        
  355.     if event.keycode == 38:
  356.         if pacman.vy == v:
  357.             pacman.command = 'stop'
  358.         else:
  359.             pacman.command = 'up'
  360.  
  361.     if event.keycode == 40:
  362.         if pacman.vy == -v:
  363.             pacman.command = 'stop'
  364.         else:
  365.             pacman.command = 'down'
  366.  
  367.  
  368.  
  369.  
  370. f = open('map1.txt','r')
  371. x0 = padding
  372. y0 = padding
  373. x = x0
  374. y = y0
  375. a = []
  376. pacman = Pacman(-10,-10)
  377. enemies = []
  378. teleports = []
  379. controlPoints = []
  380. for line in f.readlines():
  381.     a.append([])
  382.     for z in line.strip():
  383.         canv.create_rectangle(x,y,x+m,y+m)
  384.         if z == '1' or z == 'd':
  385.             a[-1].append(z)
  386.         else:
  387.             a[-1].append('0')
  388.                        
  389.         if z == '1':
  390.             canv.create_rectangle(x,y,x+m,y+m,fill='gray')
  391.         elif z == 'p':
  392.             pacman.x = x+m*0.5
  393.             pacman.y = y+m*0.5
  394.         elif z == 'c':
  395.             Coin(x,y)
  396.         elif z == 'e':
  397.             enemies.append(Enemy(x,y,'red'))
  398.         elif z == 't':
  399.             teleports.append(Teleport(x,y))
  400.         elif z == 'x':
  401.             controlPoints.append(ControlPoint(x,y))
  402.         elif z == 'k':
  403.             key = Key(x,y)
  404.             print(key)
  405.                
  406.         x += m
  407.     y += m
  408.     x = x0
  409. print('end of read file')
  410.        
  411.        
  412. def click(event):
  413.     #print(pacman.m)
  414.     path = enemies[0].create_path(pacman.m)
  415.     for r,c in path:
  416.         x = x0 + c*m + m/2
  417.         y = y0 + r*m + m/2
  418.         d = 4
  419.         canv.create_rectangle(x-d,y-d,x+d,y+d,fill='orange')
  420.    
  421.    
  422.    
  423.        
  424. f.close()
  425. canv.tag_raise(pacman.id)
  426. root.bind('<Key>',keyDown)
  427. root.bind('<1>',click)
  428. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement