forked from InfoProjekt/game
Merge pull request 'updated the button class -> can now take any arguments for the onClick Function' (#62) from Spafi/game:main into Development
Reviewed-on: InfoProjekt/game#62
This commit is contained in:
commit
0564d778a1
3 changed files with 27 additions and 23 deletions
23
classes.py
23
classes.py
|
|
@ -34,12 +34,13 @@ fonts = {
|
|||
}
|
||||
|
||||
class Button():
|
||||
def __init__(self, x, y, width, height, image, font, font_size, buttonText='', onclickFunction=None, onePress=False):
|
||||
def __init__(self, x, y, width, height, image, font, font_size, buttonText='', onclickFunction=None, onePress=False, attributes=None):
|
||||
self.font = pygame.font.Font(f'fonts/{fonts[font]}', font_size)
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.attributes = attributes
|
||||
self.onclickFunction = onclickFunction
|
||||
self.onePress = onePress
|
||||
self.alreadyPressed = False
|
||||
|
|
@ -53,15 +54,15 @@ class Button():
|
|||
|
||||
self.buttonSurf = self.font.render(buttonText, True, '#baab80')
|
||||
|
||||
def update(self, screen, clock=None, running=None, background=None, isblack=None, WIDTH=None, HEIGHT=None):
|
||||
def update(self, screen):
|
||||
mousePos = pygame.mouse.get_pos()
|
||||
if self.buttonRect.collidepoint(mousePos):
|
||||
if pygame.mouse.get_pressed(num_buttons=3)[0]:
|
||||
if self.onePress:
|
||||
self.onclickFunction()
|
||||
elif not self.alreadyPressed:
|
||||
if 'play' in str(self.onclickFunction):
|
||||
self.onclickFunction(screen, clock, running, background, isblack, WIDTH, HEIGHT)
|
||||
if self.attributes:
|
||||
self.onclickFunction(*self.attributes)
|
||||
self.alreadyPressed = True
|
||||
else:
|
||||
self.onclickFunction()
|
||||
|
|
@ -158,10 +159,10 @@ class Scene(GameObjects):
|
|||
self.level = level
|
||||
self.current_level = 0
|
||||
|
||||
def update(self, change:bool):
|
||||
def update(self, change:bool, objects):
|
||||
if change:
|
||||
self.current_level += 1
|
||||
self.level[self.current_level].update()
|
||||
self.level[self.current_level].update(objects)
|
||||
self.background = self.level[self.current_level].background
|
||||
if isinstance(self.objects, list):
|
||||
for obj in self.objects[0] + self.objects[1] + self.objects[2]:
|
||||
|
|
@ -184,10 +185,10 @@ class Stage(GameObjects):
|
|||
self.rooms = rooms
|
||||
self.current = 0
|
||||
|
||||
def update(self):
|
||||
def update(self, objects):
|
||||
for room in self.rooms:
|
||||
if room.id == self.current:
|
||||
room.update()
|
||||
room.update(objects)
|
||||
self.background = room.background
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_RIGHT]:
|
||||
|
|
@ -216,7 +217,6 @@ class Room(GameObjects):
|
|||
self.locked = False
|
||||
[self.objects[3].append(wall) for wall in self.genWalls(WIDTH, HEIGHT)]
|
||||
|
||||
|
||||
def genWalls(self, WIDTH, HEIGHT):
|
||||
walls = []
|
||||
walls.append(Obstacle('wall_l', 'wall', None, True, 32, 32, True, WIDTH=4, HEIGHT=HEIGHT))
|
||||
|
|
@ -225,7 +225,10 @@ class Room(GameObjects):
|
|||
walls.append(Obstacle('wall_b', 'wall', None, True, 32, HEIGHT + 28, True, WIDTH=WIDTH, HEIGHT=4))
|
||||
return walls
|
||||
|
||||
def update(self):
|
||||
def update(self, objects):
|
||||
self.objects = objects
|
||||
if not self.objects[1]:
|
||||
self.locked = False
|
||||
return
|
||||
|
||||
def draw(self, screen):
|
||||
|
|
|
|||
13
main.py
13
main.py
|
|
@ -31,12 +31,13 @@ def genRooms(WIDTH, HEIGHT, type:str, objects:list):
|
|||
room_objects = [Obstacle('dirt', 'boulder', 'art/images/dirt2.png', False, 32, 32, WIDTH=WIDTH - 64, HEIGHT=HEIGHT - 64)]
|
||||
room_objects.append(Obstacle('river', 'water', 'art/images/river1.png', True, 32, 32, WIDTH=WIDTH - 64, HEIGHT=HEIGHT - 64))
|
||||
rooms = [
|
||||
Room(type, 'normal', f'art/images/{type}.png', [objects[0], objects[1], objects[2], [room_objects[i] for i in range(0, random.randint(0, len(room_objects)))]], WIDTH - 64, HEIGHT - 64, [True, True, True, False], 0),
|
||||
Room(type, 'normal', f'art/images/{type}.png', [objects[0], objects[1], objects[2], [room_objects[i] for i in range(0, random.randint(0, len(room_objects)))]], WIDTH - 64, HEIGHT - 64, [True, True, True, False], 1),
|
||||
Room(type, 'normal', f'art/images/{type}.png', [objects[0], objects[1], objects[2], [room_objects[i] for i in range(0, random.randint(0, len(room_objects)))]], WIDTH - 64, HEIGHT - 64, [True, True, True, False], 2),
|
||||
Room(type, 'normal', f'art/images/{type}.png', [objects[0], objects[1], objects[2], [room_objects[random.randint(0, len(room_objects) - 1)] for i in range(0, 5)]], WIDTH - 64, HEIGHT - 64, [True, True, True, False], 0),
|
||||
Room(type, 'normal', f'art/images/{type}.png', [objects[0], objects[1], objects[2], [room_objects[random.randint(0, len(room_objects) - 1)] for i in range(0, 5)]], WIDTH - 64, HEIGHT - 64, [True, True, True, False], 1),
|
||||
Room(type, 'normal', f'art/images/{type}.png', [objects[0], objects[1], objects[2], [room_objects[random.randint(0, len(room_objects) - 1)] for i in range(0, 5)]], WIDTH - 64, HEIGHT - 64, [True, True, True, False], 2),
|
||||
]
|
||||
return rooms
|
||||
|
||||
|
||||
def play(screen, clock, running, background, isblack, WIDTH, HEIGHT):
|
||||
main = [MainCharacter('Herbert', 100, 'oldman.png', 500, 500, 20, 5, 1, 1, 50)]
|
||||
mobs = [Skeleton(i, random.randint(40, 60), 'reddy.png', random.randint(20,1000), random.randint(20,700), 5, 1, 1, 1, 200) for i in range(0,random.randint(2, 8))]
|
||||
|
|
@ -66,7 +67,7 @@ def play(screen, clock, running, background, isblack, WIDTH, HEIGHT):
|
|||
screen.blit(bg, (0, 0))
|
||||
"""
|
||||
if not freeze:
|
||||
scene.update(False)
|
||||
scene.update(False, objects)
|
||||
objects = scene.getObjects()
|
||||
screen.blit(scene.background, (32, 32))
|
||||
for thing in objects[3]:
|
||||
|
|
@ -135,7 +136,7 @@ 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, 'textbox.png', 'medieval', 48, "Play", play))
|
||||
objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2, 160, 64, 'textbox.png', 'medieval', 48, "Play", play, attributes=[screen, clock, running, background, isblack, WIDTH, HEIGHT]))
|
||||
#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:
|
||||
|
|
@ -150,7 +151,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.update(screen, clock, running, background, isblack, WIDTH, HEIGHT)
|
||||
obj.update(screen)
|
||||
|
||||
# flip() the display to put your work on screen
|
||||
pygame.display.flip()
|
||||
|
|
|
|||
14
viecher.py
14
viecher.py
|
|
@ -1,5 +1,6 @@
|
|||
import pygame as pg
|
||||
from classes import *
|
||||
from main import *
|
||||
|
||||
vec = pg.math.Vector2
|
||||
fps = 60
|
||||
|
|
@ -170,11 +171,11 @@ class MainCharacter(Fighter):
|
|||
if touches.name == 'wall_l':
|
||||
self.x += (2 + (self.x - touches.rect.x))
|
||||
elif touches.name == 'wall_r':
|
||||
self.x -= (self.rect.width - (touches.rect.x - self.x))
|
||||
self.x -= (2 + self.rect.width - (touches.rect.x - self.x))
|
||||
if touches.name == 'wall_t':
|
||||
self.y += (2 + (self.y - touches.rect.y))
|
||||
elif touches.name == 'wall_b':
|
||||
self.y -= (self.rect.height - (touches.rect.y - self.y))
|
||||
self.y -= (2 + self.rect.height - (touches.rect.y - self.y))
|
||||
return
|
||||
|
||||
if self.x <= touches.rect.x: self.x -= (self.rect.width - (touches.rect.x - self.x))
|
||||
|
|
@ -312,15 +313,14 @@ class Book():
|
|||
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.append(Button(200, self.buttons_height, 58, 50, f'{spell}.png', 'medieval', 23, attributes=[spell], onclickFunction=self.update_spell))
|
||||
self.buttons_height += 100
|
||||
|
||||
def update_spell(self):
|
||||
self.current_sp = None
|
||||
|
||||
def update_spell(self, spell):
|
||||
self.current_sp = spell
|
||||
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue