-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiFish.py
89 lines (74 loc) · 2.31 KB
/
aiFish.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 15:39:16 2018
@author: Vassilis
"""
import pygame as pg
import fish
import constants as c
import food
from Vect2D import Vect2D as v
import random as rand
import spriteAnim as spri
import os
class AIFish(fish.Fish):
def __init__(self,pos_or_x = (c.WIDTH/2,c.HEIGHT/2),y = None,*groups):
super().__init__(pos_or_x,y,groups)
self._sightr = 60
self._wallDist = 60
self._stateCirc = {}
tempspri = spri.SpriteAnim(0)
tempspri.loadAll(os.path.join("Graphics","circle"),"greenCircle.png")
self._stateCirc["green"] = tempspri
tempspri = spri.SpriteAnim(0)
tempspri.loadAll(os.path.join("Graphics","circle"),"redCircle.png")
self._stateCirc["red"] = tempspri
self._circ = "red"
def update(self,dt, environ):
self._move(environ)
super().update(dt)
def draw(self, screen):
super().draw(screen)
circle = pg.transform.scale(self._stateCirc[self._circ].findCurrentImage(self._nbframes),(self._sightr*2,self._sightr*2))
therect = circle.get_rect()
therect.center = self.pos.vec
screen.blit(circle,therect)
def _move(self,environ):
"""Sees the surroundings and chooses to move"""
closefood = False
for thing in environ :
if(isinstance(thing, food.Food)):
diff = thing.pos-self.pos
if(diff.r<self._sightr):
self._push = diff
closefood = True
self._circ = "green"
if(not closefood):
self._circ = "red"
if(self._push!=v.Vect2D(0,0)):
self._chooseDir(self._checkSides())
else:
self._push = v.Vect2D(1,0)
def _checkSides(self):
"""Returns None if no collision, allowed angle interval otherwise"""
interval = None
if(self.pos.x+self._wallDist>c.WIDTH):
interval = (90,270)
elif(self.pos.x-self._wallDist<0):
interval = (-90,90)
if(self.pos.y+self._wallDist>c.HEIGHT):
interval = (-180,0)
elif(self.pos.y-self._wallDist<0):
interval = (0,180)
return interval
def _chooseDir(self,interval = None):
"""Choose a "random" direction. If constrained is True, interval should be a tuple
containing two angles from which to pick the new direction."""
changer = 120
if(self._nbframes%changer == 0):
if(interval is None):
self._push.phi += (rand.random()-0.5)*180
else :
self._push.phi += (rand.random()-0.5)*2
if(interval is not None):
self._push.phi = rand.randrange(interval[0],interval[1])