Advertisement
xBoBox3

Untitled

Oct 13th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.24 KB | None | 0 0
  1. import pygame
  2. from pyeuclid import Vector2
  3. from math import sin,cos,pi
  4. from random import random
  5.  
  6. class Thing:
  7.     def __init__(self,pos):
  8.         self.pos = pos
  9.         things.append(self)
  10.     def update(self): pass
  11.     def draw(self,img): pass
  12.     def collide(self,who): pass
  13.  
  14. class DynamicThing(Thing):
  15.     def __init__(self,pos):
  16.         Thing.__init__(self,pos)
  17.         self.vel = Vector2(0,0)
  18.         self.lastPos = pos
  19.         self.col = (255,255,0)
  20.         self.r = 12
  21.         dynamic_things.append(self)
  22.     def update(self):
  23.         self.lastPos = self.pos
  24.         self.pos = self.pos + self.vel
  25.     def draw(self,img):
  26.         pygame.draw.circle(img, (0,0,0), [int(n) for n in self.pos], self.r, self.r)
  27.         pygame.draw.circle(img, self.col, [int(n) for n in self.pos], self.r-2, self.r-2)
  28.     def collide(self,obj):
  29.         Thing.collide(self,obj)
  30.         if isinstance(obj,Wall):
  31.             self.pos = self.lastPos
  32.  
  33. class Wall(Thing):
  34.     def draw(self,img):
  35.         x,y = self.pos.x, self.pos.y
  36.         pygame.draw.rect(img, (90,90,200), (x-16,y-16,32,32), 0)
  37.  
  38. class Pacman(DynamicThing):
  39.     def __init__(self):
  40.         DynamicThing.__init__(self,Vector2(32*9+16,32*12+16))
  41.         self.col = (255,255,0)
  42.     def update(self):
  43.         DynamicThing.update(self)
  44.         if (keyPressed[pygame.K_LEFT]): self.vel.x = -1
  45.         if (keyPressed[pygame.K_RIGHT]): self.vel.x = 1
  46.         if (keyPressed[pygame.K_DOWN]): self.vel.y = 1
  47.         if (keyPressed[pygame.K_UP]): self.vel.y = -1
  48.         if (self.vel.x==-1 and not keyPressed[pygame.K_LEFT]): self.vel.x = 0
  49.         if (self.vel.x==1 and not keyPressed[pygame.K_RIGHT]): self.vel.x = 0
  50.         if (self.vel.y==1 and not keyPressed[pygame.K_DOWN]): self.vel.y = 0
  51.         if (self.vel.y==-1 and not keyPressed[pygame.K_UP]): self.vel.y = 0
  52.     def collide(self,obj):
  53.         DynamicThing.collide(self,obj)
  54.         if isinstance(obj,Ghost):
  55.             self.pos = Vector2(32*9+16,32*12+16)
  56.  
  57. class Ghost(DynamicThing):
  58.     def __init__(self):
  59.         DynamicThing.__init__(self,Vector2(32*9+16,32*10+16))
  60.         self.col = (int(random()*255),int(random()*255),int(random()*255))
  61.         self.vel = Vector2(0,-2)
  62.     def update(self):
  63.         DynamicThing.update(self)
  64.         if random()<0.01:
  65.             self.vel = [Vector2(2,0),Vector2(-2,0),Vector2(0,2),Vector2(0,-2)][int(random()*4)]
  66.     def collide(self,obj):
  67.         DynamicThing.collide(self,obj)
  68.         if isinstance(obj,Wall):
  69.             self.vel = [Vector2(2,0),Vector2(-2,0),Vector2(0,2),Vector2(0,-2)][int(random()*4)]
  70.  
  71. def thingAtPos(pos):
  72.     tile_pos = Vector2(int(pos.x/32),int(pos.y/32))
  73.     return map[tile_pos.y][tile_pos.x]
  74.  
  75. # initializate stuff
  76. pygame.init()
  77. clock = pygame.time.Clock()
  78. screen = pygame.display.set_mode([32*19,32*22])
  79. points_in_unit_circle_border = [Vector2(cos(float(a)/8*2*pi),sin(float(a)/8*2*pi)) for a in xrange(8)]
  80. things = []
  81. dynamic_things = []
  82. exit = False
  83.  
  84. map =  [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  85.         [1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1],
  86.         [1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1],
  87.         [1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1],
  88.         [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  89.         [1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1],
  90.         [1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1],
  91.         [1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1],
  92.         [1,1,1,1,0,1,0,0,0,0,0,0,0,1,0,1,1,1,1],
  93.         [1,1,1,1,0,1,0,1,1,0,1,1,0,1,0,1,1,1,1],
  94.         [1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1],
  95.         [1,1,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,1,1],
  96.         [1,1,1,1,0,1,0,0,0,0,0,0,0,1,0,1,1,1,1],
  97.         [1,1,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,1,1],
  98.         [1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1],
  99.         [1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1],
  100.         [1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1],
  101.         [1,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1],
  102.         [1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1],
  103.         [1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,0,0,1],
  104.         [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  105.         [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]
  106.  
  107.  
  108. #create pacman, walls, ghosts
  109. pacman = Pacman()
  110. for y in xrange(len(map)):
  111.     for x in xrange(len(map[y])):
  112.         if (map[y][x]==1):
  113.             map[y][x] = Wall(Vector2(x*32+16,y*32+16))
  114. for i in xrange(4):
  115.     Ghost()
  116.  
  117. while exit==False:
  118.     clock.tick(45)
  119.  
  120.     screen.fill([255,255,255])
  121.     keyPressed = pygame.key.get_pressed()
  122.  
  123.     # events
  124.     for event in pygame.event.get():
  125.         if event.type == pygame.QUIT:
  126.             exit = True
  127.         if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
  128.             exit = True
  129.  
  130.     # more ghosts
  131.     if random()<0.001: Ghost()
  132.  
  133.     # updates e draws
  134.     for thing in things:
  135.         thing.update()
  136.         thing.draw(screen)
  137.  
  138.     # collisions
  139.     for A in dynamic_things:
  140.         #dynamic vs dynamic
  141.         for B in dynamic_things:
  142.             if A!=B and abs(A.pos-B.pos)<(A.r+B.r):
  143.                 A.collide(B)
  144.                 B.collide(A)
  145.         #dynamic vs walls
  146.         for circle_point in points_in_unit_circle_border:
  147.             thing_in_a_border = thingAtPos(A.pos+circle_point*12)
  148.             if isinstance(thing_in_a_border,Wall):
  149.                 A.collide(thing_in_a_border)
  150.  
  151.     pygame.display.flip()
  152.  
  153. pygame.quit ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement