Compare commits

...

2 commits

3 changed files with 27 additions and 16 deletions

View file

@ -34,7 +34,7 @@ fonts = {
} }
class Button(): 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.font = pygame.font.Font(f'fonts/{fonts[font]}', font_size)
self.x = x self.x = x
self.y = y self.y = y
@ -44,7 +44,7 @@ class Button():
self.onePress = onePress self.onePress = onePress
self.alreadyPressed = False 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.image.load(tb)
self.box = pygame.transform.scale(self.box, (width, height)) self.box = pygame.transform.scale(self.box, (width, height))
@ -53,7 +53,7 @@ class Button():
self.buttonSurf = self.font.render(buttonText, True, '#baab80') 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() mousePos = pygame.mouse.get_pos()
if self.buttonRect.collidepoint(mousePos): if self.buttonRect.collidepoint(mousePos):
if pygame.mouse.get_pressed(num_buttons=3)[0]: if pygame.mouse.get_pressed(num_buttons=3)[0]:

16
main.py
View file

@ -75,7 +75,7 @@ def play(screen, clock, running, background, isblack, WIDTH, HEIGHT):
for thing in objects[0]: for thing in objects[0]:
thing.book.hidden = not freeze 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) menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
thing.draw(screen) thing.draw(screen)
@ -135,9 +135,9 @@ def options(screen, clock, running, background, isblack, WIDTH, HEIGHT):
def menu(screen, clock, running, background, isblack, WIDTH, HEIGHT): def menu(screen, clock, running, background, isblack, WIDTH, HEIGHT):
objects = [] objects = []
objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2, 160, 64, 'medieval', 48, "Play", play)) 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, 'medieval', 48, "Options", uwu)) #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, 'medieval', 48, "Exit game", quitGame)) objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 + 72, 160, 64, 'textbox.png', 'medieval', 48, "Exit game", quitGame))
while running: while running:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
@ -150,7 +150,7 @@ def menu(screen, clock, running, background, isblack, WIDTH, HEIGHT):
# fill the screen with an image to clear the screen # fill the screen with an image to clear the screen
screen.blit(bg, (0, 0)) screen.blit(bg, (0, 0))
for obj in objects: 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 # flip() the display to put your work on screen
pygame.display.flip() pygame.display.flip()
@ -221,9 +221,9 @@ def main():
config = readConfig() config = readConfig()
screen, clock, running, isblack, background, objects = setUp(config["screen"]) screen, clock, running, isblack, background, objects = setUp(config["screen"])
WIDTH, HEIGHT = screen.get_size() 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 - 72, 160, 64, 'textbox.png', '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, 160, 64, 'textbox.png', '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, "Exit game", quitGame))
menu(screen, clock, running, background, isblack, WIDTH, HEIGHT) menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
test(screen, clock, running, background, isblack, WIDTH, HEIGHT) test(screen, clock, running, background, isblack, WIDTH, HEIGHT)

View file

@ -1,5 +1,6 @@
import pygame as pg import pygame as pg
from classes import * from classes import *
vec = pg.math.Vector2 vec = pg.math.Vector2
fps = 60 fps = 60
@ -195,8 +196,9 @@ class MainCharacter(Fighter):
self.y = objects[3][0].height - 32 - self.rect.height + 1 self.y = objects[3][0].height - 32 - self.rect.height + 1
""" """
def attack(self, obj, moveto = vec(0,1)): def attack(self, obj, mouse):
if self.lastAttack + self.attack_speed * 1000 < pg.time.get_ticks(): if self.lastAttack + self.attack_speed * 1000 < pg.time.get_ticks():
moveto = mouse- vec(self.x, self.y)
if self.book.current_sp == 'fireball': if self.book.current_sp == 'fireball':
weapon = Fireball('fb1', 100, self.x, self.y, moveto, 5) weapon = Fireball('fb1', 100, self.x, self.y, moveto, 5)
else: else:
@ -204,11 +206,11 @@ class MainCharacter(Fighter):
obj[3].append(weapon) obj[3].append(weapon)
self.lastAttack = pg.time.get_ticks() self.lastAttack = pg.time.get_ticks()
def update(self, keys, objects): def update(self, keys, mouse, objects):
if not self.talking: if not self.talking:
self.walk(keys, objects) self.walk(keys, objects)
if keys[pg.K_f]: if keys[pg.K_f]:
self.attack(objects) self.attack(objects, vec(mouse))
if self.health.health <= 0: if self.health.health <= 0:
return False return False
else: else:
@ -295,21 +297,30 @@ class Book():
self.rect = pg.Rect(self.x, self.y, self.sprite.get_width(), self.sprite.get_height()) self.rect = pg.Rect(self.x, self.y, self.sprite.get_width(), self.sprite.get_height())
self.sp_list = spells self.sp_list = spells
self.current_sp = current_spell self.current_sp = current_spell
self.buttons=[]
self.buttons_height = 150
def draw(self, screen): def draw(self, screen):
if self.hidden: if self.hidden:
return return
self.rect.x, self.rect.y = self.x, self.y self.rect.x, self.rect.y = self.x, self.y
screen.blit(self.sprite, self.rect) screen.blit(self.sprite, self.rect)
for button in self.buttons:
button.update(screen)
def addspell(self, spell): def addspell(self, spell):
if spell not in self.sp_list: if spell not in self.sp_list:
self.sp_list.append(spell) self.sp_list.append(spell)
self.current_sp = 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): def update(self):
pass pass
class Mobs(Fighter): class Mobs(Fighter):
def __init__(self, name, ms, sprite, x, y, health, damage, level, asp, atr, drops) -> None: 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) super().__init__(name, ms, sprite, x, y, health, damage, level, asp, atr)