コード例 #1
0
ファイル: main.py プロジェクト: ronitchitre/SnakeAI
 def check_collision(self):
     if self.food.pos == self.snake.body[0]:
         self.food = classes.Food()
         self.snake.add_block()
         self.score += 1
         self.snake.crunch_sound.play()
         for block in self.snake.body[1:]:
             if block == self.food.pos:
                 self.food = classes.Food()
コード例 #2
0
ファイル: main.py プロジェクト: ronitchitre/SnakeAI
 def __init__(self):
     self.snake = classes.Snake()
     self.food = classes.Food()
     self.score = 0
     bg_img = pygame.image.load("snakebg.jpg")
     self.bg_img = pygame.transform.scale(
         bg_img, (cell_number * cell_size, cell_number * cell_size))
     self.game_start = False
コード例 #3
0
    def add_to_db(self, name, group):
        self.root.admin['1'].latest_key += 1
        if group == "Building":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Building()
        elif group == "Location":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Location()
        elif group == "Lodging":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Lodging()
        elif group == "Player":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Player()
        elif group == "NonPlayer":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.NonPlayer()
        elif group == "Monster":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Monster()
        elif group == "Animal":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Animal()
        elif group == "Armour":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Armour()
        elif group == "Weapon":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Weapon()
        elif group == "Gear":
            self.root.mainline[str(self.root.admin['1'].latest_key)] = c.Gear()
        elif group == "Food":
            self.root.mainline[str(self.root.admin['1'].latest_key)] = c.Food()
        elif group == "Magic":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Magic()
        elif group == "Spell":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Spell()
        elif group == "Skill":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Skill()
        elif group == "Quest":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Quest()
        elif group == "Encounter":
            self.root.mainline[str(
                self.root.admin['1'].latest_key)] = c.Encounter()

        else:
            print("group unknown")
        self.root.mainline[str(self.root.admin['1'].latest_key)].name = name
        transaction.commit()
コード例 #4
0
def start_game(snake_num, food_num):
    # setting globals
    global stats
    global snakes
    global foods
    global all_entities
    global gamestart
    global difficulty

    gamestart = False

    # setting game entities
    snakes.clear()
    for i in range(snake_num):
        snakes.append(snake.Snake())
    for s in snakes:
        s.current_sprite = random.randint(0, 3)
        s.speed_base = difficulty.value.base_speed
        s.speed_modifier = difficulty.value.modifier

    foods.clear()
    for i in range(food_num):
        foods.append(classes.Food(snakes))

    # special setting for two player mode
    if snake_num == 2:
        snakes[1].all_positions = [(4 * pixel_width, 10 * pixel_width),
                                   (5 * pixel_width, 10 * pixel_width),
                                   (6 * pixel_width, 10 * pixel_width),
                                   (7 * pixel_width, 10 * pixel_width)]
        snakes[1].set_letter_keys()

    all_entities.clear()
    all_entities.extend(snakes)
    all_entities.extend(foods)

    # Make it so that food avoids every entitiy
    # that has collision enables
    for f in foods:
        f.entities = all_entities

    # setting stats
    stats = classes.Stats(snakes)
    all_entities.append(stats)
コード例 #5
0
ファイル: abc.py プロジェクト: lelapenda/__swarm-optimization
            if r < f.p:
                f.greedy_search(food, space, idx)

    #---SCOUT
    for i in range(0, N_FOOD):
        if food[i].countlimit >= 100:
            food[i] = Food()  #scouts look for new food


for func in FUNCTIONS:

    for simulacoes in range(0, N_SIMULATIONS):
        print(simulacoes, func)

        space = classes.Space(D, N_FOOD, func)
        food = [classes.Food(space) for i in range(0, N_FOOD)]
        FITNESS_VALUES = []
        BEST_FITNESS = float('inf')

        for ite in range(0, N_INTERATIONS):

            main(food, space)
            for f in food:
                if f.fitness < BEST_FITNESS:
                    BEST_FITNESS = f.fitness

            #print(BEST_FITNESS, func)
            FITNESS_VALUES.append(BEST_FITNESS)

        FITNESS_SIMULACOES_ITE[func].append(np.array(FITNESS_VALUES))
        FITNESS_SIMULACOES_FUNC[func].append(BEST_FITNESS)
コード例 #6
0
#root.mainline['20'] = c.Building()
#root.mainline['21'] = c.Lodging()

root.structure['1'] = c.Root()  # structural model
root.structure['2'] = c.Actor()
root.structure['3'] = c.Item()
root.structure['4'] = c.Action()
root.structure['5'] = c.Place()
root.structure['6'] = c.Player()
root.structure['7'] = c.NonPlayer()
root.structure['8'] = c.Monster()
root.structure['9'] = c.Animal()
root.structure['10'] = c.Armour()
root.structure['11'] = c.Weapon()
root.structure['12'] = c.Gear()
root.structure['13'] = c.Food()
root.structure['14'] = c.Magic()
root.structure['15'] = c.Spell()
root.structure['16'] = c.Skill()
root.structure['17'] = c.Quest()
root.structure['18'] = c.Encounter()
root.structure['19'] = c.Location()
root.structure['20'] = c.Building()
root.structure['21'] = c.Lodging()

