Skip to content

Commit

Permalink
Cleaning up some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
rawktron committed Jun 7, 2017
1 parent b0435b8 commit f689f12
Show file tree
Hide file tree
Showing 27 changed files with 241 additions and 255 deletions.
6 changes: 0 additions & 6 deletions .gitignore
@@ -1,10 +1,4 @@
.DS_Store
.vscode/
examples/
ansigo
!ansigo/
lock.json
manifest.json
vendor/
*.pyc
data/
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2017 ActiveState Software Inc.
Copyright (c) 2015 Milo Spencer-Harper

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
76 changes: 29 additions & 47 deletions actors.py
@@ -1,26 +1,42 @@
'''The MIT License (MIT)
Copyright (c) 2017 ActiveState Software Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.'''

from __future__ import division
import pygame
import math
from utils import *
from random import randrange
import numpy as np

#EnemyHit.wav
#EnemyShoot.wav
#HeroLaser.wav
#Respawn.wav
#ShipExplode.wav

pygame.mixer.init()
shootsfx = pygame.mixer.Sound('HeroLaser.wav')
hitsfx = pygame.mixer.Sound('EnemyHit.wav')
enemyshootsfx = pygame.mixer.Sound('EnemyShoot.wav')
explodesfx = pygame.mixer.Sound('ShipExplode.wav')
respawnsfx = pygame.mixer.Sound('Respawn.wav')
shootsfx = pygame.mixer.Sound('audio/HeroLaser.wav')
hitsfx = pygame.mixer.Sound('audio/EnemyHit.wav')
enemyshootsfx = pygame.mixer.Sound('audio/EnemyShoot.wav')
explodesfx = pygame.mixer.Sound('audio/ShipExplode.wav')
respawnsfx = pygame.mixer.Sound('audio/Respawn.wav')

# Global Init stuff should have a proper home once not placeholder art
#spritesheet = pygame.image.load("spaceship_sprite_package_by_kryptid.png")
spritesheet = pygame.image.load("python-sprites.png")
spritesheet = pygame.image.load("art/python-sprites.png")
#spritesheet.set_colorkey(spritesheet.get_at((0, 0)))

class SpriteSequence(object):
Expand Down Expand Up @@ -87,18 +103,6 @@ def __init__(self, x, y, color, direction, speed, container, brain = None):
ssrect = pygame.Rect((basex,710,16,16))
global spritesheet
self.image.blit(spritesheet,(0,0),ssrect)
#self.image.convert()
#self.image.set_colorkey(self.image.get_at((0, 0)))


# self.image = pygame.Surface((10, 10), pygame.SRCALPHA, 32)
# self.image = self.image.convert_alpha()
# self.col = list(color)
# for i in range(5, 0, -1):
# self.col[0] = color[0] * float(i) / 5
# self.col[1] = color[1] * float(i) / 5
# self.col[2] = color[2] * float(i) / 5
# pygame.draw.circle(self.image, tuple(self.col), (5, 5), i 0)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.direction = direction
Expand Down Expand Up @@ -148,6 +152,7 @@ def TakeDamage(self, damage):
# Trigger Particle or something
self.blinking = True
self.blinks = 0
self.health = 100
def Die(self):
self.kill()

Expand Down Expand Up @@ -184,25 +189,19 @@ def __init__(self, bulletgroup, brain, speed):

def onAnimComplete(self,name):
if name == "blow":
#print "BLOW ANIM COMPLETE, DYING"
self.Die()

def amdead(self):
#print self
#print "ENEMY POS: "+str(self.x)+","+str(self.y)
#print "OFFSET" + str(self.animoffset)
self.playanim("blow",(self.x-48,self.y-24))


