-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgameObjects.py
93 lines (75 loc) · 3.19 KB
/
gameObjects.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
90
91
92
93
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 5 16:11:49 2018
@author: Alverino
"""
import cv2
import numpy
GRAVITY = 4
NUMFRUITS = 3
FONT = cv2.FONT_HERSHEY_COMPLEX
FONT_SIZE = 0.7
# These determine the positions to generate "exploded" bits of the fruit
EXX = [1, 0.70711, 0, -0.70711, -1, -0.70711, 0, 0.70711]
EXY = [0, 0.70711, 1, 0.70711, 0, -0.70711, -1, -0.70711]
EXV = 8
class Fruit:
def __init__(this, radius, color, xPos, yPos, xVel, yVel):
this.radius = radius
this.color = color
this.xPos = xPos
this.yPos = yPos
this.xVel = xVel
this.yVel = yVel
def draw(this, frame):
cv2.circle(frame, (this.xPos, this.yPos), this.radius, this.color, -1)
def doPhysics(this, xBound, yBound):
this.xPos += this.xVel
this.yPos += this.yVel
this.yVel += GRAVITY
return this.xPos >= 0 and this.xPos <= xBound and this.yPos <= yBound
def explode(this, explosions):
exRad = int(this.radius/2)
explosions.append(Fruit(exRad, this.color, this.xPos, this.yPos, this.xVel, this.yVel))
for i in range(8):
explosions.append(Fruit(exRad, this.color, this.xPos + int(EXX[i]*exRad), this.yPos + int(EXY[i]*exRad), this.xVel + int(EXX[i]*EXV), this.yVel + int(EXY[i]*EXV)))
def isAbove(this, yVal):
return this.yPos < yVal
def intersects(this, fingerTip):
return (abs(fingerTip[0] - this.yPos) <= this.radius) and (abs(fingerTip[1] - this.xPos) <= this.radius)
class Bomb(Fruit):
def __init__(this, radius, color, xPos, yPos, xVel, yVel):
Fruit.__init__(this, radius, color, xPos, yPos, xVel, yVel)
def draw(this, frame):
cv2.rectangle(frame, (this.xPos - this.radius, this.yPos - this.radius), (this.xPos + this.radius, this.yPos + this.radius), this.color, -1)
cv2.putText(frame, "bomb",(this.xPos - this.radius, this.yPos - this.radius), FONT, FONT_SIZE, (255, 255, 255), 2, cv2.LINE_AA)
class Text:
def __init__(this, pos, color, text):
this.pos = pos
this.color = color
this.text = text
def write(this, frame):
cv2.putText(frame, this.text,this.pos, FONT, FONT_SIZE, this.color, 2, cv2.LINE_AA)
class Level:
def __init__(this, numFruits, pointsPerFruit, numBombs, numFrames):
this.numFruits = numFruits
this.pointsPerFruit = pointsPerFruit
this.numBombs = numBombs
this.numFrames = numFrames
def randomFruit(x0, y0):
# Generates reasonable random values for the initialization of each fruit
radius = numpy.random.randint(30, 70)
color = (numpy.random.randint(255), numpy.random.randint(255), numpy.random.randint(255))
xPos = numpy.random.randint(x0)
yPos = y0
xVel = int(((x0/2)-xPos)//10)
yVel = -numpy.random.randint(y0//20, y0//10)
return Fruit(radius, color, xPos, yPos, xVel, yVel)
def randomBomb(x0, y0):
# Generates reasonable random values for the initialization of each fruit
xPos = numpy.random.randint(x0)
yPos = y0
xVel = int(((x0/2)-xPos)//10)
yVel = -numpy.random.randint(y0//20, y0//10)
return Bomb(50, (0, 0, 0), xPos, yPos, xVel, yVel)