资源简介
是AI就躲个飞机-纯Python实现人工智能
代码片段和文件信息
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
“““
Created on Tue Jan 17 16:48:59 2017
@author: liupeng
“““
import pygame
import sys
from pygame.locals import *
import random
import math
import neuro_evolution
BACKGROUND = (200 200 200)
SCREEN_SIZE = (320 480)
class Plane():
def __init__(self plane_image):
self.plane_image = plane_image
self.rect = plane_image.get_rect()
self.width = self.rect[2]
self.height = self.rect[3]
self.x = SCREEN_SIZE[0]/2 - self.width/2
self.y = SCREEN_SIZE[1] - self.height
self.move_x = 0
self.speed = 2
self.alive = True
def update(self):
self.x += self.move_x * self.speed
def draw(self screen):
screen.blit(self.plane_image (self.x self.y self.width self.height))
def is_dead(self enemes):
if self.x < -self.width or self.x + self.width > SCREEN_SIZE[0]+self.width:
return True
for eneme in enemes:
if self.collision(eneme):
return True
return False
def collision(self eneme):
if not (self.x > eneme.x + eneme.width or self.x + self.width < eneme.x or self.y > eneme.y + eneme.height or self.y + self.height < eneme.y):
return True
else:
return False
def get_inputs_values(self enemes input_size=4):
inputs = []
for i in range(input_size):
inputs.append(0.0)
inputs[0] = (self.x*1.0 / SCREEN_SIZE[0])
index = 1
for eneme in enemes:
inputs[index] = eneme.x*1.0 / SCREEN_SIZE[0]
index += 1
inputs[index] = eneme.y*1.0 / SCREEN_SIZE[1]
index += 1
#if len(enemes) > 0:
#distance = math.sqrt(math.pow(enemes[0].x + enemes[0].width/2 - self.x + self.width/2 2) + math.pow(enemes[0].y + enemes[0].height/2 - self.y + self.height/2 2));
if len(enemes) > 0 and self.x < enemes[0].x:
inputs[index] = -1.0
index += 1
else:
inputs[index] = 1.0
return inputs
class Enemy():
def __init__(self enemy_image):
self.enemy_image = enemy_image
self.rect = enemy_image.get_rect()
self.width = self.rect[2]
self.height = self.rect[3]
self.x = random.choice(range(0 int(SCREEN_SIZE[0] - self.width/2) 71))
self.y = 0
def update(self):
self.y += 6
def draw(self screen):
screen.blit(self.enemy_image (self.x self.y self.width self.height))
def is_out(self):
return True if self.y >= SCREEN_SIZE[1] else False
class Game():
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode(SCREEN_SIZE)
self.clock = py
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-01-17 16:59 pygame001\
文件 6148 2017-01-17 16:51 pygame001\.DS_Store
目录 0 2017-01-17 17:00 __MACOSX\
目录 0 2017-01-17 17:00 __MACOSX\pygame001\
文件 120 2017-01-17 16:51 __MACOSX\pygame001\._.DS_Store
文件 5762 2017-01-17 16:53 pygame001\enemy.png
文件 281 2017-01-17 16:53 __MACOSX\pygame001\._enemy.png
文件 5768 2017-01-17 16:50 pygame001\game.py
文件 210 2017-01-17 16:50 __MACOSX\pygame001\._game.py
文件 8679 2017-01-17 16:59 pygame001\neuro_evolution.py
文件 210 2017-01-17 16:59 __MACOSX\pygame001\._neuro_evolution.py
文件 9174 2017-01-17 16:59 pygame001\neuro_evolution.pyc
文件 13645 2017-01-17 16:52 pygame001\plane.png
文件 281 2017-01-17 16:52 __MACOSX\pygame001\._plane.png
评论
共有 条评论