-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
88 lines (75 loc) · 2.26 KB
/
game.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
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 2 19:06:49 2018
@author: Vassilis
"""
import constants as c
import pygame as pg
import playFish
import groupObjet
import food
import random as rand
import aiFish
class Game:
def __init__(self):
pg.init()
self.display = pg.display.set_mode((c.WIDTH,c.HEIGHT))
self.background = (0,0,100)
self.display.fill(self.background)
self.running =True
self.clock = pg.time.Clock()
self.playfish = playFish.playFish((20,20))
self.foods = groupObjet.GroupObjet()
self.foodQty = 10
self.aifish = aiFish.AIFish((160,200))
self.fishes = groupObjet.GroupObjet()
self.fishes.add(self.playfish)
self.fishes.add(self.aifish)
for i in range(self.foodQty):
self.foods.add(food.Food(rand.randrange(0,c.WIDTH),rand.randrange(0,c.HEIGHT)))
def cleanup(self):
pg.quit()
def events(self):
for ev in pg.event.get():
if ev.type == pg.QUIT:
self.running = False
pressed = pg.key.get_pressed()
noy = not(pressed[pg.K_UP] or pressed[pg.K_DOWN])
nox = not(pressed[pg.K_LEFT] or pressed[pg.K_RIGHT])
if(nox):
self.playfish.move(c.RIGHT,False)
if(noy):
self.playfish.move(c.UP,False)
if(not(noy and nox)):
if(pressed[pg.K_UP] and not pressed[pg.K_DOWN]):
self.playfish.move(c.UP,True)
elif(pressed[pg.K_DOWN] and not pressed[pg.K_UP]):
self.playfish.move(c.DOWN,True)
if(pressed[pg.K_LEFT] and not pressed[pg.K_RIGHT]):
self.playfish.move(c.LEFT,True)
elif(pressed[pg.K_RIGHT] and not pressed[pg.K_LEFT]):
self.playfish.move(c.RIGHT,True)
def run(self):
while(self.running):
self.events()
self.clock.tick(c.FPS)
self.update(1./c.FPS)
self.draw()
pg.display.flip()
self.cleanup()
def update(self,dt):
self.playfish.update(dt)
self.aifish.update(dt,self.foods)
for fish in self.fishes:
eat = pg.sprite.spritecollide(fish,self.foods,True)
for dude in eat:
fish.eat()
nbFood = len(self.foods.sprites())
if(nbFood<self.foodQty):
for i in range(self.foodQty-nbFood):
self.foods.add(food.Food(rand.randrange(0,c.WIDTH),rand.randrange(0,c.HEIGHT)))
def draw(self):
self.display.fill(self.background) #TO be changed with draw(background or something)
self.playfish.draw(self.display)
self.aifish.draw(self.display)
self.foods.draw(self.display)