• 大小: 14.97MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-06-21
  • 语言: Python
  • 标签:

资源简介

使用遗传算法和深度学习训练AI来玩贪吃蛇的游戏

资源截图

代码片段和文件信息

import random
import pygame
import colors as col


class Arena:
    def __init__(self width height block):
        self.height = height
        self.width = width
        self.block = block
        self.food = (0 0)

    def setup_background(self screen color):
        screen.fill(color)

    def setup(self screen color_bg color):
        self.setup_background(screen color_bg)
        # building the horizontal walls
        l = self.block
        for x in range(0 self.width l):
            y = 0
            pygame.draw.rect(screen color (x y l l) 1)
            pygame.draw.rect(screen color (x+3 y+3 l-6 l-6))
            y = self.height - l
            pygame.draw.rect(screen color (x y l l) 1)
            pygame.draw.rect(screen color (x+3 y+3 l-6 l-6))
        # building the vertical walls
        for y in range(l self.height-l l):
            x = 0
            pygame.draw.rect(screen color (x y l l) 1)
            pygame.draw.rect(screen color (x+3 y+3 l-6 l-6))
            x = self.width - l
            pygame.draw.rect(screen color (x y l l) 1)
            pygame.draw.rect(screen color (x+3 y+3 l-6 l-6))
        return screen

    def newFood(self list):
        ‘‘‘
        list = snake body parts position list
        ‘‘‘
        found = False
        size = self.block
        while not found:
            x = random.randint(2*size self.width - 2*size)
            x = x - (x % size)
            y = random.randint(2*size self.height - 2*size)
            y = y - (y % size)
            i = 0
            while i < len(list):
                if x == list[i][0] and y == list[i][1]:
                    break
                i = i + 1
            if i == len(list):
                found = True
        self.food = (x y)
        return self.food

    def drawFood(self screen color):
        pygame.draw.rect(screen color (self.food[0] self.food[1] self.block self.block))
        return screen

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-04-15 17:07  AI_plays_snake-master\
     文件          40  2019-04-15 17:07  AI_plays_snake-master\.gitignore
     文件        1990  2019-04-15 17:07  AI_plays_snake-master\Arena.py
     文件        6846  2019-04-15 17:07  AI_plays_snake-master\Genetic_algo.py
     文件        1066  2019-04-15 17:07  AI_plays_snake-master\LICENSE
     文件        2117  2019-04-15 17:07  AI_plays_snake-master\README.md
     文件        6842  2019-04-15 17:07  AI_plays_snake-master\brain.py
     文件         196  2019-04-15 17:07  AI_plays_snake-master\colors.py
     文件        5063  2019-04-15 17:07  AI_plays_snake-master\game.py
     文件         434  2019-04-15 17:07  AI_plays_snake-master\input.py
     文件          22  2019-04-15 17:07  AI_plays_snake-master\requirements.txt
     目录           0  2019-04-15 17:07  AI_plays_snake-master\samples\
     文件    25079462  2019-04-15 17:07  AI_plays_snake-master\samples\generation23.gif
     文件     5011192  2019-04-15 17:07  AI_plays_snake-master\samples\generation5.gif
     目录           0  2019-04-15 17:07  AI_plays_snake-master\saved\
     文件    10698523  2019-04-15 17:07  AI_plays_snake-master\saved\top_snakes.pickle
     文件        7116  2019-04-15 17:07  AI_plays_snake-master\snake.py

评论

共有 条评论