def generate(self, barrier_name): self.count += 1 if barrier_name == "Sky": sky = Sky(0,0,WIDTH,SKY_WIDTH) if type(sky) is self.dict["Sky"]: return sky if barrier_name == "Ground": ground = Ground(0,HEIGHT-SAND_HEIGHT,WIDTH,SAND_HEIGHT) if type(ground) is self.dict["Ground"]: return ground if barrier_name == "Tube": TUBE_HEIGHT = random.randint(20,HEIGHT - SAND_HEIGHT - TUBE_GAP - 20) #TUBE_HEIGHT = self.__list_tube_height[self.index] self.index += 1 tubeTop = Tube(WIDTH, 0, TUBE_WIDTH, TUBE_HEIGHT) tubeBottom = Tube(WIDTH, TUBE_HEIGHT + TUBE_GAP, TUBE_WIDTH, HEIGHT - SAND_HEIGHT - (TUBE_HEIGHT + TUBE_GAP)) if type(tubeTop) and type(tubeBottom) is self.dict["Tube"]: return tubeTop, tubeBottom raise NameError("Wrong factory format")
def new(self): # Initialise sprite groups. self.bird_group = pygame.sprite.Group() self.pipes = pygame.sprite.Group() self.ground_group = pygame.sprite.Group() self.all_sprites = pygame.sprite.Group() # Initialise bird. self.bird = Bird(self) self.bird_group.add(self.bird) self.all_sprites.add(self.bird) # Initialise ground. self.ground = Ground() self.ground_group.add(self.ground) self.all_sprites.add(self.ground) # Initialise bottom pipe. self.pipe_down = Bottom_Pipe(self) self.pipes.add(self.pipe_down) self.all_sprites.add(self.pipe_down) # Initialise top pipe. self.pipe_up = Top_Pipe(self) self.pipes.add(self.pipe_up) self.all_sprites.add(self.pipe_up) self.run()
def startGround(self): """ the ground is a 2-D space representing the field where ants wander """ self.Ground = Ground(self, Toric=True) self.Ground.scaleX = self.Ground.scaleY = LandSize # Logical coordinates self.Ground.W = self.Ground.H = LandWindowSize # Physical coordinates self.Ground.configure(width=self.Ground.W, height=self.Ground.H) self.Ground.pack(expand=Tkinter.YES, fill=Tkinter.BOTH) # the window shows on the scren
def play_bird(): global WIN, GEN win = WIN bird = Bird.Bird(230, 350, BIRD_IMGS) ground = Ground.Ground(730, GROUND_IMG) pipes = [Pipe.Pipe(600, PIPE_IMG)] clock = pygame.time.Clock() score = 0 run = True while run: clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bird.jump() bird.move() if ground.collide(bird): run = False add_pipe = False rem = [] for pipe in pipes: if pipe.collide(bird): run = False if not pipe.passed and pipe.x < bird.x: pipe.passed = True add_pipe = True if pipe.x + pipe.PIPE_TOP.get_width() < 0: rem.append(pipe) pipe.move() if add_pipe: score += 1 pipes.append(Pipe.Pipe(600, PIPE_IMG)) for r in rem: pipes.remove(r) ground.move() draw_play_window(win, bird, pipes, ground, score)
def __init__(self): self.index = 0 self.__list_tube_height = [] for i in range(1,3000): self.__list_tube_height.append(random.randint(10,HEIGHT - SAND_HEIGHT - TUBE_GAP- 10)) self.count = 0 tube = Tube(0,0,0,0) sky = Sky(0,0,0,0) ground = Ground(0,0,0,0) self.dict = {"Tube": type(tube), "Sky" : type(sky), "Ground": type(ground)}
def main(): """The main program.""" # Initialize Pygame pygame.init() # First we set some stuff up. prefs = Preferences.Preferences() prefs.loadPreferences() video = Video.Video(prefs) video.prepareVideo() carObject = Car.Car() groundObject = Ground.Ground() carDir = 0.0 while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() carDir += 1.0 if (carDir >= 360.0): carDir -= 360.0 carObject.setAngle(carDir) video.prepareNewFrame() video.setupCamera() video.setupLights() # Drawing happens here glLoadIdentity() drawReference(0.0, 0.0, 0.0, 1.0) video.drawSimulationObject(groundObject) video.drawSimulationObject(carObject) video.finishNewFrame() pygame.time.wait(33)
if (isinstance(sceneObject, Firework)): if(sceneObject.decayed): deadFireworks.append(sceneObject) for deadFirework in deadFireworks: sceneObjects.remove(deadFirework) pygame.init() RenderEngine.init(640, 480) camera = Camera(-9, 4, -26, 70) physicsSimulator = PhysicsSimulator() physicsSimulator.simulate = True isControlCar = False sceneObjects = [] ground = Ground() sceneObjects.append(ground) sceneObjects.extend(generateNeighborhood()) car = Car(positionZ=20, rotationY=0) car.mass = 15 sceneObjects.append(car) car2 = Car(positionZ=40, positionX=-20, steeringAngle=10) sceneObjects.append(car2) car3 = Car(positionZ=11, positionX=-40, driveAcceleration=0.06) car3.mass = 15 sceneObjects.append(car3) car4 = Car(positionZ=11, positionX=40, rotationY=180, driveAcceleration=0.06) sceneObjects.append(car4) speed = 1 done = False
def verse_bird(): global WIN, GEN win = WIN print("in here") with open('best_bird.obj', 'rb') as best_bird: net = pickle.load(best_bird) ai_bird = Bird.Bird(230, 350, BIRD_IMGS) player_bird = Bird.Bird(230, 350, PLAYER_BIRD_IMGS) ground = Ground.Ground(730, GROUND_IMG) pipes = [Pipe.Pipe(600, PIPE_IMG)] clock = pygame.time.Clock() player_win = False ai_win = False score = 0 run = True while run: clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: player_bird.jump() pipe_index = 0 if len(pipes) > 1 and ai_bird.x > pipes[0].x + pipes[ 0].PIPE_TOP.get_width(): pipe_index = 1 ai_bird.move() player_bird.move() output = net.activate( (ai_bird.y, abs(ai_bird.y - pipes[pipe_index].height), abs(ai_bird.y - pipes[pipe_index].bottom), abs(pipes[pipe_index].x))) if output[0] > 0.5: ai_bird.jump() add_pipe = False rem = [] for pipe in pipes: if pipe.collide(ai_bird): player_win = True run = False if pipe.collide(player_bird): ai_win = True run = False if not pipe.passed and pipe.x < ai_bird.x: pipe.passed = True add_pipe = True if pipe.x + pipe.PIPE_TOP.get_width() < 0: rem.append(pipe) pipe.move() if add_pipe: score += 1 pipes.append(Pipe.Pipe(600, PIPE_IMG)) for r in rem: pipes.remove(r) if ai_bird.y + ai_bird.img.get_height() >= 730 or ai_bird.y < 0: player_win = True run = False if player_bird.y + player_bird.img.get_height( ) >= 730 or player_bird.y < 0: ai_win = True run = False if score >= 50: run = False ground.move() draw_verse_window(win, ai_bird, player_bird, pipes, ground, score)
def eval_genome(genomes, config): global WIN, GEN win = WIN GEN += 1 nets = [] ge = [] birds = [] for _, g in genomes: net = neat.nn.FeedForwardNetwork.create(g, config) nets.append(net) birds.append(Bird.Bird(230, 350, BIRD_IMGS)) g.fitness = 0 ge.append(g) ground = Ground.Ground(730, GROUND_IMG) pipes = [Pipe.Pipe(600, PIPE_IMG)] clock = pygame.time.Clock() score = 0 run = True while run: clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.quit() quit() pipe_index = 0 if len(birds) > 0: if len(pipes) > 1 and birds[ 0].x > pipes[0].x + pipes[0].PIPE_TOP.get_width(): pipe_index = 1 else: run = False break for x, bird in enumerate(birds): bird.move() ge[x].fitness += 0.1 output = nets[x].activate( (bird.y, abs(bird.y - pipes[pipe_index].height), abs(bird.y - pipes[pipe_index].bottom), abs(pipes[pipe_index].x))) if output[0] > 0.5: bird.jump() add_pipe = False rem = [] for pipe in pipes: for x, bird in enumerate(birds): if pipe.collide(bird): ge[x].fitness -= 2 birds.pop(x) nets.pop(x) ge.pop(x) if not pipe.passed and pipe.x < bird.x: pipe.passed = True add_pipe = True if pipe.x + pipe.PIPE_TOP.get_width() < 0: rem.append(pipe) pipe.move() if add_pipe: score += 1 for g in ge: g.fitness += 5 pipes.append(Pipe.Pipe(600, PIPE_IMG)) for r in rem: pipes.remove(r) for x, bird in enumerate(birds): if bird.y + bird.img.get_height() >= 730 or bird.y < 0: birds.pop(x) nets.pop(x) ge.pop(x) if score >= 15: print("score: ", score) run = False ground.move() draw_train_window(win, birds, pipes, ground, score, GEN)
def loadLevel(lev): f = open(lev, "r") lines = f.readlines() f.close() size = 25 offset = size / 2 tiles = [] walls = [] grounds = [] dirts = [] floors = [] steaks = [] fallblocks = [] saltspikels = [] saltspikers = [] sidespikes = [] cobbles = [] ends = [] playerPos = [] newLines = [] for line in lines: newLine = "" for c in line: if c != "\n": newLine += c newLines += [newLine] lines = newLines for y, line in enumerate(lines): for x, c in enumerate(line): if c == "#": tiles += [VineWall([x * size + offset, y * size + offset])] if c == "G": tiles += [Ground([x * size + offset, y * size + offset])] if c == "D": tiles += [Dirt([x * size + offset, y * size + offset])] if c == "F": tiles += [Floor([x * size + offset, y * size + offset])] if c == "J": tiles += [FallBlock([x * size + offset, y * size + offset])] if c == "^": tiles += [ SaltSpike([x * size + offset, y * size + offset + 4]) ] if c == "<": tiles += [SideSpikeL([x * size + offset, y * size + offset])] if c == ">": tiles += [SideSpikeR([x * size + offset, y * size + offset])] if c == "W": tiles += [Cobbled([x * size + offset, y * size + offset])] if c == "O": tiles += [END([x * size + offset, y * size + offset])] if c == "S": tiles += [Steak([x * size + offset, y * size + offset - 5])] if c == "C": playerPos += [x * size + offset, y * size + offset] return tiles, playerPos
pipe_height_down = random.randrange(130, 350, 1) pipe_x_up = pipe_x_down pipe_height_up = 380 - pipe_height_down pipe_down = Pipe(pipe_img, pipe_x_down, pipe_height_down, False) pipe_up = Pipe(pipe_img, pipe_x_up, pipe_height_up, True) return pipe_down, pipe_up pipe_down, pipe_up = config_pipe(640) pipe_down2, pipe_up2 = config_pipe(960) # config ground ground1 = Ground(ground_img, 0) ground2 = Ground(ground_img, 336) ground3 = Ground(ground_img, 672) ground4 = Ground(ground_img, 1008) # config timer frame_passed = 0 running = True while running: collision = (pygame.sprite.collide_rect( bird, pipe_down) or pygame.sprite.collide_rect(bird, pipe_up)) or ( pygame.sprite.collide_rect(bird, pipe_down2) or pygame.sprite.collide_rect(bird, pipe_up2)) or bird.y > 360