def playanim(self,name,offset):
if self.anim != self.blowAnim and name=="hit":
#print "HIT ANIM"
hitsfx.play()
self.anim = self.hitAnim
self.animoffset = (offset[0]-self.x,offset[1]-self.y)
self.anim.play()
if self.anim != self.blowAnim and name=="blow":
#print "BLOW ANIM"
explodesfx.play()
self.anim = self.blowAnim
self.animoffset = (offset[0]-self.x,offset[1]-self.y)
Expand Down Expand Up @@ -234,9 +233,6 @@ def update(self, screen, event_queue, dt, (player_x,player_y),(player_velx,playe
du = (self.velx - player_velx) / 60
dv = (self.vely - player_vely) / 60

# x is param that is the player's x position
#if math.fabs(self.x-player_x) < 5 and self.canfire:

if self.canfire:
if (trainingMode and randrange(0,100)<10) or ((netmodel == 1 and not trainingMode and self.brain.keras.predict(np.array([list((dx,dy,du,dv))]))>=0.5) or (netmodel == 0 and not trainingMode and self.brain.model.think([dx,dy,du,dv])>=0.5)):
# if (trainingMode and randrange(0,100)<10) or (not trainingMode and self.brain.model.think([dx,dy,du,dv])>=0.5):
Expand All @@ -252,8 +248,6 @@ def update(self, screen, event_queue, dt, (player_x,player_y),(player_velx,playe
class Player(Killable):
def __init__(self,bulletgroup):
super(Player, self).__init__()
# Player specifc init stuff here
# This is a dirty hack
global spritesheet
spritesheet.convert_alpha()
self.cooldown = 0.5
Expand All @@ -279,25 +273,21 @@ def __init__(self,bulletgroup):

def onAnimComplete(self,name):
if name == "blow":
#print "BLOW ANIM COMPLETE, DYING"
respawnsfx.play()
if self.lives<0:
self.Die()


def amdead(self):
#print str(self.x)+","+str(self.y)
self.playanim("blow",(self.x-48,self.y-48))

def playanim(self,name,offset):
if self.anim != self.blowAnim and name=="hit":
#print "HIT ANIM"
hitsfx.play()
self.anim = self.hitAnim
self.animoffset = (offset[0]-self.x,offset[1]-self.y)
self.anim.play()
if self.anim != self.blowAnim and name=="blow":
#print "BLOW ANIM"
explodesfx.play()
self.anim = self.blowAnim
self.animoffset = (offset[0]-self.x,offset[1]-self.y)
Expand All @@ -316,13 +306,11 @@ def update(self, screen, event_queue, dt, joystick):
self.blinkcount += dt

if self.blinkcount >= self.blinktime:
#print "TOGGLE BLINK"
self.blinkon = not self.blinkon
self.blinkcount = 0
self.blinks +=1
if (self.blinks == self.blinkcycles):
self.blinking = False
self.health = 100

if not(self.canfire):
self.bulcount += dt
Expand All @@ -333,16 +321,12 @@ def update(self, screen, event_queue, dt, joystick):
keys=pygame.key.get_pressed()

if keys[pygame.K_LEFT] or (joystick and (joystick.get_axis(0)<-DEADZONE or joystick.get_button(2))):
#print "left"
self.velx = -SHIP_ACC
if keys[pygame.K_RIGHT] or (joystick and (joystick.get_axis(0)>DEADZONE or joystick.get_button(3))):
#print "right"
self.velx = SHIP_ACC
if keys[pygame.K_UP] or (joystick and (joystick.get_axis(1)<-DEADZONE or joystick.get_button(0))):
#print "up"
self.vely = -SHIP_ACC
if keys[pygame.K_DOWN] or (joystick and (joystick.get_axis(1)>DEADZONE or joystick.get_button(1))):
#print "down"
self.vely = SHIP_ACC
if self.canfire and (keys[pygame.K_SPACE] or (joystick and joystick.get_button(11))):
bul = Bullet(self.x,self.y-42,BLUE,(0,-1),320,self.bullets)
Expand All @@ -355,10 +339,8 @@ def update(self, screen, event_queue, dt, joystick):
self.vely = max(self.vely, -self.health)

if not (keys[pygame.K_UP] or keys[pygame.K_DOWN] or (joystick and (math.fabs(joystick.get_axis(1))>DEADZONE or joystick.get_button(0) or joystick.get_button(1)))):
#print "dfsdfsdfs"
self.vely = 0
if not (keys[pygame.K_LEFT] or keys[pygame.K_RIGHT] or (joystick and (math.fabs(joystick.get_axis(0))>DEADZONE or joystick.get_button(2) or joystick.get_button(3)))):
#print "aaaoieuroiuo"
self.velx = 0

if self.x+(self.velx*dt)>640-48 or self.x+(self.velx*dt)<48:
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 5 additions & 22 deletions brain.py
Expand Up @@ -27,73 +27,56 @@ def __init__(self):
# create model
self.keras = Sequential()

# From what I could gather from the docs, columns came first in the input shape
# Configure the Keras Model
self.keras.add(Dense(4, input_shape=(4,), activation='relu'))
self.keras.add(Dense(6, activation='relu'))
self.keras.add(Dense(4, activation='relu'))
self.keras.add(Dense(4, activation='relu'))
self.keras.add(Dense(1, activation='sigmoid'))
self.keras.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])



# Keras version of learning
def train(self):
# Builds the model based on the dataset to this point
# Create a n * 4 matrix for the input data
#[list(item) for item in self.mapShots.it]
x = []
#y = np.empty((0))
y = []
#Step 1, build Numpy Arrays for complete data points
for k,v in self.mapShots.iteritems():
# Convert our tuple to a numpy array
if k in self.mapHits:
a = list(v)
x.append(a)
#y = np.append(y,self.mapHits[k])
y.append(self.mapHits[k])

#print x
#print y
# Fit the data to the model
self.keras.fit(x,y,epochs=150,batch_size=10)
scores = self.keras.evaluate(x, y)
print("\n%s: %.2f%%" % (self.keras.metrics_names[1], scores[1]*100))


# "Home grown" Neural Net implementation
def learn(self):
# Builds the model based on the dataset to this point
# Create a n * 4 matrix for the input data
#[list(item) for item in self.mapShots.it]
x = []
#y = np.empty((0))
y = []
#Step 1, build Numpy Arrays for complete data points
cumulative_error = 0
for k,v in self.mapShots.iteritems():
# Convert our tuple to a numpy array
if k in self.mapHits:
a = list(v)
cumulative_error += self.model.train(TrainingExample(a,self.mapHits[k]))

#print x
#print y
#print "Finished training, cumulative error was "+str(cumulative_error)
# Fit the data to the model
self.trained = True

self.trained = True

def add_shot(self, bullet, dx, dy, du, dv):
self.mapShots[bullet] = (dx, dy, du, dv)

def record_hit(self, bullet):
self.mapHits[bullet] = 1
#self.learn()

def record_miss(self, bullet):
self.mapHits[bullet] = 0
#self.learn()

def draw(self,screen):
#from keras.utils.layer_utils import print_summary
#print_summary(self.model)
self.model.draw(screen)
File renamed without changes.
23 changes: 23 additions & 0 deletions formulae.py
@@ -1,3 +1,26 @@
'''The MIT License (MIT)
Copyright (c) 2017 ActiveState Software Inc.
Copyright (c) 2015 Milo Spencer-Harper
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.'''

from numpy import exp, random
from math import atan, sin, cos
import parameters
Expand Down

0 comments on commit f689f12

Please sign in to comment.