transaction.commit()

for pair in root.mainline.iteritems():
    print(pair)

db.close()
コード例 #7
0
def game(music_play):
    import gameOver

    pygame.init()
    pygame.display.set_caption('Ultimate Snake')
    screen = pygame.display.set_mode(size, pygame.FULLSCREEN)

    #trys to get the highScore and saves them, if it cant then it assumes
    #that the highScore is 0
    try:
        highscore = highScores.read()
        scores = highScores.save()
    except:
        highscore = 0
        scores = []

    game = True

    while game == True:
        #plays the music if not off
        if music_play != "off":
            music.play(-1)

        #defines the bgColor, timeColor, scoreColor
        bgColor = 0, 0, 0
        scoreColor = 255, 255, 255
        timeColor = 255, 255, 255

        timer = 10000  #sets the time to get the food to 10sec

        count = 1  #count is used to write the scores once

        score = classes.Score(screen, highscore)
        snake = classes.Snake(screen)
        food = classes.Food(screen, snake)

        pause = 0
        pCount = 0
        font = pygame.font.SysFont("Helvetica", 25)
        clock.tick()

        while True:
            clock.tick()
            pause = 0

            #if the escape key is pressed then the game will quit
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        pygame.quit()
                        sys.exit()

                #press 'p' to pause and unpause the game
                #it goes into an infinite loop when "p" is pressed
                if event.type == KEYDOWN:
                    if event.key == K_p:
                        pygame.mixer.pause()  #pauses music
                        pCount = 1
                        pauseClock.tick()  #ticks the clock for the pauseClock
                        while 1:
                            event = pygame.event.wait()
                            if event.type == KEYDOWN:
                                if event.key == K_p:
                                    pygame.mixer.unpause()  #resumes music
                                    break
                                if event.type == pygame.QUIT:
                                    pygame.quit()
                                    sys.exit()
                                if event.key == K_ESCAPE:
                                    pygame.quit()
                                    sys.exit()
                        pauseClock.tick()  #ticks it again

                # use the arrow keys to move the snake
                if event.type == KEYDOWN:
                    if event.key == K_RIGHT:
                        snake.change_direction(right)
                    elif event.key == K_LEFT:
                        snake.change_direction(left)
                    elif event.key == K_UP:
                        snake.change_direction(up)
                    elif event.key == K_DOWN:
                        snake.change_direction(down)

            #changes the color to the time text to red when the time
            #left is less than or equal to 3 seconds
            if timer / 1000 <= 3:
                timeColor = 255, 0, 0
            else:
                timeColor = 255, 255, 255

            # chnage the bgColor to rainbow colors
            if score.score >= 1000:
                int1 = random.randrange(0, 255)
                int2 = random.randrange(0, 255)
                int3 = random.randrange(0, 255)
                bgColor = int1, int2, int3

            # if the snake dies
            if not snake.move(food, score):
                scores.append(score.score)
                if score.score > highscore:
                    highscore = score.score
                game = False  #stops the game
                break  #breaks the loop

            screen.fill(bgColor)  #fills the background
            snake.update()  #updates the snake
            score.update(scoreColor)  #updates the score
            food.update()  #updates the food

            pause = pauseClock.get_time()  #finds how long the game was
            #paused for

            #if the user does not collect the food after 10sec - GAME OVER
            if pCount == 1:
                timer = (timer - clock.get_time()) + pause
                pCount = 0
            else:
                timer -= clock.get_time()

            #BLITS THE SPEED STATUS OF THE SNAKES
            if food.delay == 80:
                speedPic = pygame.image.load(
                    'images/normalSpeed.bmp').convert()
                screen.blit(speedPic, (450, 7))
            elif food.delay == 40:
                speedPic = pygame.image.load('images/fastSpeed.bmp').convert()
                screen.blit(speedPic, (450, 7))
            elif food.delay == 120:
                speedPic = pygame.image.load('images/slowSpeed.bmp').convert()
                screen.blit(speedPic, (450, 7))

            #stop this game when the time has gone out
            if timer <= 0:
                scores.append(score.score)
                if score.score > highscore:
                    highscore = score.score
                game = False
                break

            if snake.extend_snake == True:
                timer = 10000

            #draws a line that seperates the statusbar from the game
            pygame.draw.line(screen, scoreColor, (0, 40), (1000, 40))

            #defines the time variables
            timeText = font.render("Time:", True, timeColor)
            time = font.render(str(timer / 1000), True, timeColor)
            #blits the time text and the time
            screen.blit(timeText, (250, 10))
            screen.blit(time, (310, 10))

            pygame.display.update()  #updates the display
            pygame.time.delay(food.delay)  #changes the speed of the snakes

        #IF THE GAME HAS ENDED
        while game == False:
            music.stop()

            #writes the highScore to the file
            if count == 1:
                highScores.write(scores)
                s = sorted(scores)
                count = 2

            #shows the gameOver screen
            gameOver.scoresListSingle(s[::-1], music_play)
            break