My Pygame Game Engine

asd

WillM f0b8d6c8 bf54c43e

-66
src/scripts/game/bullet.py
··· 1 - import pygame, math, random 2 - from src.scripts.modules.entity import * 3 - 4 - class bullet_shooty_thingy: 5 - def __init__(self, game, parent) -> None: 6 - self.game = game 7 - self.parent = parent 8 - 9 - self.parent.bullets = [] 10 - self.image = self.game.image.load("bullet.png") 11 - 12 - self.firerate = 20 13 - self.firerate_counter = 0 14 - 15 - def update(self): 16 - self.firerate_counter += 100 * self.game.delta_time 17 - if self.game.input.is_mouse_button_pressed() and self.firerate_counter > self.firerate: 18 - self.firerate_counter = 0 19 - mx, my = pygame.mouse.get_pos() 20 - self.parent.bullets.append([self.parent.transform.x, self.parent.transform.y, self.game.math.direction_between_points(self.parent.transform.x, self.parent.transform.y, self.game.relative_mxmy[0], self.game.relative_mxmy[1]) + random.randrange(-5, 5), 400]) 21 - 22 - for i, item in enumerate(self.parent.bullets): 23 - for j, jtem in enumerate(self.parent.screen.enemy.bullets): 24 - if self.game.math.distance_between_points(item[0], item[1], jtem[0], jtem[1]) < 30: 25 - self.parent.bullets.pop(i) 26 - self.parent.screen.enemy.bullets.pop(j) 27 - break 28 - 29 - def render(self): 30 - move_speed = 700 * self.game.delta_time 31 - new_bullets = self.parent.bullets 32 - for i, item in enumerate(self.parent.bullets): 33 - item[3] -= 1 34 - if item[3] == 0: 35 - new_bullets.pop(i) 36 - item[0] += self.game.math.lengthdir_x(move_speed, item[2]) 37 - item[1] += self.game.math.lengthdir_y(move_speed, item[2]) 38 - image = pygame.transform.scale(self.image, (50, 25)) 39 - img, rect = self.game.math.rotate_center(image, 180 - item[2] + 180, item[0], item[1]) 40 - self.parent.screen.game_surface.blit(img, rect) 41 - self.parent.bullets = new_bullets 42 - 43 - class bullet_shooty_thingy2: 44 - def __init__(self, game, parent) -> None: 45 - self.game = game 46 - self.parent = parent 47 - 48 - self.parent.bullets = [] 49 - self.image = self.game.image.load("bullet.png") 50 - 51 - def update(self): 52 - if self.game.input.is_mouse_button_just_pressed(): 53 - mx, my = pygame.mouse.get_pos() 54 - self.parent.bullets.append([self.parent.transform.x, self.parent.transform.y, self.game.math.direction_between_points(self.parent.transform.x, self.parent.transform.y, mx, my), 400]) 55 - 56 - def render(self): 57 - new_bullets = self.parent.bullets 58 - for i, item in enumerate(self.parent.bullets): 59 - item[3] -= 1 60 - if item[3] == 0: 61 - new_bullets.pop(i) 62 - item[0] += self.game.math.lengthdir_x(1, item[2]) 63 - item[1] += self.game.math.lengthdir_y(1, item[2]) 64 - img, rect = self.game.math.rotate_center(self.image, item[3], item[0], item[1]) 65 - self.game.renderer.screen.blit(pygame.transform.scale(img, (50, 25)), rect) 66 - self.parent.bullets = new_bullets
-113
src/scripts/game/enemy.py
··· 1 - import pygame, math 2 - from src.scripts.modules.entity import * 3 - from .bullet import bullet_shooty_thingy 4 - from src.scripts.modules.gui import * 5 - 6 - class enemy_ai_component: 7 - def __init__(self, game, parent) -> None: 8 - self.game = game 9 - self.parent = parent 10 - 11 - self.state = "movement" 12 - 13 - self.parent.bullets = [] 14 - self.bullet_firerate = 15 15 - self.bullet_firerate_counter = 0 16 - 17 - self.image = self.game.image.load("enemy_bullet.png") 18 - 19 - def update(self): 20 - if self.parent.health > 0: 21 - if self.state == "movement": 22 - move_speed = 200 * self.game.delta_time 23 - self.parent.transform.x += self.game.math.lengthdir_x(move_speed, self.game.math.direction_between_points(self.parent.transform.x, self.parent.transform.y, self.game.player.transform.x, self.game.player.transform.y)) 24 - self.parent.transform.y += self.game.math.lengthdir_y(move_speed, self.game.math.direction_between_points(self.parent.transform.x, self.parent.transform.y, self.game.player.transform.x, self.game.player.transform.y)) 25 - if self.game.math.distance_between_points(self.parent.transform.x, self.parent.transform.y, self.game.player.transform.x, self.game.player.transform.y) < 500: 26 - self.state = "attack_start" 27 - elif self.state == "attack_start": 28 - self.target_x = self.game.player.transform.x 29 - self.target_y = self.game.player.transform.y 30 - self.state = "attack" 31 - elif self.state == "attack": 32 - if self.game.math.distance_between_points(self.parent.transform.x, self.parent.transform.y, self.game.player.transform.x, self.game.player.transform.y) > 500: 33 - self.state = "movement" 34 - self.bullet_firerate_counter += 100 * self.game.delta_time 35 - if self.bullet_firerate_counter > self.bullet_firerate: 36 - self.bullet_firerate_counter = 0 37 - self.parent.bullets.append([self.parent.transform.x, self.parent.transform.y, self.game.math.direction_between_points(self.parent.transform.x, self.parent.transform.y, self.parent.screen.player.transform.x, self.parent.screen.player.transform.y), 300]) 38 - self.parent.bullets.append([self.parent.transform.x, self.parent.transform.y, self.game.math.direction_between_points(self.parent.transform.x, self.parent.transform.y, self.parent.screen.player.transform.x, self.parent.screen.player.transform.y) + 90, 300]) 39 - self.parent.bullets.append([self.parent.transform.x, self.parent.transform.y, self.game.math.direction_between_points(self.parent.transform.x, self.parent.transform.y, self.parent.screen.player.transform.x, self.parent.screen.player.transform.y) + 180, 300]) 40 - self.parent.bullets.append([self.parent.transform.x, self.parent.transform.y, self.game.math.direction_between_points(self.parent.transform.x, self.parent.transform.y, self.parent.screen.player.transform.x, self.parent.screen.player.transform.y) + 270, 300]) 41 - 42 - def render(self): 43 - move_speed = 700 * self.game.delta_time 44 - new_bullets = self.parent.bullets 45 - for i, item in enumerate(self.parent.bullets): 46 - item[3] -= 1 47 - if item[3] < 0: 48 - new_bullets.pop(i) 49 - item[0] += self.game.math.lengthdir_x(move_speed, item[2]) 50 - item[1] += self.game.math.lengthdir_y(move_speed, item[2]) 51 - image = pygame.transform.scale(self.image, (60, 60)) 52 - img, rect = self.game.math.rotate_center(image, item[2], item[0], item[1]) 53 - self.parent.screen.game_surface.blit(img, rect) 54 - self.parent.bullets = new_bullets 55 - class enemy_renderer_component: 56 - def __init__(self, game, parent) -> None: 57 - self.game = game 58 - self.parent = parent 59 - 60 - self.image = self.game.image.load("enemy.png") 61 - 62 - def update(self): 63 - pass 64 - 65 - def render(self): 66 - if self.parent.health > 0: 67 - image = pygame.transform.scale(self.image, (88, 88)) 68 - img, rect = self.game.math.rotate_center(image, 0, self.parent.transform.x, self.parent.transform.y) 69 - self.parent.screen.game_surface.blit(img, rect) 70 - 71 - class enemy_health_system: 72 - def __init__(self, game, parent) -> None: 73 - self.game = game 74 - self.parent = parent 75 - 76 - self.parent.health = 50 77 - self.cant_get_hit_by = [] 78 - 79 - self.health_bar_rect = gui_rect(game) 80 - self.health_bar_rect.set_y_constraint(percentage_constraint(0.02)) 81 - self.health_bar_rect.set_x_constraint(percentage_constraint(0.02)) 82 - self.health_bar_rect.set_width_constraint(percentage_constraint(0.3)) 83 - self.health_bar_rect.set_height_constraint(pixel_constraint(5)) 84 - self.health_bar_rect.set_border_radius(10) 85 - self.health_bar_rect.set_draw_color(self.game.color_handler.get_rgb('enemy.health_bar')) 86 - 87 - def update(self): 88 - self.health_bar_rect.tween_size(percentage_constraint(0.3 - (0.03 * ((50 - self.parent.health) / 5))), pixel_constraint(5), 24) 89 - self.health_bar_rect.update() 90 - if self.parent.health > 0: 91 - new_bullets = self.game.player.bullets 92 - for i, item in enumerate(self.game.player.bullets): 93 - if self.game.math.distance_between_points(self.parent.transform.x, self.parent.transform.y, item[0], item[1]) < 80: 94 - new_bullets.pop(i) 95 - self.parent.health -= 1 96 - self.cant_get_hit_by.append(item) 97 - self.game.player.bullets = new_bullets 98 - 99 - def render(self): 100 - self.health_bar_rect.render() 101 - 102 - class Enemy(Entity): 103 - def __init__(self, game, screen) -> None: 104 - self.game = game 105 - self.screen = screen 106 - 107 - self.components = [] 108 - 109 - self.transform = Transform() 110 - self.transform.y = 200 111 - self.add_component(enemy_health_system(game, self)) 112 - self.add_component(enemy_renderer_component(game, self)) 113 - self.add_component(enemy_ai_component(game, self))
-83
src/scripts/game/player.py
··· 1 - import pygame, math 2 - from src.scripts.modules.entity import * 3 - from .bullet import * 4 - from src.scripts.modules.gui import * 5 - 6 - class player_movement_component: 7 - def __init__(self, game, parent) -> None: 8 - self.game = game 9 - self.parent = parent 10 - 11 - def update(self): 12 - move_speed = 300 * self.game.delta_time 13 - if self.game.input.is_pressed("a") and self.parent.transform.x > 0 + 32 + move_speed: 14 - self.parent.transform.x -= move_speed 15 - if self.game.input.is_pressed("d") and self.parent.transform.x < self.parent.screen.game_surface.get_width() - move_speed - 32: 16 - self.parent.transform.x += move_speed 17 - if self.game.input.is_pressed("w") and self.parent.transform.y > 0 + 32 + move_speed: 18 - self.parent.transform.y-= move_speed 19 - if self.game.input.is_pressed("s") and self.parent.transform.y < self.parent.screen.game_surface.get_height() - move_speed - 32: 20 - self.parent.transform.y += move_speed 21 - 22 - def render(self): 23 - pass 24 - 25 - class player_renderer_component: 26 - def __init__(self, game, parent) -> None: 27 - self.game = game 28 - self.parent = parent 29 - 30 - self.image = self.game.image.load("player.png") 31 - 32 - def update(self): 33 - pass 34 - 35 - def render(self): 36 - image = pygame.transform.scale(self.image, (64, 64)) 37 - img, rect = self.game.math.rotate_center(image, 0, self.parent.transform.x, self.parent.transform.y) 38 - self.parent.screen.game_surface.blit(img, rect) 39 - 40 - class player_health_system: 41 - def __init__(self, game, parent) -> None: 42 - self.game = game 43 - self.parent = parent 44 - 45 - self.parent.health = 10 46 - self.cant_get_hit_by = [] 47 - 48 - self.health_bar_rect = gui_rect(game) 49 - self.health_bar_rect.set_y_constraint(percentage_constraint(0.02)) 50 - self.health_bar_rect.set_x_constraint(percentage_constraint(0.67)) 51 - self.health_bar_rect.set_width_constraint(percentage_constraint(0.3)) 52 - self.health_bar_rect.set_height_constraint(pixel_constraint(5)) 53 - self.health_bar_rect.set_border_radius(10) 54 - self.health_bar_rect.set_draw_color(self.game.color_handler.get_rgb('player.health_bar')) 55 - 56 - def update(self): 57 - self.health_bar_rect.tween_size(percentage_constraint(0.3 - (0.03 * (10 - self.parent.health))), pixel_constraint(5), 24) 58 - self.health_bar_rect.update() 59 - 60 - for i, item in enumerate(self.parent.screen.enemy.bullets): 61 - if self.game.math.distance_between_points(self.parent.transform.x, self.parent.transform.y, item[0], item[1]) < 60: 62 - self.parent.screen.enemy.bullets.pop(i) 63 - self.parent.health -= 1 64 - 65 - def render(self): 66 - self.health_bar_rect.render() 67 - 68 - class Player(Entity): 69 - def __init__(self, game, screen) -> None: 70 - self.game = game 71 - self.screen = screen 72 - 73 - self.game.player = self 74 - 75 - self.components = [] 76 - 77 - self.transform = Transform() 78 - self.transform.y = 500 79 - 80 - self.add_component(bullet_shooty_thingy(game, self)) 81 - self.add_component(player_health_system(game, self)) 82 - self.add_component(player_movement_component(game, self)) 83 - self.add_component(player_renderer_component(game, self))
-49
src/scripts/screens/screen_game.py
··· 1 - from src.scripts.modules.renderer import Screen 2 - from src.scripts.modules.gui import * 3 - import os 4 - 5 - from src.scripts.game.player import Player 6 - from src.scripts.game.enemy import Enemy 7 - 8 - class screen_game(Screen): 9 - def __init__(self, game): 10 - self.game = game 11 - 12 - self.player = Player(game, self) 13 - self.enemy = Enemy(game, self) 14 - 15 - self.game_surface = pygame.Surface((1280, 720), pygame.SRCALPHA) 16 - 17 - def update(self): 18 - self.player.update() 19 - self.enemy.update() 20 - 21 - if self.player.health < 0: 22 - self.player.health = 10 23 - self.enemy.health = 50 24 - self.game.renderer.switch_screen('lose') 25 - if self.enemy.health < 2: 26 - self.player.health = 10 27 - self.enemy.health = 50 28 - self.game.renderer.switch_screen('win') 29 - 30 - def render(self): 31 - self.game_surface = pygame.Surface((1280, 720), pygame.SRCALPHA) 32 - self.game.renderer.screen.fill(self.game.color_handler.get_rgb('main_menu.background')) 33 - self.player.render() 34 - self.enemy.render() 35 - width_diff = abs(self.game.renderer.screen.get_width() - self.game.renderer.original_size[0]) 36 - height_diff = abs(self.game.renderer.screen.get_height() - self.game.renderer.original_size[1]) 37 - if width_diff > height_diff: 38 - aspect_ratio = self.game.renderer.screen.get_width() / self.game.renderer.original_size[0] 39 - width = self.game.renderer.screen.get_width() 40 - height = self.game.renderer.original_size[1] * aspect_ratio 41 - else: 42 - aspect_ratio = self.game.renderer.screen.get_height() / self.game.renderer.original_size[1] 43 - width = self.game.renderer.original_size[0] * aspect_ratio 44 - height = self.game.renderer.screen.get_height() 45 - 46 - mx, my = pygame.mouse.get_pos() 47 - self.game.relative_mxmy = (mx / aspect_ratio, my / aspect_ratio) 48 - 49 - self.game.renderer.screen.blit(pygame.transform.scale(self.game_surface, (int(width), int(height))), (0, 0))
-61
src/scripts/screens/screen_lose.py
··· 1 - from src.scripts.modules.renderer import Screen 2 - from src.scripts.modules.gui import * 3 - import os 4 - 5 - from src.scripts.game.player import Player 6 - from src.scripts.game.enemy import Enemy 7 - 8 - class screen_lose(Screen): 9 - def __init__(self, game): 10 - self.game = game 11 - 12 - self.play_button = gui_press_button(game) 13 - self.play_button.rect.set_x_constraint(percentage_constraint(0.03)) 14 - self.play_button.rect.set_y_constraint(percentage_constraint(0.8)) 15 - self.play_button.rect.set_width_constraint(percentage_constraint(0.3)) 16 - self.play_button.rect.set_height_constraint(percentage_constraint(0.15)) 17 - self.play_button.rect.set_border_radius(10) 18 - self.play_button.rect.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg')) 19 - 20 - self.play_button_text = gui_text(game) 21 - self.play_button_text.parent = self.play_button.rect 22 - self.play_button_text.text = "Back" 23 - self.play_button_text.set_x_constraint(center_constraint()) 24 - self.play_button_text.set_y_constraint(center_constraint()) 25 - self.play_button_text.set_size_constraint(percentage_constraint(0.8)) 26 - self.play_button_text.set_color(self.game.color_handler.get_rgb('main_menu.text')) 27 - 28 - self.screen = gui_rect(game) 29 - self.screen.set_x_constraint(center_constraint()) 30 - self.screen.set_y_constraint(center_constraint()) 31 - self.screen.set_width_constraint(percentage_constraint(1)) 32 - self.screen.set_height_constraint(percentage_constraint(1)) 33 - self.screen.visible = False 34 - 35 - self.text = gui_text(game) 36 - self.text.parent = self.screen 37 - self.text.text = "You lost" 38 - self.text.set_x_constraint(center_constraint()) 39 - self.text.set_y_constraint(center_constraint()) 40 - self.text.set_size_constraint(percentage_constraint(0.2)) 41 - self.text.set_color(self.game.color_handler.get_rgb('main_menu.text')) 42 - 43 - 44 - def update(self): 45 - self.screen.update() 46 - self.play_button.update() 47 - self.play_button_text.update() 48 - self.text.update() 49 - self.play_button.rect.tween_to(percentage_constraint(0.03), percentage_constraint(0.8), 6) 50 - if self.play_button.hover: 51 - self.play_button.rect.tween_to(percentage_constraint(0.05), percentage_constraint(0.8), 6) 52 - 53 - if self.play_button.pressed: 54 - self.game.renderer.switch_screen('main_menu') 55 - 56 - def render(self): 57 - self.game.renderer.screen.fill(self.game.color_handler.get_rgb('main_menu.background')) 58 - self.screen.render() 59 - self.play_button.render() 60 - self.play_button_text.render() 61 - self.text.render()
-102
src/scripts/screens/screen_main_menu.py
··· 1 - from src.scripts.modules.renderer import Screen 2 - from src.scripts.modules.gui import * 3 - import os 4 - 5 - class screen_main_menu(Screen): 6 - def __init__(self, game): 7 - self.game = game 8 - 9 - self.buttons_bg = gui_rect(game) 10 - self.buttons_bg.set_x_constraint(percentage_constraint(0.1)) 11 - self.buttons_bg.set_y_constraint(center_constraint()) 12 - self.buttons_bg.set_width_constraint(percentage_constraint(0.1)) 13 - self.buttons_bg.set_height_constraint(percentage_constraint(0.8)) 14 - self.buttons_bg.set_border_radius(10) 15 - self.buttons_bg.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg_2')) 16 - 17 - self.play_button = gui_press_button(game) 18 - self.play_button.rect.set_x_constraint(percentage_constraint(0.15)) 19 - self.play_button.rect.set_y_constraint(percentage_constraint(0.15)) 20 - self.play_button.rect.set_width_constraint(percentage_constraint(0.3)) 21 - self.play_button.rect.set_height_constraint(percentage_constraint(0.15)) 22 - self.play_button.rect.set_border_radius(10) 23 - self.play_button.rect.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg')) 24 - 25 - self.play_button_text = gui_text(game) 26 - self.play_button_text.parent = self.play_button.rect 27 - self.play_button_text.text = "Play" 28 - self.play_button_text.set_x_constraint(center_constraint()) 29 - self.play_button_text.set_y_constraint(center_constraint()) 30 - self.play_button_text.set_size_constraint(percentage_constraint(0.8)) 31 - self.play_button_text.set_color(self.game.color_handler.get_rgb('main_menu.text')) 32 - 33 - self.play_button2 = gui_press_button(game) 34 - self.play_button2.rect.set_x_constraint(percentage_constraint(0.15)) 35 - self.play_button2.rect.set_y_constraint(center_constraint()) 36 - self.play_button2.rect.set_width_constraint(percentage_constraint(0.3)) 37 - self.play_button2.rect.set_height_constraint(percentage_constraint(0.15)) 38 - self.play_button2.rect.set_border_radius(10) 39 - self.play_button2.rect.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg')) 40 - 41 - self.play_button2_text = gui_text(game) 42 - self.play_button2_text.parent = self.play_button2.rect 43 - self.play_button2_text.text = "Settings" 44 - self.play_button2_text.set_x_constraint(center_constraint()) 45 - self.play_button2_text.set_y_constraint(center_constraint()) 46 - self.play_button2_text.set_size_constraint(percentage_constraint(0.8)) 47 - self.play_button2_text.set_color(self.game.color_handler.get_rgb('main_menu.text')) 48 - 49 - self.play_button3 = gui_press_button(game) 50 - self.play_button3.rect.set_x_constraint(percentage_constraint(0.15)) 51 - self.play_button3.rect.set_y_constraint(percentage_constraint(0.7)) 52 - self.play_button3.rect.set_width_constraint(percentage_constraint(0.3)) 53 - self.play_button3.rect.set_height_constraint(percentage_constraint(0.15)) 54 - self.play_button3.rect.set_border_radius(10) 55 - self.play_button3.rect.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg')) 56 - 57 - self.play_button3_text = gui_text(game) 58 - self.play_button3_text.parent = self.play_button3.rect 59 - self.play_button3_text.text = "Exit" 60 - self.play_button3_text.set_x_constraint(center_constraint()) 61 - self.play_button3_text.set_y_constraint(center_constraint()) 62 - self.play_button3_text.set_size_constraint(percentage_constraint(0.8)) 63 - self.play_button3_text.set_color(self.game.color_handler.get_rgb('main_menu.text')) 64 - 65 - def update(self): 66 - self.buttons_bg.update() 67 - self.play_button.update() 68 - self.play_button2.update() 69 - self.play_button3.update() 70 - self.play_button_text.update() 71 - self.play_button2_text.update() 72 - self.play_button3_text.update() 73 - self.play_button.rect.tween_to(percentage_constraint(0.15), percentage_constraint(0.15), 6) 74 - if self.play_button.hover: 75 - self.play_button.rect.tween_to(percentage_constraint(0.175), percentage_constraint(0.15), 6) 76 - 77 - if self.play_button.pressed: 78 - self.game.renderer.switch_screen('game') 79 - 80 - self.play_button2.rect.tween_to(percentage_constraint(0.15), center_constraint(), 6) 81 - if self.play_button2.hover: 82 - self.play_button2.rect.tween_to(percentage_constraint(0.175), center_constraint(), 6) 83 - 84 - if self.play_button2.pressed: 85 - self.game.renderer.switch_screen('settings') 86 - 87 - self.play_button3.rect.tween_to(percentage_constraint(0.15), percentage_constraint(0.7), 6) 88 - if self.play_button3.hover: 89 - self.play_button3.rect.tween_to(percentage_constraint(0.175), percentage_constraint(0.7), 6) 90 - 91 - if self.play_button3.pressed: 92 - self.game.stop() 93 - 94 - def render(self): 95 - self.game.renderer.screen.fill(self.game.color_handler.get_rgb('main_menu.background')) 96 - self.buttons_bg.render() 97 - self.play_button.render() 98 - self.play_button2.render() 99 - self.play_button3.render() 100 - self.play_button_text.render() 101 - self.play_button2_text.render() 102 - self.play_button3_text.render()
-41
src/scripts/screens/screen_settings.py
··· 1 - from src.scripts.modules.renderer import Screen 2 - from src.scripts.modules.gui import * 3 - import os 4 - 5 - from src.scripts.game.player import Player 6 - from src.scripts.game.enemy import Enemy 7 - 8 - class screen_settins(Screen): 9 - def __init__(self, game): 10 - self.game = game 11 - 12 - self.play_button = gui_press_button(game) 13 - self.play_button.rect.set_x_constraint(percentage_constraint(0.03)) 14 - self.play_button.rect.set_y_constraint(percentage_constraint(0.8)) 15 - self.play_button.rect.set_width_constraint(percentage_constraint(0.3)) 16 - self.play_button.rect.set_height_constraint(percentage_constraint(0.15)) 17 - self.play_button.rect.set_border_radius(10) 18 - self.play_button.rect.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg')) 19 - 20 - self.play_button_text = gui_text(game) 21 - self.play_button_text.parent = self.play_button.rect 22 - self.play_button_text.text = "Back" 23 - self.play_button_text.set_x_constraint(center_constraint()) 24 - self.play_button_text.set_y_constraint(center_constraint()) 25 - self.play_button_text.set_size_constraint(percentage_constraint(0.8)) 26 - self.play_button_text.set_color(self.game.color_handler.get_rgb('main_menu.text')) 27 - 28 - def update(self): 29 - self.play_button.update() 30 - self.play_button_text.update() 31 - self.play_button.rect.tween_to(percentage_constraint(0.03), percentage_constraint(0.8), 6) 32 - if self.play_button.hover: 33 - self.play_button.rect.tween_to(percentage_constraint(0.05), percentage_constraint(0.8), 6) 34 - 35 - if self.play_button.pressed: 36 - self.game.renderer.switch_screen('main_menu') 37 - 38 - def render(self): 39 - self.game.renderer.screen.fill(self.game.color_handler.get_rgb('main_menu.background')) 40 - self.play_button.render() 41 - self.play_button_text.render()
-61
src/scripts/screens/screen_win.py
··· 1 - from src.scripts.modules.renderer import Screen 2 - from src.scripts.modules.gui import * 3 - import os 4 - 5 - from src.scripts.game.player import Player 6 - from src.scripts.game.enemy import Enemy 7 - 8 - class screen_win(Screen): 9 - def __init__(self, game): 10 - self.game = game 11 - 12 - self.play_button = gui_press_button(game) 13 - self.play_button.rect.set_x_constraint(percentage_constraint(0.03)) 14 - self.play_button.rect.set_y_constraint(percentage_constraint(0.8)) 15 - self.play_button.rect.set_width_constraint(percentage_constraint(0.3)) 16 - self.play_button.rect.set_height_constraint(percentage_constraint(0.15)) 17 - self.play_button.rect.set_border_radius(10) 18 - self.play_button.rect.set_draw_color(self.game.color_handler.get_rgb('main_menu.bright_bg')) 19 - 20 - self.play_button_text = gui_text(game) 21 - self.play_button_text.parent = self.play_button.rect 22 - self.play_button_text.text = "Back" 23 - self.play_button_text.set_x_constraint(center_constraint()) 24 - self.play_button_text.set_y_constraint(center_constraint()) 25 - self.play_button_text.set_size_constraint(percentage_constraint(0.8)) 26 - self.play_button_text.set_color(self.game.color_handler.get_rgb('main_menu.text')) 27 - 28 - self.screen = gui_rect(game) 29 - self.screen.set_x_constraint(center_constraint()) 30 - self.screen.set_y_constraint(center_constraint()) 31 - self.screen.set_width_constraint(percentage_constraint(1)) 32 - self.screen.set_height_constraint(percentage_constraint(1)) 33 - self.screen.visible = False 34 - 35 - self.text = gui_text(game) 36 - self.text.parent = self.screen 37 - self.text.text = "You won" 38 - self.text.set_x_constraint(center_constraint()) 39 - self.text.set_y_constraint(center_constraint()) 40 - self.text.set_size_constraint(percentage_constraint(0.2)) 41 - self.text.set_color(self.game.color_handler.get_rgb('main_menu.text')) 42 - 43 - 44 - def update(self): 45 - self.screen.update() 46 - self.play_button.update() 47 - self.play_button_text.update() 48 - self.text.update() 49 - self.play_button.rect.tween_to(percentage_constraint(0.03), percentage_constraint(0.8), 6) 50 - if self.play_button.hover: 51 - self.play_button.rect.tween_to(percentage_constraint(0.05), percentage_constraint(0.8), 6) 52 - 53 - if self.play_button.pressed: 54 - self.game.renderer.switch_screen('main_menu') 55 - 56 - def render(self): 57 - self.game.renderer.screen.fill(self.game.color_handler.get_rgb('main_menu.background')) 58 - self.screen.render() 59 - self.play_button.render() 60 - self.play_button_text.render() 61 - self.text.render()