Merge branch 'Development' of https://git.spafi.eu/InfoProjekt/game into InfoProjekt-Development3

Signed-off-by: SpagettiFisch <63868515+SpagettiFisch@users.noreply.github.com>
This commit is contained in:
SpagettiFisch 2024-03-10 09:23:59 +01:00
commit 0032f8b2e1
8 changed files with 249 additions and 64 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 771 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 954 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 626 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View file

Before

Width:  |  Height:  |  Size: 329 B

After

Width:  |  Height:  |  Size: 329 B

View file

@ -171,8 +171,12 @@ class GameObjects():
self.type = _type
self.background = bg
if bg != None:
with open(bg, 'r') as bg:
self.background = pygame.transform.scale(pygame.image.load(bg), [WIDTH, HEIGHT])
if WIDTH != None and HEIGHT != None:
with open(bg, 'r') as bg:
self.background = pygame.transform.scale(pygame.image.load(bg), [WIDTH, HEIGHT])
else:
with open(bg, 'r') as bg:
self.background = pygame.transform.scale2x(pygame.image.load(bg))
self.objects = objects
def update(self, objects):
@ -234,8 +238,6 @@ class Stage(GameObjects):
if room.id == self.current:
return room.getObjects()
def placeRooms(self):
class Room(GameObjects):
def __init__(self, name:str, _type:str, bg, objects:list, WIDTH, HEIGHT, exits:list, id:int) -> None:
super().__init__(name, _type, bg, objects, WIDTH, HEIGHT)

169
main.py
View file

