def random_object_generator(self, nr): self.objects = [] self.obstacles = [] self.Glist = self.create_grid() rand_obst = randint(1, 3) while rand_obst != 0: rand_dimension = randint(15, 60) i = randint(0, len(self.Glist) - 1) pos = list(self.Glist)[i] rand_x = pos[0] rand_y = pos[1] #checking if the position is available if (((rand_x - 30, rand_y - 30) in self.Glist) and ((rand_x - 30, rand_y) in self.Glist) and ((rand_x, rand_y - 30) in self.Glist) and (rand_dimension < 30) and (self.Glist[list(self.Glist)[i]] == True) and (self.Glist[rand_x - 30, rand_y - 30] == True) and (self.Glist[rand_x - 30, rand_y] == True) and (self.Glist[rand_x, rand_y - 30] == True)): ob4 = Circle(rand_x, rand_y, rand_dimension, self.screen, self.yellow) self.obstacles.append(ob4) self.Glist = self.occupy_grid(rand_x, rand_y) self.Glist = self.occupy_grid(rand_x - 30, rand_y - 30) self.Glist = self.occupy_grid(rand_x - 30, rand_y) self.Glist = self.occupy_grid(rand_x, rand_y - 30) rand_obst = rand_obst - 1 elif (((rand_x - 30, rand_y - 30) in self.Glist) and ((rand_x - 30, rand_y) in self.Glist) and ((rand_x, rand_y - 30) in self.Glist) and ((rand_x + 30, rand_y - 30) in self.Glist) and ((rand_x + 30, rand_y) in self.Glist) and ((rand_x, rand_y + 30) in self.Glist) and ((rand_x - 30, rand_y + 30) in self.Glist) and ((rand_x - 60, rand_y) in self.Glist) and ((rand_x - 60, rand_y - 30) in self.Glist) and ((rand_x, rand_y - 60) in self.Glist) and ((rand_x - 30, rand_y - 60) in self.Glist) and (rand_dimension > 30) and (rand_dimension <= 44) and (self.Glist[list(self.Glist)[i]] == True) and (self.Glist[rand_x - 30, rand_y - 30] == True) and (self.Glist[rand_x - 30, rand_y] == True) and (self.Glist[rand_x, rand_y - 30] == True) and (self.Glist[rand_x + 30, rand_y - 30] == True) and (self.Glist[rand_x + 30, rand_y] == True) and (self.Glist[rand_x, rand_y + 30] == True) and (self.Glist[rand_x - 30, rand_y + 30] == True) and (self.Glist[rand_x - 60, rand_y] == True) and (self.Glist[rand_x - 60, rand_y - 30] == True) and (self.Glist[rand_x, rand_y - 60] == True) and (self.Glist[rand_x - 30, rand_y - 60] == True)): ob4 = Circle(rand_x, rand_y, rand_dimension, self.screen, self.yellow) self.Glist = self.occupy_grid(rand_x, rand_y) self.Glist = self.occupy_grid(rand_x - 30, rand_y - 30) self.Glist = self.occupy_grid(rand_x - 30, rand_y) self.Glist = self.occupy_grid(rand_x, rand_y - 30) self.Glist = self.occupy_grid(rand_x + 30, rand_y - 30) self.Glist = self.occupy_grid(rand_x + 30, rand_y) self.Glist = self.occupy_grid(rand_x, rand_y + 30) self.Glist = self.occupy_grid(rand_x - 30, rand_y + 30) self.Glist = self.occupy_grid(rand_x - 60, rand_y) self.Glist = self.occupy_grid(rand_x - 60, rand_y - 30) self.Glist = self.occupy_grid(rand_x, rand_y - 60) self.Glist = self.occupy_grid(rand_x - 30, rand_y - 60) rand_obst = rand_obst - 1 self.obstacles.append(ob4) elif (((rand_x - 30, rand_y - 30) in self.Glist) and ((rand_x - 30, rand_y) in self.Glist) and ((rand_x, rand_y - 30) in self.Glist) and ((rand_x + 30, rand_y - 30) in self.Glist) and ((rand_x + 30, rand_y) in self.Glist) and ((rand_x, rand_y + 30) in self.Glist) and ((rand_x - 30, rand_y + 30) in self.Glist) and ((rand_x - 60, rand_y) in self.Glist) and ((rand_x - 60, rand_y - 30) in self.Glist) and ((rand_x, rand_y - 60) in self.Glist) and ((rand_x - 30, rand_y - 60) in self.Glist) and ((rand_x - 60, rand_y - 60) in self.Glist) and ((rand_x + 30, rand_y - 60) in self.Glist) and ((rand_x + 30, rand_y + 30) in self.Glist) and ((rand_x - 60, rand_y + 30) in self.Glist)) and ( (rand_dimension > 44) and (self.Glist[list(self.Glist)[i]] == True) and (self.Glist[rand_x - 30, rand_y - 30] == True) and (self.Glist[rand_x - 30, rand_y] == True) and (self.Glist[rand_x, rand_y - 30] == True) and (self.Glist[rand_x + 30, rand_y - 30] == True) and (self.Glist[rand_x + 30, rand_y] == True) and (self.Glist[rand_x, rand_y + 30] == True) and (self.Glist[rand_x - 30, rand_y + 30] == True) and (self.Glist[rand_x - 60, rand_y] == True) and (self.Glist[rand_x - 60, rand_y - 30] == True) and (self.Glist[rand_x, rand_y - 60] == True) and (self.Glist[rand_x - 30, rand_y - 60] == True) and (self.Glist[rand_x - 60, rand_y - 60] == True) and (self.Glist[rand_x + 30, rand_y - 60] == True) and (self.Glist[rand_x + 30, rand_y + 30] == True) and (self.Glist[rand_x - 60, rand_y + 30] == True)): ob4 = Circle(rand_x, rand_y, rand_dimension, self.screen, self.yellow) self.Glist = self.occupy_grid(rand_x, rand_y) self.Glist = self.occupy_grid(rand_x - 30, rand_y - 30) self.Glist = self.occupy_grid(rand_x - 30, rand_y) self.Glist = self.occupy_grid(rand_x, rand_y - 30) self.Glist = self.occupy_grid(rand_x + 30, rand_y - 30) self.Glist = self.occupy_grid(rand_x + 30, rand_y) self.Glist = self.occupy_grid(rand_x, rand_y + 30) self.Glist = self.occupy_grid(rand_x - 30, rand_y + 30) self.Glist = self.occupy_grid(rand_x - 60, rand_y) self.Glist = self.occupy_grid(rand_x - 60, rand_y - 30) self.Glist = self.occupy_grid(rand_x, rand_y - 60) self.Glist = self.occupy_grid(rand_x - 30, rand_y - 60) self.Glist = self.occupy_grid(rand_x - 60, rand_y - 60) self.Glist = self.occupy_grid(rand_x + 30, rand_y - 60) self.Glist = self.occupy_grid(rand_x + 30, rand_y + 30) self.Glist = self.occupy_grid(rand_x - 60, rand_y + 30) rand_obst = rand_obst - 1 self.obstacles.append(ob4) while nr != 0: rand_dimension = randint(15, 60) rand_shape = randint(1, 3) rand_colour = randint(1, 3) colours = {1: self.red, 2: self.blue, 3: self.green} j = randint(0, len(self.Glist) - 1) pos = list(self.Glist)[j] rand_x = pos[0] rand_y = pos[1] if rand_shape == 1: if ((rand_dimension < 30) and (self.Glist[rand_x, rand_y] == True)): ob1 = Square(rand_x, rand_y, 1, rand_dimension, self.screen, colours[rand_colour]) self.objects.append(ob1) self.Glist = self.occupy_grid(rand_x, rand_y) nr = nr - 1 elif (((rand_x + 30, rand_y) in self.Glist) and ((rand_x + 30, rand_y + 30) in self.Glist) and ((rand_x, rand_y + 30) in self.Glist)) and ( (rand_dimension > 30) and (self.Glist[rand_x, rand_y] == True) and (self.Glist[rand_x + 30, rand_y] == True) and (self.Glist[rand_x + 30, rand_y + 30] == True) and (self.Glist[rand_x, rand_y + 30] == True)): self.Glist = self.occupy_grid(rand_x + 30, rand_y) self.Glist = self.occupy_grid(rand_x + 30, rand_y + 30) self.Glist = self.occupy_grid(rand_x, rand_y + 30) ob1 = Square(rand_x, rand_y, 1, rand_dimension, self.screen, colours[rand_colour]) self.objects.append(ob1) nr = nr - 1 elif rand_shape == 2: if ((rand_dimension < 30) and (self.Glist[rand_x, rand_y] == True)): ob2 = Rectangle(rand_x, rand_y, 1, rand_dimension, self.screen, colours[rand_colour]) self.objects.append(ob2) nr = nr - 1 self.Glist = self.occupy_grid(rand_x, rand_y) elif (((rand_x + 30, rand_y) in self.Glist) and ((rand_x + 30, rand_y + 30) in self.Glist) and ((rand_x, rand_y + 30) in self.Glist)) and ( (rand_dimension > 30) and (rand_dimension <= 45) and (self.Glist[rand_x, rand_y] == True) and (self.Glist[rand_x + 30, rand_y] == True) and (self.Glist[rand_x + 30, rand_y + 30] == True) and (self.Glist[rand_x, rand_y + 30] == True)): self.Glist = self.occupy_grid(rand_x + 30, rand_y) self.Glist = self.occupy_grid(rand_x + 30, rand_y + 30) self.Glist = self.occupy_grid(rand_x, rand_y + 30) ob2 = Rectangle(rand_x, rand_y, 1, rand_dimension, self.screen, colours[rand_colour]) self.objects.append(ob2) nr = nr - 1 elif (((rand_x + 30, rand_y) in self.Glist) and ((rand_x + 30, rand_y + 30) in self.Glist) and ((rand_x, rand_y + 30) in self.Glist) and ((rand_x + 30, rand_y + 60) in self.Glist) and ((rand_x, rand_y + 60) in self.Glist) and (rand_dimension > 45) and (self.Glist[rand_x, rand_y] == True) and (self.Glist[rand_x + 30, rand_y] == True) and (self.Glist[rand_x + 30, rand_y + 30] == True) and (self.Glist[rand_x, rand_y + 30] == True) and ((rand_x + 30, rand_y + 60) == True) and (self.Glist[rand_x, rand_y + 60] == True)): self.Glist = self.occupy_grid(rand_x + 30, rand_y) self.Glist = self.occupy_grid(rand_x + 30, rand_y + 30) self.Glist = self.occupy_grid(rand_x, rand_y + 30) self.Glist = self.occupy_grid(rand_x + 30, rand_y + 60) self.Glist = self.occupy_grid(rand_x, rand_y + 60) ob2 = Rectangle(rand_x, rand_y, 1, rand_dimension, self.screen, colours[rand_colour]) self.objects.append(ob2) nr = nr - 1 elif rand_shape == 3: if ((rand_dimension < 30) and (self.Glist[rand_x, rand_y] == True)): ob3 = Triangle(rand_x, rand_y, 1, rand_dimension, self.screen, colours[rand_colour]) self.objects.append(ob3) self.Glist = self.occupy_grid(rand_x, rand_y) nr = nr - 1 elif (((rand_x + 30, rand_y) in self.Glist) and ((rand_x, rand_y + 30) in self.Glist) and (rand_dimension > 30) and (self.Glist[rand_x, rand_y] == True) and (rand_dimension <= 45) and (self.Glist[rand_x + 30, rand_y] == True) and (self.Glist[rand_x, rand_y + 30] == True)): self.Glist = self.occupy_grid(rand_x + 30, rand_y) self.Glist = self.occupy_grid(rand_x, rand_y + 30) ob3 = Triangle(rand_x, rand_y, 1, rand_dimension, self.screen, colours[rand_colour]) self.objects.append(ob3) nr = nr - 1 elif (((rand_x + 30, rand_y) in self.Glist) and ((rand_x + 30, rand_y + 30) in self.Glist) and ((rand_x, rand_y + 30) in self.Glist)) and ( (rand_dimension > 45) and (self.Glist[rand_x, rand_y] == True) and (self.Glist[rand_x + 30, rand_y] == True) and (self.Glist[rand_x + 30, rand_y + 30] == True) and (self.Glist[rand_x, rand_y + 30] == True)): self.Glist = self.occupy_grid(rand_x + 30, rand_y) self.Glist = self.occupy_grid(rand_x, rand_y + 30) self.Glist = self.occupy_grid(rand_x + 30, rand_y + 30) ob3 = Triangle(rand_x, rand_y, 1, rand_dimension, self.screen, colours[rand_colour]) self.objects.append(ob3) nr = nr - 1 pygame.display.update()
def random_object_generator(self, nr): self.objects = [] self.obstacles = [] self.Glist = self.create_grid() rand_obst = randint(2, 3) while rand_obst != 0: rand_dimension = randint(45, 60) i = randint(0, len(self.Glist) - 1) pos = list(self.Glist)[i] rand_x = pos[0] rand_y = pos[1] ok = 1 #checking if the position is available for x in range(rand_x - 60, rand_x + 61, 30): for y in range(rand_y - 60, rand_y + 61, 30): if (x, y) in self.Glist and self.Glist[(x, y)] == True: pass else: ok = 0 if ok: for x in range(rand_x - 60, rand_x + 61, 30): for y in range(rand_y - 60, rand_y + 61, 30): self.Glist = self.occupy_grid(x, y) ob4 = Circle(rand_x, rand_y, rand_dimension, self.screen, self.yellow) rand_obst = rand_obst - 1 self.obstacles.append(ob4) self.green_keys = [] self.red_keys = [] self.blue_keys = [] while nr > 0: rand_shape = randint(1, 3) rand_colour = randint(1, 3) colours = {1: self.red, 2: self.blue, 3: self.green} condition_for_red = 0 condition_for_blue = 0 condition_for_green = 0 condition_exist = 0 if rand_colour == 1: #red price = randint(4, 6) x = randint(1, 100) if x > 60: condition_for_red = 1 elif rand_colour == 2: #blue price = randint(7, 9) x = randint(1, 100) if x > 70: condition_for_blue = 1 elif rand_colour == 3: #green price = randint(1, 3) x = randint(1, 100) if x > 50: condition_for_green = 1 j = randint(0, len(self.Glist) - 1) pos = list(self.Glist)[j] rand_x = pos[0] rand_y = pos[1] if rand_shape == 1: ok = 1 for x in range(rand_x, rand_x + 31, 30): for y in range(rand_y, rand_y + 31, 30): if (x, y) in self.Glist and self.Glist[(x, y)] == True: pass else: ok = 0 if ok: for x in range(rand_x, rand_x + 31, 30): for y in range(rand_y, rand_y + 31, 30): self.Glist = self.occupy_grid(x, y) ob1 = Square(rand_x, rand_y, price, 45, self.screen, colours[rand_colour]) condition_exist = 1 self.objects.append(ob1) nr = nr - 1 elif rand_shape == 2: ok = 1 for x in range(rand_x, rand_x + 31, 30): for y in range(rand_y, rand_y + 31, 30): if (x, y) in self.Glist and self.Glist[(x, y)] == True: pass else: ok = 0 if ok: for x in range(rand_x, rand_x + 31, 30): for y in range(rand_y, rand_y + 31, 30): self.Glist = self.occupy_grid(x, y) ob2 = Triangle(rand_x, rand_y, price, 45, self.screen, colours[rand_colour]) condition_exist = 1 self.objects.append(ob2) nr = nr - 1 elif rand_shape == 3: ok = 1 for x in range(rand_x, rand_x + 31, 30): for y in range(rand_y, rand_y + 31, 30): if (x, y) in self.Glist and self.Glist[(x, y)] == True: pass else: ok = 0 if ok: for x in range(rand_x, rand_x + 31, 30): for y in range(rand_y, rand_y + 31, 30): self.Glist = self.occupy_grid(x, y) ob3 = Rectangle(rand_x, rand_y, price, 45, self.screen, colours[rand_colour]) condition_exist = 1 self.objects.append(ob3) nr = nr - 1 if condition_exist and condition_for_red: self.red_keys.append('key') if condition_exist and condition_for_blue: self.blue_keys.append('key') if condition_exist and condition_for_green: self.green_keys.append('key') pygame.display.update()
def random_object_generator(self, nr): self.objects = [] self.obstacles = [] self.Glist = self.create_grid() rand_obst = randint(2, 3) while rand_obst != 0: rand_dimension = randint(45, 60) i = randint(0, len(self.Glist) - 1) pos = list(self.Glist)[i] rand_x = pos[0] rand_y = pos[1] ok = 1 #checking if the position is available for x in range(rand_x - 60, rand_x + 61, 30): for y in range(rand_y - 60, rand_y + 61, 30): if (x, y) in self.Glist and self.Glist[(x, y)] == True: pass else: ok = 0 if ok: for x in range(rand_x - 60, rand_x + 61, 30): for y in range(rand_y - 60, rand_y + 61, 30): self.Glist = self.occupy_grid(x, y) ob4 = Circle(rand_x, rand_y, rand_dimension, self.screen, self.yellow) rand_obst = rand_obst - 1 self.obstacles.append(ob4) while nr > 0: rand_shape = randint(1, 3) rand_colour = randint(1, 3) colours = {1: self.red, 2: self.blue, 3: self.green} if rand_colour == 1: price = randint(4, 6) elif rand_colour == 2: price = randint(7, 9) elif rand_colour == 3: price = randint(1, 3) j = randint(0, len(self.Glist) - 1) pos = list(self.Glist)[j] rand_x = pos[0] rand_y = pos[1] if rand_shape == 1: ok = 1 for x in range(rand_x, rand_x + 31, 30): for y in range(rand_y, rand_y + 31, 30): if (x, y) in self.Glist and self.Glist[(x, y)] == True: pass else: ok = 0 if ok: for x in range(rand_x, rand_x + 31, 30): for y in range(rand_y, rand_y + 31, 30): self.Glist = self.occupy_grid(x, y) ob1 = Square(rand_x, rand_y, price, 45, self.screen, colours[rand_colour]) self.objects.append(ob1) nr = nr - 1 elif rand_shape == 2: ok = 1 for x in range(rand_x, rand_x + 31, 30): for y in range(rand_y, rand_y + 31, 30): if (x, y) in self.Glist and self.Glist[(x, y)] == True: pass else: ok = 0 if ok: for x in range(rand_x, rand_x + 31, 30): for y in range(rand_y, rand_y + 31, 30): self.Glist = self.occupy_grid(x, y) ob2 = Triangle(rand_x, rand_y, price, 45, self.screen, colours[rand_colour]) self.objects.append(ob2) nr = nr - 1 elif rand_shape == 3: ok = 1 for x in range(rand_x, rand_x + 31, 30): for y in range(rand_y, rand_y + 31, 30): if (x, y) in self.Glist and self.Glist[(x, y)] == True: pass else: ok = 0 if ok: for x in range(rand_x, rand_x + 31, 30): for y in range(rand_y, rand_y + 31, 30): self.Glist = self.occupy_grid(x, y) ob3 = Rectangle(rand_x, rand_y, price, 45, self.screen, colours[rand_colour]) self.objects.append(ob3) nr = nr - 1 pygame.display.update()
def random_object_generator(self,nr): """ This function generates 2 or 3 sun obstacles, 4 to 10 asteroids and nr random shapes. Input: 1: nr = number of random shapes. """ self.objects=[] self.obstacles=[] self.Glist=self.create_grid() rand_obst=randint(2,3) while rand_obst!=0: rand_dimension=randint(45,60) i=randint(0,len(self.Glist)-1) pos=list(self.Glist)[i] rand_x=pos[0] rand_y=pos[1] ok=1 #checking if the position is available for x in range(rand_x-60,rand_x+61,30): for y in range(rand_y-60,rand_y+61,30): if (x,y)in self.Glist and self.Glist[(x,y)]==True: pass else: ok=0 if ok: for x in range(rand_x-60,rand_x+61,30): for y in range(rand_y-60,rand_y+61,30): self.Glist=self.occupy_grid(x,y) ob4=Circle(rand_x,rand_y,rand_dimension,self.screen,self.yellow) rand_obst=rand_obst-1 self.obstacles.append(ob4) # ASTEROIDS # rand_asteroid = randint(4,10) while rand_asteroid != 0: rand_dimension = randint(15,35) x_rand, y_rand = self.randomAsteroidSpawn() a1=Asteroid(x_rand,y_rand,rand_dimension,self.screen,self.grey) for x in range(x_rand-rand_dimension,x_rand+rand_dimension, 1): for y in range(y_rand-rand_dimension,y_rand+rand_dimension, 1): self.Glist=self.occupy_grid(x,y) rand_asteroid=rand_asteroid-1 self.obstacles.append(a1) self.A_list.append(a1) # ASTEROIDS # while nr>0: rand_shape=randint(1,3) rand_colour=randint(1,3) colours={1:self.red,2:self.blue,3:self.green} if rand_colour==1: price=randint(4,6) elif rand_colour==2: price=randint(7,9) elif rand_colour==3: price=randint(1,3) j=randint(0,len(self.Glist)-1) pos=list(self.Glist)[j] rand_x=pos[0] rand_y=pos[1] if rand_shape==1: ok=1 for x in range(rand_x,rand_x+31,30): for y in range(rand_y,rand_y+31,30): if (x,y)in self.Glist and self.Glist[(x,y)]==True: pass else: ok=0 if ok: for x in range(rand_x,rand_x+31,30): for y in range(rand_y,rand_y+31,30): self.Glist=self.occupy_grid(x,y) ob1=Square(rand_x,rand_y,price,45,self.screen,colours[rand_colour]) self.objects.append(ob1) nr=nr-1 elif rand_shape==2: ok=1 for x in range(rand_x,rand_x+31,30): for y in range(rand_y,rand_y+31,30): if (x,y)in self.Glist and self.Glist[(x,y)]==True: pass else: ok=0 if ok: for x in range(rand_x,rand_x+31,30): for y in range(rand_y,rand_y+31,30): self.Glist=self.occupy_grid(x,y) ob2=Triangle(rand_x,rand_y,price,45,self.screen,colours[rand_colour]) self.objects.append(ob2) nr=nr-1 elif rand_shape==3: ok=1 for x in range(rand_x,rand_x+31,30): for y in range(rand_y,rand_y+31,30): if (x,y)in self.Glist and self.Glist[(x,y)]==True: pass else: ok=0 if ok: for x in range(rand_x,rand_x+31,30): for y in range(rand_y,rand_y+31,30): self.Glist=self.occupy_grid(x,y) ob3=Rectangle(rand_x,rand_y,price,45,self.screen,colours[rand_colour]) self.objects.append(ob3) nr=nr-1 pygame.display.update()