To much gap between last commit
Im now working next on shoorting for turret Added new sand map and new cannon tower images aswell as function to play animations, showing range, placing turrets on allowed tiles for towers Buy and cancel button to buy towers
This commit is contained in:
+107
-6
@@ -1,6 +1,7 @@
|
||||
from game.screens.base_screen import BaseScreen
|
||||
from game.enemy import Enemy
|
||||
from game.camera import Camera
|
||||
from game.tower import Tower, TowerGroup
|
||||
from game.util import *
|
||||
import pygame as pg
|
||||
import pygame_gui as pgi
|
||||
@@ -15,19 +16,99 @@ class GamePlay(BaseScreen):
|
||||
# self.map_surface = pg.surface.Surface((self.game_surface.get_width()/self.tile_multiplicator,self.game_surface.get_height()/self.tile_multiplicator))
|
||||
|
||||
self.camera = Camera()
|
||||
self.center = (self.game_surface.get_width() /2, self.game_surface.get_height() /2)
|
||||
self.movment = [False, False, False, False]
|
||||
self.selected = None
|
||||
self.notunselect = True
|
||||
|
||||
self.map = 'test'
|
||||
self.map = 'desert'
|
||||
self.path = 'assets/maps/' + str(self.map) + '.json'
|
||||
|
||||
self.enemy = Enemy(self.game, 0.5, 100)
|
||||
|
||||
try:
|
||||
self.game.tilemap.load(self.path)
|
||||
except FileNotFoundError:
|
||||
print('File not Found')
|
||||
|
||||
self.enemy_group = pg.sprite.Group()
|
||||
self.tower_group = TowerGroup()
|
||||
|
||||
self.enemy = Enemy(self.game, 0.5, 100)
|
||||
|
||||
self.buy_button = pgi.elements.UIButton(
|
||||
relative_rect=pg.Rect(
|
||||
self.width - 200,
|
||||
self.height -250,
|
||||
150,
|
||||
50
|
||||
),
|
||||
|
||||
text="Buy Tower",
|
||||
manager=self.game.ui_manager
|
||||
)
|
||||
self.cancel_button = pgi.elements.UIButton(
|
||||
relative_rect=pg.Rect(
|
||||
self.width - 200,
|
||||
self.height -175,
|
||||
150,
|
||||
50
|
||||
),
|
||||
|
||||
text="Cancel",
|
||||
manager=self.game.ui_manager
|
||||
)
|
||||
self.buy_button.hide()
|
||||
self.cancel_button.hide()
|
||||
self.bought = False
|
||||
self.elements = [
|
||||
self.buy_button,
|
||||
self.cancel_button
|
||||
]
|
||||
|
||||
def process_event(self, event):
|
||||
#calculate tile position from mouse position
|
||||
self.mpos = pg.mouse.get_pos()
|
||||
game_rect = self.game_surface.get_rect(topleft=(0, self.height / 6))
|
||||
|
||||
if game_rect.collidepoint(self.mpos):
|
||||
local_mpos = (
|
||||
self.mpos[0] - game_rect.x,
|
||||
self.mpos[1] - game_rect.y
|
||||
)
|
||||
|
||||
world_pos = self.camera.screen_to_world(local_mpos)
|
||||
self.tile_pos_decimal = pixel_to_tile(world_pos, self.game.tilemap.tile_size)
|
||||
self.tile_pos = (int(self.tile_pos_decimal[0]), int(self.tile_pos_decimal[1]))
|
||||
else:
|
||||
self.tile_pos = None
|
||||
|
||||
if event.type == pgi.UI_BUTTON_PRESSED:
|
||||
if event.ui_element == self.buy_button:
|
||||
self.bought = True
|
||||
# self.tower = Tower(self.game, self.mpos, self.tile_pos, 'cannon')
|
||||
if event.ui_element == self.cancel_button:
|
||||
self.bought = False
|
||||
|
||||
self.tile_pos = (int(self.tile_pos_decimal[0]), int(self.tile_pos_decimal[1]))
|
||||
if event.type == pg.MOUSEBUTTONDOWN:
|
||||
if self.tile_pos is not None:
|
||||
if event.button == 1:
|
||||
self.selected = self.select_turret(self.tile_pos)
|
||||
self.clear_selection()
|
||||
if self.selected is not None:
|
||||
print(f'selected: {self.selected}')
|
||||
self.selected.selected = True
|
||||
print(f'{self.selected}')
|
||||
if self.bought:
|
||||
if self.game.tilemap.placeable(self.tile_pos):
|
||||
if not any(tower.tile_pos == self.tile_pos for tower in self.tower_group):
|
||||
tower = Tower(self.game, self.mpos, self.tile_pos, 'cannon', 40)
|
||||
self.tower_group.add(tower)
|
||||
self.bought = False
|
||||
if event.button == 3:
|
||||
for tower in self.tower_group:
|
||||
if tower.tile_pos == self.tile_pos:
|
||||
tower.kill()
|
||||
|
||||
if event.type == pg.KEYDOWN:
|
||||
if event.key == pg.K_a:
|
||||
self.movment[0] = True
|
||||
@@ -39,9 +120,9 @@ class GamePlay(BaseScreen):
|
||||
self.movment[3] = True
|
||||
|
||||
if event.key == pg.K_q:
|
||||
self.camera.change_zoom(-0.25)
|
||||
self.camera.zoom_offset(self.center, -0.25)
|
||||
if event.key == pg.K_e:
|
||||
self.camera.change_zoom(0.25)
|
||||
self.camera.zoom_offset(self.center ,0.25)
|
||||
if event.type == pg.KEYUP:
|
||||
if event.key == pg.K_a:
|
||||
self.movment[0] = False
|
||||
@@ -54,6 +135,7 @@ class GamePlay(BaseScreen):
|
||||
|
||||
def update(self, dt):
|
||||
self.enemy.update()
|
||||
self.tower_group.update("sigma")
|
||||
|
||||
self.camera.move((self.movment[1] - self.movment[0]) * 2, (self.movment[3] - self.movment[2]) * 2)
|
||||
|
||||
@@ -64,7 +146,26 @@ class GamePlay(BaseScreen):
|
||||
self.game.tilemap.render_game(self.game_surface, self.camera)
|
||||
|
||||
self.enemy.render(self.game_surface, self.camera)
|
||||
self.tower_group.render(self.game_surface, self.camera)
|
||||
|
||||
if self.bought:
|
||||
if self.game.tilemap.placeable(self.tile_pos):
|
||||
self.game_surface.blit(pg.transform.scale2x(self.game.assets["cannon"]), (self.mpos[0] - (self.game.tilemap.tile_size //2), self.mpos[1] - (self.game.tilemap.tile_size //2 + self.height/6)))
|
||||
else:
|
||||
self.game_surface.blit(pg.transform.scale2x(self.game.assets["red_cannon"]), (self.mpos[0] - (self.game.tilemap.tile_size //2), self.mpos[1] - (self.game.tilemap.tile_size //2 + self.height/6)))
|
||||
|
||||
# self.game_surface.blit(pg.transform.scale(self.map_surface, self.game_surface.get_size()))
|
||||
|
||||
|
||||
surface.blit(self.game_surface, (0, self.height/6))
|
||||
|
||||
# if self.bought:
|
||||
# self.game_surface.blit(pg.transform.scale2x(self.game.assets["cannon"]), (self.mpos[0] - (self.game.tilemap.tile_size //2), self.mpos[1] - (self.game.tilemap.tile_size //2)))
|
||||
|
||||
def select_turret(self, tile_pos):
|
||||
for tower in self.tower_group:
|
||||
if (tile_pos[0], tile_pos[1]) == (tower.tile_pos[0], tower.tile_pos[1]):
|
||||
return tower
|
||||
|
||||
def clear_selection(self):
|
||||
for tower in self.tower_group:
|
||||
tower.selected = False
|
||||
@@ -70,7 +70,6 @@ class MainMenu(BaseScreen):
|
||||
if event.type == pgi.UI_BUTTON_PRESSED:
|
||||
|
||||
if event.ui_element == self.play_button:
|
||||
|
||||
# Zum Gameplay wechseln
|
||||
self.game.change_screen("game")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user