Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys
- from pygame.locals import *
- import time
- import random
- pygame.init()
- # set up the window
- DISPLAYSURF = pygame.display.set_mode((500, 500), 0, 32)
- pygame.display.set_caption('EVOLUTION GAME BIIIIIITCH')
- # set up the colors
- BLACK = ( 0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- GREEN = ( 0, 255, 0)
- BLUE = ( 0, 0, 255)
- # draw on the surface object
- DISPLAYSURF.fill(BLACK)
- robot_list=[]
- plant_list= []
- FPS=23
- fpsClock = pygame.time.Clock()
- list_of_plantsx=[]
- list_of_plantsy = []
- class trevor(object):
- def __init__(self):
- self.x = random.randrange(1,500)
- self.x2 = self.x + 6
- self.y = random.randrange(1,500)
- self.y2 = self.y + 6
- self.tcentrex=self.x +3
- self.tcentrey= self.y + 3
- self.movevalue=random.randrange(1,50)
- self.counter = 0
- self.direction=0
- self.nearest_dict={}
- self.energy= 10000
- self.dead= False
- def dead_checker(self):
- if self.energy <=0:
- self.dead = True
- else:
- self.dead =False
- def distance(self,listx,listy):
- for self.x in listx:
- self.number=listx.index(self.x)
- self.y = listy[self.number]
- self.distance=(((self.x-self.tcentrex)**2) + ((self.y-self.tcentrey)**2))**0.5
- self.nearest_dict[self.distance] = (self.x,self.y)
- def which_direction(self):
- list_sorted= sorted(self.nearest_dict)
- fx = self.nearest_dict[list_sorted[0]]
- fy = self.nearest_dict[list_sorted[1]]
- if fx > self.x:
- self.direction = "down"
- elif fx < self.x:
- self.direction = "up"
- elif fy > self.y:
- self.direction = "right"
- elif fy > self.y:
- self.direction ="left"
- def move(self):
- if self.dead == False:
- if self.direction == "up":
- self.y -= self.movevalue
- self.y2 -= self.movevalue
- self.energy -= self.movevalue**2
- if self.y <= 5:
- self.direction = "down"
- if self.direction == "down":
- self.y += self.movevalue
- self.y2 += self.movevalue
- self.energy -= self.movevalue**2
- if self.y >= 495:
- self.direction = "up"
- if self.direction == "right":
- self.x += self.movevalue
- self.x2 += self.movevalue
- self.energy -= self.movevalue**2
- if self.x >= 495:
- self.direction = "left"
- if self.direction == "left":
- self.x -= self.movevalue
- self.x2 -= self.movevalue
- self.energy -= self.movevalue**2
- if self.x <= 5:
- self.direction = "right"
- else:
- self.x -= 0
- self.x2 -= 0
- self.y -= 0
- self.y2 -=0
- def draw(self, surface, colour):
- pygame.draw.polygon(surface, colour, ((self.x, self.y), (self.x2, self.y), (self.x2, self.y2), (self.x, self.y2)))
- def undraw(self, surface,back_colour):
- pygame.draw.polygon(surface, back_colour, ((self.x, self.y), (self.x2, self.y), (self.x2, self.y2), (self.x, self.y2)))
- class plant(object):
- def __init__(self):
- self.x = random.randrange(1,500)
- self.x2 = self.x + 6
- self.y = random.randrange(1,500)
- self.y2 = self.y + 6
- self.centrex = self.x+3
- self.centrey = self.y +3
- self.value=random.randrange(1,500)
- self.counter=0
- self.ON=True
- def draw(self, surface, colour):
- pygame.draw.polygon(surface, colour, ((self.x, self.y), (self.x2, self.y), (self.x2, self.y2), (self.x, self.y2)))
- def flash(self):
- value=self.value
- return value
- def counter_maker(self):
- counter= self.counter
- return counter
- def flashy(self,colour1,colour2):
- if self.counter<self.value:
- self.draw(DISPLAYSURF,colour1)
- self.counter +=1
- self.ON = True
- pygame.display.update()
- elif self.counter== self.value:
- self.draw(DISPLAYSURF,colour2)
- self.counter +=1
- pygame.display.update()
- self.ON = False
- elif self.value<p.counter and self.counter<self.value*2:
- self.draw(DISPLAYSURF,colour2)
- self.counter +=1
- pygame.display.update()
- self.ON = False
- elif self.counter == self.value*2:
- self.counter = 0
- pygame.display.update()
- def add_to_list(self):
- if self.ON == True:
- list_of_plantsx.append(self.centrex)
- list_of_plantsy.append(self.centrey)
- for _ in xrange(7):
- plant_list.append(plant())
- for _ in xrange(60):
- robot_list.append(trevor())
- for p in plant_list:
- p.counter =p.counter_maker()
- p.value=p.flash()
- # run the game loop
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- list_of_plantsy=[]
- list_of_plantsx =[]
- for p in plant_list:
- p.flashy(RED,BLUE)
- p.add_to_list()
- for r in robot_list:
- r.undraw(DISPLAYSURF, BLACK)
- r.dead_checker()
- r.distance(list_of_plantsx,list_of_plantsy)
- r.which_direction()
- r.move()
- r.draw(DISPLAYSURF,WHITE)
- print list_of_plantsx
- print list_of_plantsy
- fpsClock.tick(FPS)
- pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement