Advertisement
TitoLuyo

game

Apr 8th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.63 KB | None | 0 0
  1. import math
  2. import pygame
  3. from pygame import font
  4. #from random import randint
  5.  
  6. pygame.init()
  7. font.init()
  8. myfont = pygame.font.SysFont("monospace", 15,(0,0,0))
  9. window = pygame.display.set_mode((1080,720))
  10. pygame.display.set_caption("Hide And Seek!")
  11. clock = pygame.time.Clock()
  12. fps = 30
  13.  
  14. red     = (250,  0,  0)
  15. green   = (  0,255,  0)
  16. blue    = (  0,  0,255)
  17. yellow  = (255,255,  0)
  18. cyan    = (  0,255,255)
  19. magenta = (255,  0,255)
  20. black  = (  0,  0  ,0)
  21. white  = (255,255,255)
  22. colors = [red,green,blue, yellow, cyan, magenta]
  23.  
  24. playbuttonimg = pygame.image.load("playbuttonimg.png").convert_alpha()
  25. playbuttonpressimg = pygame.image.load("playbuttonpressimg.png").convert_alpha()
  26.  
  27. class player():
  28.         def __init__(self,x,y,state):
  29.                 self.x = x
  30.                 self.y = y
  31.                 self.state = state
  32.                 self.speed = 5
  33.                 if self.state == "hide":
  34.                         print "im hiding!"
  35.                 elif self.state == "seek":
  36.                         print "im seeking!"
  37.                        
  38.         def update(self,controller):
  39.                 self.direction = controller
  40.                
  41.                 if self.direction == "left":
  42.                         self.x -= self.speed
  43.                 if self.direction == "right":
  44.                         self.x += self.speed
  45.                 if self.direction == "down":
  46.                         self.y += self.speed
  47.                 if self.direction == "up":
  48.                         self.y -= self.speed
  49.         def fieldofview(self,obstacles):
  50.                 for item in obstacles:
  51.                         x = item[0]  # <-- x of the obstacles
  52.                         y = item[1]  # <-- y of the obstacles
  53.                         k = 300 # fixed distance
  54.                         dist = math.hypot(x-self.x, y-self.y)
  55.                         #print dist,
  56.                         if (dist<k):
  57.                             endx = int(self.x+(x-self.x)*k/dist)
  58.                             endy = int(self.y+(y-self.y)*k/dist)
  59.                         else:
  60.                             endx = x
  61.                             endy = y
  62.                        
  63.                         #endx = x+(x-self.x)
  64.                         #endy = y +(y-self.y)
  65.                         #pygame.draw.polygon(window,(black),((x,y),(x+25,y+25),(endx,endy+25),(endx,endy)))
  66.                        
  67.                         if self.x >= x and self.y >= y:
  68.                             points = ((endx,endy),(endx,endy+25),(x,y+25),(x+25,y+25),(x+25,y),(endx+25,endy))
  69.                             #print "C3",
  70.                         elif self.x <= x and self.y >= y:
  71.                             points = ((x,y),(x,y+25),(x+25,y+25),(endx+25,endy+25),(endx+25,endy),(endx,endy))
  72.                             #print "C4",
  73.                         elif self.x <= x and self.y <= y:
  74.                             points = ((x,y),(x,y+25),(endx,endy+25),(endx+25,endy+25),(endx+25,endy),(x+25,y))
  75.                             #print "C1",
  76.                         else: # elif self.x >= x and self.y <= y:
  77.                             points = ((x,y),(endx,endy),(endx,endy+25),(endx+25,endy+25),(x+25,y+25),(x+25,y))
  78.                             #print "C2",
  79.                         pygame.draw.polygon(window,(black),points)
  80.                         #for i in range(0, 6):
  81.                         #    pygame.draw.circle(window, (colors[i]), points[i], 3, 0)
  82.                 #print ""
  83.         def render(self):
  84.                 pygame.draw.rect(window,(blue),(self.x,self.y,25,25))
  85.                
  86. def game():
  87.         run = True
  88.         Player = player(100,100,"hide")
  89.         obstacles = [[250,250],[400,450]]
  90.         while run:
  91.                 lmb,rmb,mmb = pygame.mouse.get_pressed()
  92.                 keys = pygame.key.get_pressed()
  93.                 if keys[pygame.K_LEFT]:
  94.                         controller = "left"
  95.                 elif keys[pygame.K_RIGHT]:
  96.                         controller =  "right"
  97.                 elif keys[pygame.K_UP]:
  98.                         controller = "up"
  99.                 elif keys[pygame.K_DOWN]:
  100.                         controller = "down"
  101.                 else:
  102.                         controller = "none"
  103.                 for event in pygame.event.get():
  104.                         if event.type == pygame.QUIT:
  105.                                 run = False
  106.                 window.fill(white)
  107.                 mousex,mousey = pygame.mouse.get_pos()
  108.                
  109.                 Player.update(controller)
  110.                 for item in obstacles:
  111.                         x = item[0]
  112.                         y = item[1]
  113.                         pygame.draw.rect(window,(black),(x,y,25,25))
  114.                 Player.render()
  115.                 Player.fieldofview(obstacles)
  116.                
  117.                 clock.tick(fps)
  118.                 pygame.display.flip()
  119.                
  120.                
  121. def mainmenu():
  122.         run = True
  123.         while run:
  124.                 lmb,rmb,mmb = pygame.mouse.get_pressed()
  125.                 for event in pygame.event.get():
  126.                         pass
  127.                 window.fill(white)
  128.                 mousex,mousey = pygame.mouse.get_pos()
  129.                 window.blit(playbuttonimg,(50,150))
  130.                
  131.                 if 50<= mousex <= 250 and 150 <= mousey <=250:
  132.                         window.blit(playbuttonpressimg,(50,150))
  133.                         if lmb == True:
  134.                                 run = False
  135.                                 return "play"
  136.                
  137.                 clock.tick(fps)
  138.                 pygame.display.flip()
  139. button = mainmenu()
  140.  
  141. if button == "play":
  142.         game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement