Advertisement
Guest User

Untitled

a guest
Sep 16th, 2012
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.49 KB | None | 0 0
  1. import pygame, sys
  2. from pygame.locals import *
  3. import time
  4. import random
  5.  
  6. pygame.init()
  7.  
  8. # set up the window
  9. DISPLAYSURF = pygame.display.set_mode((500, 500), 0, 32)
  10. pygame.display.set_caption('EVOLUTION GAME BIIIIIITCH')
  11.  
  12. # set up the colors
  13. BLACK = (  0,   0,   0)
  14. WHITE = (255, 255, 255)
  15. RED   = (255,   0,   0)
  16. GREEN = (  0, 255,   0)
  17. BLUE  = (  0,   0, 255)
  18.  
  19. # draw on the surface object
  20. DISPLAYSURF.fill(BLACK)
  21. robot_list=[]
  22. plant_list= []
  23. FPS=23
  24. fpsClock = pygame.time.Clock()
  25.  
  26. list_of_plantsx=[]
  27. list_of_plantsy = []
  28.  
  29.  
  30. class trevor(object):
  31.     def __init__(self):
  32.        self.x = random.randrange(1,500)
  33.        self.x2 = self.x + 6
  34.        self.y = random.randrange(1,500)
  35.        self.y2 = self.y + 6
  36.        self.tcentrex=self.x +3
  37.        self.tcentrey= self.y + 3
  38.        self.movevalue=random.randrange(1,50)
  39.        self.counter = 0
  40.        self.direction=0
  41.        self.nearest_dict={}
  42.        self.energy= 10000
  43.        self.dead= False
  44.  
  45.     def dead_checker(self):
  46.       if self.energy <=0:
  47.         self.dead = True
  48.       else:
  49.           self.dead =False
  50.  
  51.     def distance(self,listx,listy):
  52.       for self.x in listx:
  53.         self.number=listx.index(self.x)
  54.         self.y = listy[self.number]
  55.         self.distance=(((self.x-self.tcentrex)**2) + ((self.y-self.tcentrey)**2))**0.5
  56.         self.nearest_dict[self.distance] = (self.x,self.y)
  57.  
  58.  
  59.  
  60.  
  61.     def which_direction(self):
  62.       list_sorted= sorted(self.nearest_dict)
  63.      
  64.       fx = self.nearest_dict[list_sorted[0]]
  65.       fy = self.nearest_dict[list_sorted[1]]
  66.       if fx > self.x:
  67.         self.direction = "down"
  68.       elif fx < self.x:
  69.         self.direction = "up"
  70.       elif fy > self.y:
  71.         self.direction = "right"
  72.       elif fy > self.y:
  73.         self.direction ="left"
  74.  
  75.  
  76.     def move(self):
  77.  
  78.       if self.dead == False:
  79.  
  80.  
  81.         if self.direction == "up":
  82.           self.y -= self.movevalue
  83.           self.y2 -= self.movevalue
  84.           self.energy -= self.movevalue**2
  85.           if self.y <= 5:
  86.             self.direction = "down"
  87.          
  88.                
  89.         if self.direction == "down":
  90.           self.y += self.movevalue
  91.           self.y2 += self.movevalue
  92.           self.energy -= self.movevalue**2
  93.           if self.y >= 495:
  94.             self.direction = "up"
  95.  
  96.        
  97.  
  98.         if self.direction == "right":
  99.           self.x += self.movevalue
  100.           self.x2 += self.movevalue
  101.           self.energy -= self.movevalue**2
  102.           if self.x >= 495:
  103.             self.direction = "left"
  104.        
  105.  
  106.         if self.direction == "left":
  107.           self.x -= self.movevalue
  108.           self.x2 -= self.movevalue
  109.           self.energy -= self.movevalue**2
  110.           if self.x <= 5:
  111.             self.direction = "right"
  112.       else:
  113.           self.x -= 0
  114.           self.x2 -= 0
  115.           self.y -= 0
  116.           self.y2 -=0
  117.      
  118.        
  119.     def draw(self, surface, colour):
  120.         pygame.draw.polygon(surface, colour, ((self.x, self.y), (self.x2, self.y), (self.x2, self.y2), (self.x, self.y2)))
  121.     def undraw(self, surface,back_colour):
  122.         pygame.draw.polygon(surface, back_colour, ((self.x, self.y), (self.x2, self.y), (self.x2, self.y2), (self.x, self.y2)))
  123.    
  124.  
  125.  
  126. class plant(object):
  127.     def __init__(self):
  128.        self.x = random.randrange(1,500)
  129.        self.x2 = self.x + 6
  130.        self.y = random.randrange(1,500)
  131.        self.y2 = self.y + 6
  132.        self.centrex = self.x+3
  133.        self.centrey = self.y +3
  134.        self.value=random.randrange(1,500)
  135.        self.counter=0
  136.        self.ON=True
  137.        
  138.  
  139.  
  140.     def draw(self, surface, colour):
  141.         pygame.draw.polygon(surface, colour, ((self.x, self.y), (self.x2, self.y), (self.x2, self.y2), (self.x, self.y2)))
  142.  
  143.     def flash(self):
  144.       value=self.value
  145.       return value
  146.     def counter_maker(self):
  147.       counter= self.counter
  148.       return counter
  149.     def flashy(self,colour1,colour2):
  150.       if self.counter<self.value:
  151.         self.draw(DISPLAYSURF,colour1)
  152.         self.counter +=1
  153.         self.ON = True
  154.         pygame.display.update()
  155.       elif self.counter== self.value:
  156.         self.draw(DISPLAYSURF,colour2)
  157.         self.counter +=1
  158.         pygame.display.update()
  159.         self.ON = False
  160.       elif self.value<p.counter and self.counter<self.value*2:
  161.         self.draw(DISPLAYSURF,colour2)
  162.         self.counter +=1
  163.         pygame.display.update()
  164.         self.ON = False
  165.       elif self.counter == self.value*2:
  166.         self.counter = 0
  167.         pygame.display.update()
  168.  
  169.     def add_to_list(self):
  170.       if self.ON == True:
  171.         list_of_plantsx.append(self.centrex)
  172.         list_of_plantsy.append(self.centrey)
  173.  
  174.  
  175.  
  176.  
  177. for _ in xrange(7):
  178.     plant_list.append(plant())
  179.    
  180.  
  181. for _ in xrange(60):
  182.     robot_list.append(trevor())
  183.  
  184. for p in plant_list:
  185.     p.counter =p.counter_maker()
  186.     p.value=p.flash()
  187.  
  188.  
  189. # run the game loop
  190. while True:
  191.     for event in pygame.event.get():
  192.         if event.type == QUIT:
  193.             pygame.quit()
  194.             sys.exit()
  195.     list_of_plantsy=[]
  196.     list_of_plantsx =[]
  197.     for p in plant_list:
  198.         p.flashy(RED,BLUE)
  199.         p.add_to_list()
  200.        
  201.  
  202.     for r in robot_list:
  203.         r.undraw(DISPLAYSURF, BLACK)
  204.         r.dead_checker()
  205.         r.distance(list_of_plantsx,list_of_plantsy)
  206.         r.which_direction()
  207.         r.move()
  208.         r.draw(DISPLAYSURF,WHITE)
  209.     print list_of_plantsx
  210.     print list_of_plantsy
  211.  
  212.  
  213.  
  214.  
  215.  
  216.          
  217.            
  218.     fpsClock.tick(FPS)
  219.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement