From 4cfb4b92f0f5b7006265c75f15157c5ab0ab6007 Mon Sep 17 00:00:00 2001 From: Lyzzy Date: Tue, 5 Mar 2024 15:48:06 +0000 Subject: [PATCH] Dateien nach "/" hochladen --- classes.py | 8 ++++---- main.py | 16 ++++++++-------- viecher.py | 22 +++++++++++++++++----- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/classes.py b/classes.py index ec2c32c..c7b3418 100644 --- a/classes.py +++ b/classes.py @@ -34,7 +34,7 @@ fonts = { } class Button(): - def __init__(self, x, y, width, height, font, font_size, buttonText='Button', onclickFunction=None, onePress=False): + def __init__(self, x, y, width, height, image, font, font_size, buttonText='', onclickFunction=None, onePress=False): self.font = pygame.font.Font(f'fonts/{fonts[font]}', font_size) self.x = x self.y = y @@ -44,7 +44,7 @@ class Button(): self.onePress = onePress self.alreadyPressed = False - with open('art/images/textbox.png', 'r') as tb: + with open(f'art/images/{image}', 'r') as tb: self.box = pygame.image.load(tb) self.box = pygame.transform.scale(self.box, (width, height)) @@ -53,7 +53,7 @@ class Button(): self.buttonSurf = self.font.render(buttonText, True, '#baab80') - def process(self, screen, clock, running, background, isblack, WIDTH, HEIGHT): + def update(self, screen, clock=None, running=None, background=None, isblack=None, WIDTH=None, HEIGHT=None): mousePos = pygame.mouse.get_pos() if self.buttonRect.collidepoint(mousePos): if pygame.mouse.get_pressed(num_buttons=3)[0]: @@ -227,4 +227,4 @@ class Obstacle(GameObjects): self.rect = pygame.Rect((x, y), self.background.get_size()) def draw(self, screen): - screen.blit(self.background, self.rect) \ No newline at end of file + screen.blit(self.background, self.rect) diff --git a/main.py b/main.py index 840dfae..ee279e0 100644 --- a/main.py +++ b/main.py @@ -64,7 +64,7 @@ def play(screen, clock, running, background, isblack, WIDTH, HEIGHT): if not freeze: for thing in objects[0]: thing.book.hidden = not freeze - if not thing.update(pygame.key.get_pressed(), objects): + if not thing.update(pygame.key.get_pressed(), pygame.mouse.get_pos(), objects): menu(screen, clock, running, background, isblack, WIDTH, HEIGHT) thing.draw(screen) @@ -127,9 +127,9 @@ def options(screen, clock, running, background, isblack, WIDTH, HEIGHT): def menu(screen, clock, running, background, isblack, WIDTH, HEIGHT): objects = [] - objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2, 160, 64, 'medieval', 48, "Play", play)) - #objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 - 72, 160, 64, 'medieval', 48, "Options", uwu)) - objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 + 72, 160, 64, 'medieval', 48, "Exit game", quitGame)) + objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2, 160, 64, 'textbox.png', 'medieval', 48, "Play", play)) + #objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 - 72, 160, 64, 'textbox.png', 'medieval', 48, "Options", uwu)) + objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 + 72, 160, 64, 'textbox.png', 'medieval', 48, "Exit game", quitGame)) while running: for event in pygame.event.get(): if event.type == pygame.QUIT: @@ -142,7 +142,7 @@ def menu(screen, clock, running, background, isblack, WIDTH, HEIGHT): # fill the screen with an image to clear the screen screen.blit(bg, (0, 0)) for obj in objects: - obj.process(screen, clock, running, background, isblack, WIDTH, HEIGHT) + obj.update(screen, clock, running, background, isblack, WIDTH, HEIGHT) # flip() the display to put your work on screen pygame.display.flip() @@ -180,9 +180,9 @@ def main(): config = readConfig() screen, clock, running, isblack, background, objects = setUp(config["screen"]) WIDTH, HEIGHT = screen.get_size() - #objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 - 72, 160, 64, 'medieval', 48, "Play", play)) - #objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2, 160, 64, 'medieval', 48, "Options", uwu)) - #objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 + 72, 160, 64, 'medieval', 48, "Exit game", quitGame)) + #objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 - 72, 160, 64, 'textbox.png', 'medieval', 48, "Play", play)) + #objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2, 160, 64, 'textbox.png', 'medieval', 48, "Options", uwu)) + #objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 + 72, 160, 64, 'textbox.png', 'medieval', 48, "Exit game", quitGame)) menu(screen, clock, running, background, isblack, WIDTH, HEIGHT) test(screen, clock, running, background, isblack, WIDTH, HEIGHT) diff --git a/viecher.py b/viecher.py index 4dfcf6c..e7e0ee3 100644 --- a/viecher.py +++ b/viecher.py @@ -1,4 +1,6 @@ import pygame as pg +from classes import * + vec = pg.math.Vector2 fps = 60 @@ -163,8 +165,9 @@ class MainCharacter(Fighter): if isinstance(touches, NPC): touches.talk(objects) - def attack(self, obj, moveto = vec(0,1)): + def attack(self, obj, mouse): if self.lastAttack + self.attack_speed * 1000 < pg.time.get_ticks(): + moveto = mouse- vec(self.x, self.y) if self.book.current_sp == 'fireball': weapon = Fireball('fb1', 100, self.x, self.y, moveto, 5) else: @@ -172,11 +175,11 @@ class MainCharacter(Fighter): obj[3].append(weapon) self.lastAttack = pg.time.get_ticks() - def update(self, keys, objects): + def update(self, keys, mouse, objects): if not self.talking: self.walk(keys, objects) - if keys[pg.K_f]: - self.attack(objects) + if keys[pg.K_f]: + self.attack(objects, vec(mouse)) if self.health.health <= 0: return False else: @@ -263,21 +266,30 @@ class Book(): self.rect = pg.Rect(self.x, self.y, self.sprite.get_width(), self.sprite.get_height()) self.sp_list = spells self.current_sp = current_spell + self.buttons=[] + self.buttons_height = 150 def draw(self, screen): if self.hidden: return self.rect.x, self.rect.y = self.x, self.y screen.blit(self.sprite, self.rect) + for button in self.buttons: + button.update(screen) def addspell(self, spell): if spell not in self.sp_list: self.sp_list.append(spell) self.current_sp = spell + self.buttons.append(Button(200, self.buttons_height, 58, 50, f'{spell}.png', 'medieval', 23)) + self.buttons_height += 100 + + def update_spell(self): + self.current_sp = None + def update(self): pass - class Mobs(Fighter): def __init__(self, name, ms, sprite, x, y, health, damage, level, asp, atr, drops) -> None: super().__init__(name, ms, sprite, x, y, health, damage, level, asp, atr)