-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshopGame.py
More file actions
250 lines (228 loc) · 7.24 KB
/
Copy pathshopGame.py
File metadata and controls
250 lines (228 loc) · 7.24 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import pygame
import time
import random
from enum import Enum
from customersOperations import Customer, generateCustomer, CustomerState
from traderOperations import *
LOT=[]
mapx=15
mapy=50
startx=-1500
starty=-100
tilemap=[]
selectedtrader=0
pygame.init()
buyrect=pygame.Rect(650,60,80,30)
screen = pygame.display.set_mode((800, 600))
mealList=['Bread', 'Potatoes', 'Waffles','Spaghetti','Soup','Pizza','Ice Cream','Sushi','Chicken','Roast Beef']
possibleFoods = mealList
Done=False
readyToServe = False
timeLastServed=time.time()
servingSpeed=1
money=100
shopImage=pygame.image.load("buildingTiles_113.png")
roadImage=pygame.image.load("roads.png")
treeImage=pygame.image.load("cityTiles_067.png")
groundImage=pygame.image.load("cityTiles_066.png")
grassImage=pygame.image.load("cityTiles_066.png")
busImage=pygame.image.load("cityTiles_002.png")
Image=pygame.image.load("cityTiles_073.png")
ourImage=pygame.image.load("buildingTiles_100.png")
carImage=pygame.image.load("carBlue2_003.png")
pygame.font.init()
myfont=pygame.font.SysFont('Times New Roman',24)
t=time.time()
imageList=[busImage,roadImage,grassImage,groundImage,treeImage,shopImage,Image,ourImage,carImage]
stock = {
"Potatoes": 5,
"Bread": 6,
'Waffles':0,
'Spaghetti': 0,
'Soup': 0,
'Pizza': 0,
'Ice Cream': 0,
'Sushi': 0,
'Chicken': 0,
'Roast Beef': 0
}
prices = {
"Potatoes": 3,
"Bread": 5,
'Waffles':0,
'Spaghetti':0,
'Soup':0,
'Pizza':0,
'Ice Cream':0,
'Sushi':0,
'Chicken':0,
'Roast Beef':0
}
amountOfads=0
rationalPrices = {
"Potatoes": 5,
"Bread": 5,
'Waffles':10,
'Spaghetti':10,
'Soup':5,
'Pizza':10,
'Ice Cream':5,
'Sushi':5,
'Chicken':10,
'Roast Beef':10
}
#sustomerState = Enum('CustomerState', 'onStreet goingToShop waiting leavingShop')
customers = []
def makeTileMap():
for i in range(mapy):
list=[]
for o in range(mapx):
if o==5:
list.append(random.randint(0,1))
else:
list.append(random.randint(2,4))
tilemap.append(list)
for i in range(0,5*2, 2):
tilemap[12+i][6]=5
tilemap[12+i+1][6]=4
tilemap[12+i+1][5]=6
tilemap[20][4]=7
makeTileMap()
for i in range(0, 10, 2):
LOT.append(generateTrader(200,i*100, possibleFoods,pygame.Rect(6*132+(12+i)*66+startx,(12+i)*33+starty-imageList[tilemap[12+i][6]].get_rect().size[1],132,127)))
print(tilemap)
def drawSlider(food, mousePos, clickedOn):
rect=pygame.Rect(5,(possibleFoods.index(food)+1)*25+160,150,10)
textsurface=myfont.render(food+" , "+str(prices[food]),False,(255,255,255))
screen.blit(textsurface,(50,(possibleFoods.index(food)+1)*25+170))
pygame.draw.rect(screen,(255,255,255),rect)
if rect.contains(pygame.Rect(mousePos[0], mousePos[1], 1, 1)) and clickedOn:
newPrice = int((mousePos[0]-5)/3)
prices[food]=newPrice
sliderect=pygame.Rect((prices[food]*3)-10,((possibleFoods.index(food)+1)*25)+155,20,20)
pygame.draw.rect(screen,(0, 150, 50),sliderect)
'''
=======================================
=======================================
draw map
=======================================
=======================================
'''
def drawmapI():
for i in range(mapy):
for o in range(mapx):
screen.blit(imageList[tilemap[i][o]],(o*132+i*66+startx,i*33+starty-imageList[tilemap[i][o]].get_rect().size[1]))
def drawmap():
for i in range(mapy):
for o in range(0,12):
screen.blit(imageList[tilemap[i][o]],(o*132+i*66+startx,i*33+starty-imageList[tilemap[i][o]].get_rect().size[1]))
def moveCustomers():
#print("attempting to move",)
global money
global timeLastServed
for c in customers:
if c.state == CustomerState.onStreet:
c.y += 2.5
c.x += 5
elif c.state == CustomerState.goingToShop:
c.x += 5
c.y += 2.5
elif c.state==CustomerState.waiting:
#print(time.time()-timeLastServed)
if time.time()-timeLastServed>=servingSpeed:
timeLastServed=time.time()
c.state=CustomerState.leavingShop
if stock[c.desiredMeal] >0 and c.budget>prices[c.desiredMeal] :
amountOverpriced = (prices[c.desiredMeal]-rationalPrices[c.desiredMeal])*2.5
if random.randrange(0,100 + amountOfads)>amountOverpriced:
money+=prices[c.desiredMeal]
stock[c.desiredMeal] -= 1
if c.state == CustomerState.leavingShop:
c.y += 2.5
c.x += 5
#or i in customers:
#print(i)
customerSpawnTime=time.time()
selectedtrader=LOT[0]
checkTiming = False
'''
=======================================
=======================================
while loop
=======================================
=======================================
'''
drawmapI()
while not Done:
money-=(time.time() - t)/4
t = time.time()
drawmapI()
mousePos=pygame.mouse.get_pos()
clicked=False
if checkTiming:
t2 = time.time()
print("map drawing took " + str(t2 - t) + 'seconds')
for event in pygame.event.get():
if event.type== pygame.KEYDOWN:
selectedtrader.handleKeypress(event)
if event.type == pygame.QUIT:
done = True
if event.type==pygame.MOUSEBUTTONDOWN:
clicked=True
mousePos=pygame.mouse.get_pos()
if checkTiming:
t3 = time.time()
print("Event Handling took " + str(t3 - t2) + 'seconds')
mouseRect=pygame.Rect(mousePos[0],mousePos[1],1,1)
if buyrect.contains(mouseRect) and clicked and money> selectedtrader.prices[selectedtrader.item]:
if not selectedtrader.item=='Ad':
stock[selectedtrader.item] += 1
print("buy"+selectedtrader.item)
money-=selectedtrader.prices[selectedtrader.item]
else:
amountOfads+=20
money-=selectedtrader.prices[selectedtrader.item]
for i in range(len(LOT)):
if LOT[i].rect.contains(pygame.Rect(mousePos[0], mousePos[1], 1, 1)) and clicked:
selectedtrader=LOT[i]
trade(selectedtrader,screen)
if checkTiming:
t4=time.time()
print("trader Handling took " + str(t4 - t3) + 'seconds')
if time.time()-customerSpawnTime>=random.randint(1,3):
customers.append(generateCustomer(possibleFoods))
customerSpawnTime=time.time()
moveCustomers()
for i in customers:
if i.y==360 and i.desiredMeal in mealList and i.state == CustomerState.onStreet and i.budget>=prices[i.desiredMeal] and stock[i.desiredMeal]>0 and prices[i.desiredMeal]:
amountOverpriced = (prices[i.desiredMeal]-rationalPrices[i.desiredMeal])*2.5
if random.randrange(0,100)>amountOverpriced:
#print("yay! a customer entered with the meal of: " + i.desiredMeal)
i.state = CustomerState.goingToShop
if i.x>=600 and i.state==CustomerState.goingToShop:
print("h")
i.state = CustomerState.waiting
screen.blit(carImage,(i.x,i.y))
if checkTiming:
t5=time.time()
print("trader Handling took " + str(t5 - t4) + 'seconds')
#rect = pygame.Rect(i.x,i.y,10,10)
#pygame.draw.rect(screen, (0, 0,255 - possibleFoods.index(i.desiredMeal)*(255/len(possibleFoods))), rect)
# print("")
for meal in mealList:
drawSlider(meal,mousePos,clicked)
textsurface=myfont.render("Money: "+str(round(money, 2)),False,(255,255,255))
screen.blit(textsurface,(5,5))
for i in range(len(mealList)):
textsurface=myfont.render(mealList[i]+str(stock[mealList[i]]),False,(255,255,255))
screen.blit(textsurface,(5,(i+1)*15))
textsurface=myfont.render('Amount of ads:'+str(amountOfads),False,(255,255,255))
screen.blit(textsurface,(5,165))
pygame.display.flip()
if money<=0:
print("you loser, you just lost again!!!!!")
Done=True
if checkTiming:
t6=time.time()
print("Meal Handling took " + str(t6 - t5) + 'seconds')
print("One loop took " + str( time.time() - t) + 'seconds')