-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
194 lines (155 loc) · 6.83 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import pygame, sys
from player import Player
from balloons import GaussianBalloons, UniformBalloons, LimitBalloons, GeometricBalloons
import argparse
# from datetime import date
from pygame.locals import *
from numpy import random
# TODO: start button on game screen
parser = argparse.ArgumentParser()
parser.add_argument("--name", default= "PENROLINE", required=True)
parser.add_argument("--gender", default = "IDK", required=True)
parser.add_argument("--age", type=int, default = 20, required=True)
parser.add_argument("--balloons", type=int, default = 10, required=True)
parser.add_argument("--course", type=int, default = 6, required=True)
parser.add_argument("--exp", type=int, required=False)
parser.add_argument("--lossAversion", default = "False", required=True)
parser.add_argument("--dist", default = None, required=False)
parser.add_argument("--obs", default = None, required=False)
parser.add_argument("--seenGraphs", default=False, required=False)
args = parser.parse_args()
exp = int(args.exp)
#distirbutions and number of variables
DISTS = ["GAUSSIAN", "UNIFORM", "GEOMETRIC", "LIMIT"]
N = int(args.balloons)
if(args.dist != None):
diststring = args.dist.split()
print(diststring)
distribution = diststring[0]
N = int(diststring[1])
if distribution == 'GAUSSIAN':
balloons = GaussianBalloons(N, int(diststring[2]), int(diststring[3]))
elif distribution == 'UNIFORM':
balloons = UniformBalloons(N, int(diststring[2]), int(diststring[3]))
elif distribution == 'LIMIT':
balloons = LimitBalloons(N, int(diststring[2]))
elif distribution == 'GEOMETRIC':
balloons = GeometricBalloons(N, int(diststring[2]))
else:
raise Exception("no distribution found ;-;???")
if(args.obs != None):
obs = args.obs.split(',')
obs = [int(i) for i in obs]
if(len(obs) != N):
raise Exception("observation size not equal to N!")
balloons.setBalloons(obs)
else:
distribution = random.choice(DISTS)
balloons = None
if distribution == 'GAUSSIAN':
balloons = GaussianBalloons(N)
elif distribution == 'UNIFORM':
balloons = UniformBalloons(N)
elif distribution == 'LIMIT':
balloons = LimitBalloons(N)
elif distribution == 'GEOMETRIC':
balloons = GeometricBalloons(N)
else:
raise Exception("no distribution found ;-;???")
B = balloons.getBallons()
print(B)
player = Player(args.name, args.age, args.gender, args.course, balloons, args.lossAversion, args.exp, args.seenGraphs)
# Game Part
pygame.init()
SCREEN_HEIGHT = 800
SCREEN_WIDTH = 600
BALLOON_SIZE = 20
DISPLAYSURF = pygame.display.set_mode((SCREEN_HEIGHT, SCREEN_WIDTH))
pygame.display.set_caption('Balloon Game!')
total_score = 0
currBalloonIdx = 0
numberBalloons = balloons.N
curr_pumps = 0 # aka player score
lastKeyPressed = 0
timeDelay = 2000 # wait 0.2 seconds between key presses
BLUE = (0, 0, 255)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0,0,0)
# https://www.geeksforgeeks.org/python-display-text-to-pygame-window/
lastTimePressed = pygame.time.get_ticks()
# add START condition here
lastTimePressed = pygame.time.get_ticks()
while currBalloonIdx < numberBalloons: # main game loop
if currBalloonIdx % 2 == 0: # have color alternate between conseq. balloons
BALLOON_COLOR = BLUE
else:
BALLOON_COLOR = RED
max_pumps = B[currBalloonIdx]
font = pygame.font.Font('freesansbold.ttf', 32)
# score text
currBalloonTxt = font.render('On Balloon #: ', True, RED, WHITE)
currBalloonRect = currBalloonTxt.get_rect()
currBalloonNumber = font.render(str(currBalloonIdx+1), True, BALLOON_COLOR, WHITE)
currNumberRect = currBalloonNumber.get_rect()
currNumberRect.center = (1.15 * SCREEN_WIDTH / 3, SCREEN_HEIGHT / 50)
currScoreTxt = font.render('Pumps: $' + str(curr_pumps), True, BLUE, WHITE)
currScoreRect = currScoreTxt.get_rect()
currScoreRect.center = (1.7 * SCREEN_WIDTH / 3, SCREEN_HEIGHT / 50)
if args.lossAversion.lower() == "false":
totalPointsDisplayed = total_score
else:
totalPointsDisplayed = total_score+curr_pumps
totalScoreTxt = font.render('Total Earned: $' + str(totalPointsDisplayed), True, RED, WHITE)
totalScoreRect = totalScoreTxt.get_rect()
totalScoreRect.center = (8.75 * SCREEN_WIDTH / 9, SCREEN_HEIGHT / 50)
# instruction text
upKeyText = font.render('Press X to Collect $', True, BLACK, WHITE)
upKeyRect = upKeyText.get_rect()
rightKeyText = font.render('Press space to pump', True, BLACK, WHITE)
rightKeyRect = rightKeyText.get_rect()
rightKeyRect.center = (15*SCREEN_WIDTH/50, SCREEN_HEIGHT / 10)
upKeyRect.center = (7 * SCREEN_WIDTH / 8, SCREEN_HEIGHT / 10)
for event in pygame.event.get():
currKeyPressed = pygame.time.get_ticks()
if event.type == QUIT:
pygame.quit()
# record data
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: # INC BALLOON SIZE
curr_pumps += 1
print("MAX PUMPS")
print(max_pumps)
print("CURR BALLOON")
print(currBalloonIdx)
print("CURR PUMPS")
print(curr_pumps)
if curr_pumps == max_pumps: # BALLOON POPS
player.addActionData(currBalloonIdx, curr_pumps, "POP", currKeyPressed-lastKeyPressed, totalPointsDisplayed)
curr_pumps = 0 # reset current score
BALLOON_SIZE = 20 # reset to initial size
currBalloonIdx+=1
else: # PUMP BALLOON
BALLOON_SIZE += 5
player.addActionData(currBalloonIdx, curr_pumps, "PUMP", currKeyPressed-lastKeyPressed, totalPointsDisplayed)
lastKeyPressed = currKeyPressed # update logics
elif event.type == pygame.KEYDOWN and event.key == pygame.K_x: # NEXT BALLOON
player.addActionData(currBalloonIdx, curr_pumps, "PASS", currKeyPressed-lastKeyPressed, totalPointsDisplayed)
lastKeyPressed = pygame.time.get_ticks()
total_score += curr_pumps
curr_pumps = 0
BALLOON_SIZE = 20 # reset to initial size
currBalloonIdx+=1
lastKeyPressed = currKeyPressed # update logics
DISPLAYSURF.fill(WHITE) # white background
DISPLAYSURF.blit(currBalloonTxt, currBalloonRect)
DISPLAYSURF.blit(currBalloonNumber, currNumberRect)
DISPLAYSURF.blit(currScoreTxt, currScoreRect)
DISPLAYSURF.blit(totalScoreTxt, totalScoreRect)
DISPLAYSURF.blit(rightKeyText, rightKeyRect)
DISPLAYSURF.blit(upKeyText, upKeyRect)
pygame.draw.circle(DISPLAYSURF, BALLOON_COLOR, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2), BALLOON_SIZE)
pygame.display.update()
player.addDistributionData()
player.writeData()
player.writeStringToData("TOTAL SCORE {}".format(total_score))