Skip to content

Commit 2cd8eef

Browse files
area_selection_changes
1 parent 1d8d790 commit 2cd8eef

File tree

5 files changed

+75
-82
lines changed

5 files changed

+75
-82
lines changed

images/europe_map.png

6.18 KB
Loading

images/selection_area.png

9.48 KB
Loading

images/selection_area_attack.png

13.4 KB
Loading

images/selection_area_selected.png

12.5 KB
Loading

init.py

Lines changed: 75 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -134,63 +134,60 @@ def get_player_color(game_type, player):
134134

135135

136136
class Player:
137-
def __init__(self, name="player1", game_type="singleplayer"):
137+
def __init__(self, name="player1", player_type="bot", game_type="singleplayer"):
138138
self.name = name
139139
self.display_name = name[:1].upper() + name[1:] # Change name to uppercase first letter
140140
self.color = get_player_color(game_type, name)
141-
141+
self.player_type = player_type
142+
142143
def turn(self, game):
143-
bot_turn = True
144-
bot_selectable_areas = []
145-
for area in range(len(game.continent.areas)):
146-
if game.continent.areas[area].owner == self.name:
147-
bot_selectable_areas.append(game.continent.areas[area])
148-
if bot_selectable_areas:
149-
game.bot_selected_area = bot_selectable_areas[0]
150-
else:
151-
bot_turn = False
152-
bot_attackbles = []
153-
for area in range(len(game.continent.areas)):
154-
if game.continent.areas[area].owner == "player1" or game.continent.areas[area].owner == "":
155-
bot_attack = game.continent.areas[area]
156-
break
157-
bot_attack_index = 0
158-
for area in range(len(game.continent.areas)):
159-
if game.continent.areas[area].owner == "player1" or game.continent.areas[area].owner == "":
160-
if game.continent.areas[area].count < bot_attack.count:
161-
bot_attack = game.continent.areas[area]
162-
bot_attack_index = area
163-
while bot_turn is True and game.bot_selected_area.count > bot_attack.count * 5:
164-
print(self.display_name + " Attacking: " + game.continent.areas[bot_attack_index].name + ", From: " + game.bot_selected_area.name)
165-
bot_turn, game.continent.areas[bot_attack_index], game.bot_selected_area, has_succeded = game.attack(game.continent.areas[bot_attack_index], game.bot_selected_area)
166-
bot_selectable_areas.remove(game.bot_selected_area)
144+
if self.player_type == "bot":
145+
bot_turn = True
146+
bot_selectable_areas = []
147+
for area in range(len(game.continent.areas)):
148+
if game.continent.areas[area].owner == self.name:
149+
bot_selectable_areas.append(game.continent.areas[area])
167150
if bot_selectable_areas:
168151
game.bot_selected_area = bot_selectable_areas[0]
169152
else:
170153
bot_turn = False
171-
return game.continent.areas
172-
154+
bot_attackbles = []
155+
for area in range(len(game.continent.areas)):
156+
if game.continent.areas[area].owner == "player1" or game.continent.areas[area].owner == "":
157+
bot_attack = game.continent.areas[area]
158+
break
159+
bot_attack_index = 0
160+
for area in range(len(game.continent.areas)):
161+
if game.continent.areas[area].owner == "player1" or game.continent.areas[area].owner == "":
162+
if game.continent.areas[area].count < bot_attack.count:
163+
bot_attack = game.continent.areas[area]
164+
bot_attack_index = area
165+
while bot_turn is True and game.bot_selected_area.count > bot_attack.count * 4: #1 less than the loss level (5 - 1 = 4) this little bot takes chances
166+
print(self.display_name + " Attacking: " + game.continent.areas[bot_attack_index].name + ", From: " + game.bot_selected_area.name)
167+
bot_turn, game.continent.areas[bot_attack_index], game.bot_selected_area, has_succeded = game.attack(game.continent.areas[bot_attack_index], game.bot_selected_area)
168+
bot_selectable_areas.remove(game.bot_selected_area)
169+
if bot_selectable_areas:
170+
game.bot_selected_area = bot_selectable_areas[0]
171+
else:
172+
bot_turn = False
173+
return game.continent.areas
174+
elif self.player_type == "player":
175+
return game.continent.areas
176+
else:
177+
return game.continent.areas # just return, cannot pass because requires returned values
173178

174179
class Area:
175180
def __init__(self, name="", area_pos=(0, 0), offset=(0, 0)):
176181
self.name = name
177182
self.display_name = name[:1].upper() + name[1:] # Change name to uppercase first letter
178-
self.image = pygame.image.load("images/area_" + name + ".png") # ex: images/area_england.png
183+
self.image = pygame.image.load("images/selection_area.png")
179184
self.rect = self.image.get_rect()
180185
self.rect.center = area_pos
181186
self.owner = ""
182187
self.count = 0
183188
self.count_pos_offset = offset
184189