@ -37,16 +37,15 @@ def genRooms(WIDTH, HEIGHT, type:str, objects:list):
for j in range(random.randint(5, 10))
]
#rooms =Room(type, 'normal', room_backgrounds[random.randint(0, 4)], [objects[0], objects[1], objects[2], [room_objects[random.randint(0, len(room_objects) - 1)] for i in range(0, random.randint(0, 1))]], WIDTH - 64, HEIGHT - 64, [True, True, True, True], j)
return rooms
def play(screen, clock, running, background, isblack, WIDTH, HEIGHT):
main = [MainCharacter('Herbert', 100, 'people/oldman.png', 500, 500, 20, 5, 1, 1, 50)]
main = [herbert]
mobs = [Skeleton(i, random.randint(40, 60), random.randint(50, WIDTH - 50), random.randint(50, HEIGHT - 50), 5, 1, 1, 1, 200) for i in range(0,random.randint(2, 8))]+[Zombie(i, random.randint(40, 60), random.randint(50, WIDTH-50), random.randint(50, HEIGHT-50), 5, 1, 1, 1, 100) for i in range(0,random.randint(2, 8))]
weapons = []
others = [Fire('f1', 0, 200, 300)]
npcs = [NPC('name', 100, 'people/oldlady.png', 1, 200, 200)]
others = []
npcs = []
objects = [main, mobs, npcs, weapons, others]
level = []
rooms = genRooms(WIDTH, HEIGHT, 'grass', objects)
@ -74,19 +73,22 @@ def play(screen, clock, running, background, isblack, WIDTH, HEIGHT):
objects = scene.getObjects()
screen.blit(scene.background, (32, 32))
for thing in objects[4]:
thing.draw(screen)
for weapon in objects[3]:
weapon.update(objects)
weapon.draw(screen)
for thing in objects[4]:
thing.update(objects)
thing.draw(screen)
for thing in objects[0]:
thing.book.hidden = not freeze
if not thing.update(pygame.key.get_pressed(), pygame.mouse.get_pos(), objects):
menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
thing.draw(screen)
result = thing.update(pygame.key.get_pressed(), pygame.mouse.get_pos(), objects)
if result == 'village':
village(screen, clock, running, background, isblack, WIDTH, HEIGHT)
elif result == 'play':
play(screen, clock, running, background, isblack, WIDTH, HEIGHT)
else:
thing.draw(screen)
for mob in objects[1]:
mob.update(objects)
@ -99,7 +101,6 @@ def play(screen, clock, running, background, isblack, WIDTH, HEIGHT):
objects[0][0].book.addspell('windslash')
scene.update(False, objects)
else:
objects[0][0].book.hidden = not freeze
objects[0][0].book.draw(screen)
@ -109,6 +110,145 @@ def play(screen, clock, running, background, isblack, WIDTH, HEIGHT):
clock.tick(fps) # limits FPS to 60
def village(screen, clock, running, background, isblack, WIDTH, HEIGHT):
main = [herbert]
mobs = []
weapons = []
others = [Obstacle('fireplace', 'interactable', 'art/images/background/fireplace.png', False, 200, 500),
Obstacle('portal', 'interactable', 'art/images/background/portal.png', False, 700, 300),
Obstacle('house', 'Interactable', 'art/images/background/house.png', False, 500, 150, WIDTH=180, HEIGHT=160)]
npcs = [NPC('oldlady', 100, 'people/oldlady.png', 0, 200, 200)]
objects = [main, mobs, npcs, weapons, others]
room = Room('village', 'village', 'art/images/background/village.png', objects, WIDTH - 64, HEIGHT - 64, [True, True, True, True], 0)
freeze = True #Gameplay is freezed in certain situations
main[0].health.health = 20
while running:
screen.fill('#000000')
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_e: #when book is open gameplay is freezed
freeze = not freeze
# RENDER YOUR GAME HERE
"""with open(background, 'r') as i:
bg = pygame.image.load(i)
bg = pygame.transform.scale(bg, (WIDTH, HEIGHT))
# fill the screen with an image to clear the screen
screen.blit(bg, (0, 0))
"""
if not freeze:
objects = room.getObjects()
screen.blit(room.background, (32, 32))
for thing in objects[4]:
thing.draw(screen)
for weapon in objects[3]:
weapon.update(objects)
weapon.draw(screen)
for thing in objects[0]:
thing.book.hidden = not freeze
result = thing.update(pygame.key.get_pressed(), pygame.mouse.get_pos(), objects)
if result == 'village':
menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
elif result == 'play':
play(screen, clock, running, background, isblack, WIDTH, HEIGHT)
elif result == 'house':
house(screen, clock, running, background, isblack, WIDTH, HEIGHT)
else:
thing.draw(screen)
for mob in objects[1]:
mob.update(objects)
mob.draw(screen)
for npc in objects[2]:
npc.update(pygame.key.get_pressed(), objects)
npc.draw(screen)
objects[0][0].book.addspell('windslash')
room.update(objects)
else:
objects[0][0].book.hidden = not freeze
objects[0][0].book.draw(screen)
objects[0][0].book.update()
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(fps) # limits FPS to
def house(screen, clock, running, background, isblack, WIDTH, HEIGHT):
main = [herbert]
mobs = []
weapons = []
others = []
npcs = [NPC('oldman', 100, 'people/reddy.png', 0, 200, 200)]
objects = [main, mobs, npcs, weapons, others]
room = Room('house', 'house', 'art/images/background/insideHouse.png', objects, WIDTH - 64, HEIGHT - 64, [True, True, True, True], 0)
freeze = False #Gameplay is freezed in certain situations
while running:
screen.fill('#000000')
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_e: #when book is open gameplay is freezed
freeze = not freeze
# RENDER YOUR GAME HERE
"""with open(background, 'r') as i:
bg = pygame.image.load(i)
bg = pygame.transform.scale(bg, (WIDTH, HEIGHT))
# fill the screen with an image to clear the screen
screen.blit(bg, (0, 0))
"""
if not freeze:
objects = room.getObjects()
screen.blit(room.background, (32, 32))
for thing in objects[4]:
thing.draw(screen)
# for weapon in objects[3]:
# weapon.update(objects)
# weapon.draw(screen)
for thing in objects[0]:
thing.book.hidden = not freeze
result = thing.update(pygame.key.get_pressed(), pygame.mouse.get_pos(), objects)
if result == 'village':
menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
elif result == 'play':
play(screen, clock, running, background, isblack, WIDTH, HEIGHT)
elif result == 'wall':
village(screen, clock, running, background, isblack, WIDTH, HEIGHT)
else:
thing.draw(screen)
for npc in objects[2]:
npc.update(pygame.key.get_pressed(), objects)
npc.draw(screen)
objects[0][0].book.addspell('windslash')
room.update(objects)
else:
objects[0][0].book.hidden = not freeze
objects[0][0].book.draw(screen)
objects[0][0].book.update()
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(fps) # limits FPS to
def options(screen, clock, running, background, isblack, WIDTH, HEIGHT):
objects = []
# List that is displayed while selecting the window resolution level
@ -148,7 +288,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, attributes=[screen, clock, running, background, isblack, WIDTH, HEIGHT]))
objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2, 160, 64, 'textbox.png', 'medieval', 48, "Play", village, 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:
@ -265,4 +405,5 @@ def main():
pygame.quit()
if __name__ == '__main__':
herbert = MainCharacter('Herbert', 100, 'people/oldman.png', 500, 500, 20, 5, 1, 1, 50)
main()

