设为首页 加入收藏

TOP

x01.AntWorld: An Python AI Game(一)
2017-11-20 08:34:54 】 浏览:257
Tags:x01.AntWorld: Python Game

1. 学习了一下 AI 五子棋,顺手改作 19 路的棋盘,便于围棋通用。render.py 主要修改如下:

# 常量部分:
IMAGE_PATH = 'img/'
StoneSize = 32
WIDTH = 650
HEIGHT = 732
ColSize = 33
RowSize = 34.44
H_Pad = (HEIGHT- RowSize * 19) / 2 + (RowSize - StoneSize) / 2 + 1
W_Pad = (WIDTH - ColSize * 19) / 2 + (ColSize - StoneSize) / 2 + 1
Pad = ColSize - StoneSize, RowSize - StoneSize
PIECE = StoneSize

# 坐标转换部分:
    def coordinate_transform_map2pixel(self, i, j):    
        # 从 chessMap 里的逻辑坐标到 UI 上的绘制坐标的转换
        return j * (PIECE+Pad[0]) + W_Pad, i * (PIECE+Pad[1]) + H_Pad

    def coordinate_transform_pixel2map(self, x, y):    
        # 从 UI 上的绘制坐标到 chessMap 里的逻辑坐标的转换
        i , j = int((y-H_Pad) / (PIECE+Pad[1])), int((x-W_Pad) / (PIECE+Pad[0]))
        
        if i < 0 or i >= N or j < 0 or j >= N:
            return None, None
        else:
            return i, j

2. 发现 pygame 还不错,便从网上搜索到《Beginning Game Development With Python And Pygame》,其中蚂蚁游戏的 AI 表现甚好,主要代码如下:

import pygame

from pygame.locals import *
from random import randint
from core.vector2d import Vector2D

ScreenSize = (640, 480)
NestLocation = (320, 240)
AntCount = 20
NestSize = 100.0
ImgPath = "img/"

class State(object):
    def __init__(self, name):
        self.name = name
    def do_actions(self):
        pass 
    def check_conditions(self):
        pass 
    def entry_actions(self):
        pass 
    def exit_actions(self):
        pass 

class StateMachine(object):
    def __init__(self):
        self.states = {}
        self.active_state = None 
    def add_state(self, state):
        self.states[state.name] = state 
    def think(self):
        if self.active_state is None:
            return 
        self.active_state.do_actions()
        new_state_name = self.active_state.check_conditions()
        if new_state_name is not None:
            self.set_state(new_state_name)
    def set_state(self, new_state_name):
        if self.active_state is not None:
            self.active_state.exit_actions()
        self.active_state = self.states[new_state_name]
        self.active_state.entry_actions()

class World(object):
    def __init__(self):
        self.entities = {}
        self.entity_id = 0
        self.background = pygame.surface.Surface(ScreenSize).convert()
        self.background.fill((255,255,255))
        pygame.draw.circle(self.background, (200,255,200), NestLocation, int(NestSize))
    def add_entity(self, entity):
        self.entities[self.entity_id] = entity
        entity.id = self.entity_id
        self.entity_id += 1
    def remove_entity(self, entity):
        del self.entities[entity.id]
    def get(self, entity_id):
        if entity_id in self.entities:
            return self.entities[entity_id]
        else:
            return None
    def process(self, time_passed):
        time_passed_seconds = time_passed / 1000.0
        entities = list(self.entities.values())
        for entity in entities:
            entity.process(time_passed_seconds)
    def render(self, surface):
        surface.blit(self.background, (0,0))
        for entity in self.entities.values():
            entity.render(surface)
    def got_close_entity(self, name, location, range=100.0):
        location = Vector2D(*location)
        for entity in self.entities.values():
            if entity.name == name:
                distance = location.get_distance_to(entity.location)
                if distance < range:
                    return entity
        return None 

class GameEntity(object):
    def __init__(self, world, name, image):
        self.world = world
        self.name = name
        self.image = image
        self.location = Vector2D(0, 0)
        self.destination = Vector2D(0, 0)
        self.speed = 0
        self.brain = StateMachine()
        self.id = 0
    def render(self, surface):
        x,
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇pandas选择数据-【老鱼学pandas】 下一篇docker创建镜像发布到远端仓库

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目