185190

186-
def add_player(name, ptype):
187-
players[name] = Player(name, ptype)
188-
189-
190-
def remove_player(name):
191-
players[name] = None
192-
193-
194191
"""
195192
This is Where you want to go if you want to mod maps
196193
@@ -206,13 +203,13 @@ class Europe:
206203
color = blue
207204

208205

209-
Europe.areas.append(Area("england", (DisplayParams.center[0] - DisplayParams.center[0] / 2.25, DisplayParams.center[1] - DisplayParams.center[0] / 5.5), (0, 24)))
210-
Europe.areas.append(Area("franconia", (DisplayParams.center[0] - DisplayParams.center[0] / 2.83, DisplayParams.center[1] + DisplayParams.center[0] / 9.9), (0, -32)))
211-
Europe.areas.append(Area("sweden", (DisplayParams.center[0] - DisplayParams.center[0] / 8.6, DisplayParams.center[1] - DisplayParams.center[0] / 2.92), (0, 48)))
212-
Europe.areas.append(Area("spain", (DisplayParams.center[0] - DisplayParams.center[0] / 2.05, DisplayParams.center[1] + DisplayParams.center[0] / 3.25), (0, -48)))
213-
Europe.areas.append(Area("moscovy", (DisplayParams.center[0] + DisplayParams.center[0] / 3.15, DisplayParams.center[1] - DisplayParams.center[0] / 5.48), (0, 80)))
214-
Europe.areas.append(Area("germana", (DisplayParams.center[0] - DisplayParams.center[0] / 7.5, DisplayParams.center[1] + DisplayParams.center[0] / 20), (0, 48)))
215-
Europe.areas.append(Area("ottoman", (DisplayParams.center[0] + DisplayParams.center[0] / 3.23, DisplayParams.center[1] + DisplayParams.center[0] / 4.8), (0, -48)))
206+
Europe.areas.append(Area("england", (DisplayParams.center[0] - DisplayParams.center[0] / 2.25, DisplayParams.center[1] - DisplayParams.center[0] / 5.5), (0, 0)))
207+
Europe.areas.append(Area("franconia", (DisplayParams.center[0] - DisplayParams.center[0] / 2.83, DisplayParams.center[1] + DisplayParams.center[0] / 9.9), (0, 0)))
208+
Europe.areas.append(Area("sweden", (DisplayParams.center[0] - DisplayParams.center[0] / 8.6, DisplayParams.center[1] - DisplayParams.center[0] / 2.3), (0, 0)))
209+
Europe.areas.append(Area("spain", (DisplayParams.center[0] - DisplayParams.center[0] / 2.05, DisplayParams.center[1] + DisplayParams.center[0] / 2.65), (0, 0)))
210+
Europe.areas.append(Area("moscovy", (DisplayParams.center[0] + DisplayParams.center[0] / 3.15, DisplayParams.center[1] - DisplayParams.center[0] / 2.6), (0, 0)))
211+
Europe.areas.append(Area("germana", (DisplayParams.center[0] - DisplayParams.center[0] / 7.5, DisplayParams.center[1] + DisplayParams.center[0] / 8.5), (0, 0)))
212+
Europe.areas.append(Area("ottoman", (DisplayParams.center[0] + DisplayParams.center[0] / 7.2, DisplayParams.center[1] + DisplayParams.center[0] / 4.2), (0, 0)))
216213

217214
continents = [Europe]
218215

@@ -258,30 +255,29 @@ def __init__(self, name="", maxturns=1000):
258255
self.background_image = pygame.image.load("images/ocean.png")
259256
self.background_rect = self.background_image.get_rect()
260257
self.background_rect.center = DisplayParams.center
258+
self.continent_image = pygame.image.load("images/europe_map.png")
259+
self.continent_rect = self.continent_image.get_rect()
260+
self.continent_rect.center = DisplayParams.center
261261
self.selected_area = None
262262
self.HUD = HUD()
263263

264264
def attack(self, attack_area, selected_area):
265265
has_conquered = False
266266
if attack_area.owner == selected_area.owner:
267267
self.HUD = log_action(self, "Cannot attack self")
268-
print("Cannot attack own area")
269268
return not has_conquered, attack_area, selected_area, has_conquered
270269
while has_conquered is False:
271270
if selected_area.count < 2:
272-
self.HUD = log_action(self, "Not enough troops")
273-
print("Not enough troops in selected area, skipping.")
271+
self.HUD = log_action(self, "Not enough troops, skipping")
274272
return not has_conquered, attack_area, selected_area, has_conquered
275273
match self.name:
276274
case "conquest_classic":
277275
lose_win = randint(0, 15)
278276
if lose_win < 5:
279277
self.HUD = log_action(self, "Defender Lost")
280-
print("Attacked area lost 1 troop")
281278
attack_area.count -= 1
282279
else:
283280
self.HUD = log_action(self, "Attacker Lost")
284-
print("Selected area lost 1 troop")
285281
selected_area.count -= 1
286282
case "conquest_mission":
287283
lose_win = randint(0, 11)
@@ -300,24 +296,21 @@ def attack(self, attack_area, selected_area):
300296
if attack_area.count < 1 and selected_area.count > 1:
301297
if selected_area.owner == "player1":
302298
has_conquered = True
303-
self.HUD = log_action(self, "Player1 conquered another area")
304-
print("Player1 has defeated the opposing territory")
299+
self.HUD = log_action(self, "Player1 conquered an area")
305300
attack_area.owner = "player1"
306301
attack_area.count = 0 # reset the count
307302
attack_area.count = -1 + selected_area.count # move all except one troop into invaded territory
308303
selected_area.count -= selected_area.count - 1 # leave one troop
309304
elif selected_area.owner == "bot1":
310305
has_conquered = True
311-
self.HUD = log_action(self, "Bot1 conquered another area")
312-
print("Bot1 has defeated the opposing territory")
306+
self.HUD = log_action(self, "Bot1 conquered an area")
313307
attack_area.owner = "bot1"
314308
attack_area.count = 0 # reset the count
315309
attack_area.count = -1 + selected_area.count # move all except one troop into invaded territory
316310
selected_area.count -= selected_area.count - 1 # leave one troop
317311
elif attack_area.owner == "":
318312
has_conquered = True
319-
self.HUD = log_action(self, selected_area.owner[:1].upper() + selected_area.owner[1:] + " claimed a new area")
320-
print(selected_area.owner[:1].upper() + selected_area.owner[1:] + " has claimed a territory")
313+
self.HUD = log_action(self, selected_area.owner[:1].upper() + selected_area.owner[1:] + " claimed an area")
321314
attack_area.owner = selected_area.owner
322315
attack_area.count = 0 # reset the count
323316
attack_area.count = -1 + selected_area.count # move all except one troop into invaded territory
@@ -344,22 +337,22 @@ def start_game(game_type):
344337
def display_screen(game):
345338
screen.fill(black)
346339
screen.blit(game.background_image, game.background_rect)
340+
screen.blit(game.continent_image, game.continent_rect)
347341
for area in range(len(game.continent.areas)):
348-
font = pygame.font.Font(None, 48)
342+
font = pygame.font.Font(None, 64)
349343
count_image = font.render(str(game.continent.areas[area].count), False, game.players[game.continent.areas[area].owner].color)
350344
count_rect = count_image.get_rect()
351-
count_rect.center = (game.continent.areas[area].rect.center[0] - game.continent.areas[area].count_pos_offset[0],
352-
game.continent.areas[area].rect.center[1] - game.continent.areas[area].count_pos_offset[1])
345+
count_rect.center = (game.continent.areas[area].rect.center[0] - game.continent.areas[area].count_pos_offset[0], game.continent.areas[area].rect.center[1] - game.continent.areas[area].count_pos_offset[1])
353346
screen.blit(game.continent.areas[area].image, game.continent.areas[area].rect)
354347
screen.blit(count_image, count_rect)
355-
screen.blit(game.HUD.hudbar_image, game.HUD.hudbar_top_rect)
356-
screen.blit(game.HUD.hudbar_image, game.HUD.hudbar_bottom_rect)
357-
screen.blit(game.HUD.end_turn_image, game.HUD.end_turn_rect)
358-
screen.blit(game.HUD.select_image, game.HUD.select_rect)
359-
screen.blit(game.HUD.turn_play_image, game.HUD.turn_play_rect)
360-
screen.blit(game.HUD.game_name_image, game.HUD.game_name_rect)
361-
screen.blit(game.HUD.info_image, game.HUD.info_rect)
362-
screen.blit(game.HUD.play_name_image, game.HUD.play_name_rect)
348+
screen.blit(game.HUD.hudbar_image, game.HUD.hudbar_top_rect)
349+
screen.blit(game.HUD.hudbar_image, game.HUD.hudbar_bottom_rect)
350+
screen.blit(game.HUD.end_turn_image, game.HUD.end_turn_rect)
351+
screen.blit(game.HUD.select_image, game.HUD.select_rect)
352+
screen.blit(game.HUD.turn_play_image, game.HUD.turn_play_rect)
353+
screen.blit(game.HUD.game_name_image, game.HUD.game_name_rect)
354+
screen.blit(game.HUD.info_image, game.HUD.info_rect)
355+
screen.blit(game.HUD.play_name_image, game.HUD.play_name_rect)
363356
pygame.display.flip()
364357

365358
def log_action(game, msg):
@@ -372,7 +365,7 @@ def log_action(game, msg):
372365
def play_game_classic():
373366
play_track("music/play.wav", 0.5)
374367
game = Game("conquest_classic")
375-
game.players = {"player1": Player("player1", "singleplayer"), "bot1": Player("bot1", "singleplayer"), "": Player("gaia1", "singleplayer")}
368+
game.players = {"player1": Player("player1", "player"), "bot1": Player("bot1", "bot"), "": Player("gaia1", "unclaimed")}
376369
game.players["player1"].color = blue
377370
game.players["bot1"].color = red
378371
game.players[""].color = white
@@ -381,12 +374,11 @@ def play_game_classic():
381374
game.continent.areas[area].count = 0
382375
random_area = randint(0, len(game.continent.areas) - 1)
383376
game.continent.areas[random_area].owner = "player1"
384-
game.continent.areas[random_area].count = 5
385-
print("Player1 home area is: " + game.continent.areas[random_area].name)
377+
game.continent.areas[random_area].count = 10
386378
player_home = random_area
387379
bot_home = random_area - 1
388380
game.continent.areas[random_area - 1].owner = "bot1"
389-
game.continent.areas[random_area - 1].count = 5
381+
game.continent.areas[random_area - 1].count = 10
390382
game.bot_selected_area = game.continent.areas[random_area - 1]
391383
turns = 0
392384
has_quit = False
@@ -412,46 +404,47 @@ def play_game_classic():
412404
if game.continent.areas[area].rect.collidepoint(pos[0], pos[1]):
413405
if game.continent.areas[area].owner == "player1":
414406
game.selected_area = game.continent.areas[area]
407+
game.selected_area.image = pygame.image.load("images/selection_area_selected.png")
415408
game.HUD.select_image = font.render("Selected: " + game.continent.areas[area].display_name, False, black)
416409
game.HUD.select_rect = game.HUD.select_image.get_rect()
417410
game.HUD.select_rect.center = (DisplayParams.center[0] - (DisplayParams.center[0] - DisplayParams.size[0] / 8), DisplayParams.size[1] - 24)
418-
print("Player1 selecting area: " + game.continent.areas[area].name)
419411
elif game.continent.areas[area].owner == "bot1" or game.continent.areas[area].owner == "":
420412
if game.selected_area is None:
421-
print("No Area selected, cannot attack")
422413
game.HUD = log_action(game, "No selection")
423414
else:
424415
print("Player1 attacking area: " + game.continent.areas[area].name + ", From: " + game.selected_area.name)
425416
turn, game.continent.areas[area], game.selected_area, succeded = game.attack(game.continent.areas[area], game.selected_area)
426417
if game.HUD.end_turn_rect.collidepoint(pos[0], pos[1]):
427418
turn = False
428-
break
429419
bot_areas = 0
420+
player_areas = 0
430421
bot_owned_areas = []
431422
for area in range(len(game.continent.areas)):
432423
if game.continent.areas[area].owner == "bot1":
433424
bot_areas += 1
434425
bot_owned_areas.append(game.continent.areas[area])
426+
elif game.continent.areas[area].owner == "player1":
427+
player_areas += 1
435428
if bot_areas <= 0:
436429
has_won = True
437430
display_screen(game)
438-
game.HUD = log_action(game, "Player1 has won")
439-
print("Player1 has beat bot1")
431+
game.HUD = log_action(game, "Bot1 has lost")
440432
break
441-
player_areas = 0
442-
for area in range(len(game.continent.areas)):
443-
if game.continent.areas[area].owner == "player1":
444-
player_areas += 1
445433
if player_areas <= 0:
446434
has_lost = True
447435
display_screen(game)
448436
game.HUD = log_action(game, "Player1 has lost")
449-
print("Player1 has lost to bot1")
450437
break
451438
display_screen(game)
452439
turns += 1
453-
game.selected_area = None
454-
game.continent.areas = game.players["bot1"].turn(game)
440+
if game.selected_area:
441+
game.selected_area.image = pygame.image.load("images/selection_area.png")
442+
game.selected_area = None
443+
game.HUD.select_image = font.render("Selected: ", False, black)
444+
game.HUD.select_rect = game.HUD.select_image.get_rect()
445+
game.HUD.select_rect.center = (DisplayParams.center[0] - (DisplayParams.center[0] - DisplayParams.size[0] / 8), DisplayParams.size[1] - 24)
446+
for player in game.players:
447+
game.continent.areas = game.players[player].turn(game)
455448
if game.continent.areas[player_home].owner == "player1":
456449
game.continent.areas[player_home].count += player_areas # make it so that you can't get stuck, especially when attacked by the bot1 player.
457450
else:

0 commit comments

Comments
 (0)