View file

@ -58,38 +58,66 @@ class Objects():
pg.draw.rect(screen, '#ef0120', self.rect, 2)
class NPC(Objects):
def __init__(self, name, ms, sprite, convo_act, x, y) -> None:
def __init__(self, name, ms, sprite, convo_scene, x, y) -> None:
super().__init__(name, ms, sprite, x, y)
self.talking = False
self.hidden = False
self.conversation = Convo('Hello, you can shoot fireballs with f now.', convo_act, 'person')
self.conversation = Convo(self, convo_scene)
self.lastUpdate = pg.time.get_ticks()
def talk(self, objects):
self.talking = True
objects[0][0].talking = True
self.conversation.hidden = False
def draw(self, screen):
super().draw(screen)
if self.talking:
self.conversation.draw(screen)
def update(self, keys, objects):
if self.talking:
self.conversation.update(keys, objects)
def update(self, keys, objects):
if self.lastUpdate + 200 < pg.time.get_ticks():
if self.talking:
self.conversation.update(keys, objects)
self.lastUpdate = pg.time.get_ticks()
else:
touches = pg.sprite.spritecollideany(self, objects[0])
if touches is not None and keys[pg.K_SPACE] and isinstance(touches, MainCharacter):
self.talk(objects)
self.lastUpdate = pg.time.get_ticks()
class Convo(Label):
def __init__(self, text, convo_act, person, x = 140, y = 600, width = 1000, height = 100, font='simple', font_size = 20) -> None:
def __init__(self, npc, convo_scene, text='', x = 140, y = 600, width = 1000, height = 100, font='simple', font_size = 20) -> None:
super().__init__(x, y, width, height, text, font, font_size)
self.convo_act=0
self.npc = npc
self.convo_scene = convo_scene
self.convos = [
['oldlady', 0, ['Hello', 'How are you?']],
['oldman', 0, ['Please help', 'there are so many bad people']]
]
def draw(self, screen):
self.text = self.findConversation()[self.convo_act]
super().draw(screen)
def findConversation(self):
for convo in self.convos:
if convo[0] == self.npc.name and convo[1] == self.convo_scene:
return convo[2]
return ['ERROR']
def update(self, keys, objects):
if keys[pg.K_SPACE]:
objects[0][0].book.addspell('fireball')
self.talking = False
objects[0][0].talking = False
self.hidden = True
convo = self.findConversation()
if self.convo_act+1 < len(convo):
self.text = convo[self.convo_act]
self.convo_act += 1
else:
self.convo_act = 0
self.npc.talking = False
objects[0][0].talking = False
class Fighter(Objects):
@ -112,7 +140,7 @@ class MainCharacter(Fighter):
self.talking = False
self.level = Level(1000, 38, 150, 40, f'will to live: {level}%', 'simple', 20, )
self.health = Hearts(health, sprite=['fullheart.png', 'fullheart.png', 'fullheart.png', 'fullheart.png', 'fullheart.png'], x=900, y= 50, hurtCooldown=self.hurtCooldown)
self.speech = Speech(self.x+20, self.y-50, 150, 100, 'brr Im freezing')
self.thinks = Thinks(self.x+20, self.y-50, 150, 100, 'brr Im freezing')
self.freezing = True
def draw(self, screen):
@ -124,13 +152,27 @@ class MainCharacter(Fighter):
self.level.draw(screen)
self.book.draw(screen)
pg.draw.rect(screen, '#e900fa', self.rect, 2)
if self.speech.hidden == False:
self.speech.draw(screen, self.x+20, self.y-100)
if self.thinks.hidden == False:
self.thinks.draw(screen, self.x+20, self.y-100)
def hurt(self, damage, objects):
if not self.talking:
self.health.hurt(damage)
def obstacle_interaction(self, objects):
touches = pg.sprite.spritecollideany(self, objects[4])
if touches is not None:
if touches.name == 'fireplace':
self.freezing = False
elif touches.name == 'portal':
return 'play'
elif touches.name == 'house':
return 'house'
elif 'wall' in touches.name:
return 'wall'
else:
return True
def walk(self, keys, objects):
moveto = vec(0, 0)
if keys[pg.K_w] or keys[pg.K_UP]:
@ -150,7 +192,7 @@ class MainCharacter(Fighter):
if touches is not None and not isinstance(touches, Weapons):
if isinstance(touches, Obstacle):
if not touches.collision:
print(touches.name)
# print(touches.name)
return
if touches.type == 'wall':
if touches.name == 'wall_l':
@ -204,11 +246,11 @@ class MainCharacter(Fighter):
self.walk(keys, objects)
if pg.mouse.get_pressed()[0]:
self.attack(objects, vec(mouse))
self.speech.update(self)
self.thinks.update(self)
if self.health.health <= 0:
return False
return 'village'
else:
return True
return self.obstacle_interaction(objects)
class Hearts():
def __init__(self, health, sprite, x, y, hurtCooldown) -> None:
@ -262,8 +304,8 @@ class Level(Label):
def __init__(self, x, y, width, height, text, font='simple', font_size=20, font_color='#1e90ff', sprite='label.png') -> None:
super().__init__(x, y, width, height, text, font, font_size, font_color, sprite)
class Speech(Label):
def __init__(self, x, y, width, height, text, font='simple', font_size=15, font_color='#000000', sprite='speech.png') -> None:
class Thinks(Label):
def __init__(self, x, y, width, height, text, font='simple', font_size=15, font_color='#000000', sprite='thinks.png') -> None:
super().__init__(x, y, width, height, text, font, font_size, font_color, sprite)
def draw(self, screen, x, y):
@ -285,24 +327,36 @@ class Book():
self.sprite = pg.transform.scale(self.sprite, (1280, 720))
self.x = x
self.y = y
self.hidden = True
self.hidden = False
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.labels = [Label(100, 100, 500, 50, "Dear User, ", font_color='#000000', sprite='empty.png'),
Label(100, 150, 500, 50, "this book will help you to survive.", font_color='#000000', sprite='empty.png'),
Label(100, 200, 500, 50, "Click on a picture to choose your spell.", font_color='#000000', sprite='empty.png'),
Label(100, 250, 500, 50, "Talk to fairies to unlock new spells!", font_color='#000000', sprite='empty.png')]
self.text_left = ["Dear User, ", "in case you fell on the ground too hard,", "here is a quick reminder:",
"You are a homeless person.","One cold day you went to the library to get warm.",
"There you saw and opened me out of boedom.", "This lead to you being thrown in this world.",
"But you can find a way out of here again."]
self.text_right = ["This book will help you to survive.", "Click on a picture to choose your spell.",
"Talk to fairies to unlock new spells!"]
self.buttons=[]
self.buttons_height = 400
self.buttons_y = 400
self.buttons_x = 800
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 label in self.labels:
text_left_y = 100
text_right_y = 100
for text in self.text_left:
label = Label(100, text_left_y, 500, 50, text, font_color='#000000', sprite='empty.png')
label.draw(screen)
text_left_y += 50
for text in self.text_right:
label = Label(680, text_right_y, 500, 50, text, font_color='#000000', sprite='empty.png')
label.draw(screen)
text_right_y += 50
for button in self.buttons:
button.update(screen)
@ -310,8 +364,8 @@ 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}_icon.png', 'medieval', 23, attributes=[spell], onclickFunction=self.update_spell))
self.buttons_height += 100
self.buttons.append(Button(self.buttons_x, self.buttons_y, 58, 50, f'{spell}_icon.png', 'medieval', 23, attributes=[spell], onclickFunction=self.update_spell))
self.buttons_y += 100
def update_spell(self, spell):
self.current_sp = spell
@ -401,7 +455,8 @@ class Weapons(Objects):
if touches is not None and isinstance(touches, kills):
touches.hurt(self.damage, objects)
self.hidden = True
objects[3].remove(self)
if self in objects[3]:
objects[3].remove(self)
def move(self, objects):
self.moveto.scale_to_length(self.speed)
@ -452,16 +507,3 @@ class Punch(Weapons):
def update(self, objects):
self.move(objects)
self.die(objects, MainCharacter)
class Fire(Objects):
def __init__(self, name, ms, x, y, sprite='background/fireplace.png') -> None:
super().__init__(name, ms, sprite, x, y)
def warming(self, objects):
touches = pg.sprite.spritecollideany(self, objects[0])
if touches is not None and isinstance(touches, MainCharacter):
touches.freezing = False
def update(self, objects):
self.warming(objects)