Changed Book and fixed NPC convo #75

Merged
Spafi merged 11 commits from Lyzzy/game:main into Development 2024-03-10 09:16:42 +01:00
8 changed files with 251 additions and 62 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.type = _type
self.background = bg self.background = bg
if bg != None: if bg != None:
with open(bg, 'r') as bg: if WIDTH != None and HEIGHT != None:
self.background = pygame.transform.scale(pygame.image.load(bg), [WIDTH, HEIGHT]) 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 self.objects = objects
def update(self, objects): def update(self, objects):
@ -287,3 +291,5 @@ class Obstacle(GameObjects):
else: else:
pygame.draw.rect(screen, '#e7f8e0', self.rect, 2) pygame.draw.rect(screen, '#e7f8e0', self.rect, 2)

167
main.py
View file

@ -41,12 +41,11 @@ def genRooms(WIDTH, HEIGHT, type:str, objects:list):
return rooms return rooms
def play(screen, clock, running, background, isblack, WIDTH, HEIGHT): 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))] 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 = [] weapons = []
others = [Fire('f1', 0, 200, 300)] others = []
npcs = [NPC('name', 100, 'people/oldlady.png', 1, 200, 200)] npcs = []
objects = [main, mobs, npcs, weapons, others] objects = [main, mobs, npcs, weapons, others]
level = [] level = []
rooms = genRooms(WIDTH, HEIGHT, 'grass', objects) rooms = genRooms(WIDTH, HEIGHT, 'grass', objects)
@ -74,19 +73,22 @@ def play(screen, clock, running, background, isblack, WIDTH, HEIGHT):
objects = scene.getObjects() objects = scene.getObjects()
screen.blit(scene.background, (32, 32)) screen.blit(scene.background, (32, 32))
for thing in objects[4]:
thing.draw(screen)
for weapon in objects[3]: for weapon in objects[3]:
weapon.update(objects) weapon.update(objects)
weapon.draw(screen) weapon.draw(screen)
for thing in objects[4]:
thing.update(objects)
thing.draw(screen)
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(), pygame.mouse.get_pos(), objects): result = thing.update(pygame.key.get_pressed(), pygame.mouse.get_pos(), objects)
menu(screen, clock, running, background, isblack, WIDTH, HEIGHT) if result == 'village':
thing.draw(screen) 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]: for mob in objects[1]:
mob.update(objects) mob.update(objects)
@ -99,7 +101,6 @@ def play(screen, clock, running, background, isblack, WIDTH, HEIGHT):
objects[0][0].book.addspell('windslash') objects[0][0].book.addspell('windslash')
scene.update(False, objects) scene.update(False, objects)
else: else:
objects[0][0].book.hidden = not freeze objects[0][0].book.hidden = not freeze
objects[0][0].book.draw(screen) 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 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): def options(screen, clock, running, background, isblack, WIDTH, HEIGHT):
objects = [] objects = []
# List that is displayed while selecting the window resolution level # 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): def menu(screen, clock, running, background, isblack, WIDTH, HEIGHT):
objects = [] 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, "Options", uwu))
objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 + 72, 160, 64, 'textbox.png', '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:
@ -265,4 +405,5 @@ def main():
pygame.quit() pygame.quit()
if __name__ == '__main__': if __name__ == '__main__':
herbert = MainCharacter('Herbert', 100, 'people/oldman.png', 500, 500, 20, 5, 1, 1, 50)
main() main()

View file

@ -58,16 +58,16 @@ class Objects():
pg.draw.rect(screen, '#ef0120', self.rect, 2) pg.draw.rect(screen, '#ef0120', self.rect, 2)
class NPC(Objects): 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) super().__init__(name, ms, sprite, x, y)
self.talking = False self.talking = False
self.hidden = 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): def talk(self, objects):
self.talking = True self.talking = True
objects[0][0].talking = True objects[0][0].talking = True
self.conversation.hidden = False
def draw(self, screen): def draw(self, screen):
super().draw(screen) super().draw(screen)
@ -75,21 +75,49 @@ class NPC(Objects):
self.conversation.draw(screen) self.conversation.draw(screen)
def update(self, keys, objects): def update(self, keys, objects):
if self.talking: if self.lastUpdate + 200 < pg.time.get_ticks():
self.conversation.update(keys, objects) 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): 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) 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): def update(self, keys, objects):
if keys[pg.K_SPACE]: if keys[pg.K_SPACE]:
objects[0][0].book.addspell('fireball') convo = self.findConversation()
self.talking = False if self.convo_act+1 < len(convo):
objects[0][0].talking = False self.text = convo[self.convo_act]
self.hidden = True self.convo_act += 1
else:
self.convo_act = 0
self.npc.talking = False
objects[0][0].talking = False
class Fighter(Objects): class Fighter(Objects):
@ -112,7 +140,7 @@ class MainCharacter(Fighter):
self.talking = False self.talking = False
self.level = Level(1000, 38, 150, 40, f'will to live: {level}%', 'simple', 20, ) 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.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 self.freezing = True
def draw(self, screen): def draw(self, screen):
@ -124,13 +152,27 @@ class MainCharacter(Fighter):
self.level.draw(screen) self.level.draw(screen)
self.book.draw(screen) self.book.draw(screen)
pg.draw.rect(screen, '#e900fa', self.rect, 2) pg.draw.rect(screen, '#e900fa', self.rect, 2)
if self.speech.hidden == False: if self.thinks.hidden == False:
self.speech.draw(screen, self.x+20, self.y-100) self.thinks.draw(screen, self.x+20, self.y-100)
def hurt(self, damage, objects): def hurt(self, damage, objects):
if not self.talking: if not self.talking:
self.health.hurt(damage) 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): def walk(self, keys, objects):
moveto = vec(0, 0) moveto = vec(0, 0)
if keys[pg.K_w] or keys[pg.K_UP]: 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 touches is not None and not isinstance(touches, Weapons):
if isinstance(touches, Obstacle): if isinstance(touches, Obstacle):
if not touches.collision: if not touches.collision:
print(touches.name) # print(touches.name)
return return
if touches.type == 'wall': if touches.type == 'wall':
if touches.name == 'wall_l': if touches.name == 'wall_l':
@ -204,11 +246,11 @@ class MainCharacter(Fighter):
self.walk(keys, objects) self.walk(keys, objects)
if pg.mouse.get_pressed()[0]: if pg.mouse.get_pressed()[0]:
self.attack(objects, vec(mouse)) self.attack(objects, vec(mouse))
self.speech.update(self) self.thinks.update(self)
if self.health.health <= 0: if self.health.health <= 0:
return False return 'village'
else: else:
return True return self.obstacle_interaction(objects)
class Hearts(): class Hearts():
def __init__(self, health, sprite, x, y, hurtCooldown) -> None: 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: 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) super().__init__(x, y, width, height, text, font, font_size, font_color, sprite)
class Speech(Label): class Thinks(Label):
def __init__(self, x, y, width, height, text, font='simple', font_size=15, font_color='#000000', sprite='speech.png') -> None: 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) super().__init__(x, y, width, height, text, font, font_size, font_color, sprite)
def draw(self, screen, x, y): def draw(self, screen, x, y):
@ -285,24 +327,36 @@ class Book():
self.sprite = pg.transform.scale(self.sprite, (1280, 720)) self.sprite = pg.transform.scale(self.sprite, (1280, 720))
self.x = x self.x = x
self.y = y 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.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.labels = [Label(100, 100, 500, 50, "Dear User, ", 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:",
Label(100, 150, 500, 50, "this book will help you to survive.", font_color='#000000', sprite='empty.png'), "You are a homeless person.","One cold day you went to the library to get warm.",
Label(100, 200, 500, 50, "Click on a picture to choose your spell.", font_color='#000000', sprite='empty.png'), "There you saw and opened me out of boedom.", "This lead to you being thrown in this world.",
Label(100, 250, 500, 50, "Talk to fairies to unlock new spells!", font_color='#000000', sprite='empty.png')] "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=[]
self.buttons_height = 400 self.buttons_y = 400
self.buttons_x = 800
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 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) 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: for button in self.buttons:
button.update(screen) button.update(screen)
@ -310,8 +364,8 @@ class Book():
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}_icon.png', 'medieval', 23, attributes=[spell], onclickFunction=self.update_spell)) 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_height += 100 self.buttons_y += 100
def update_spell(self, spell): def update_spell(self, spell):
self.current_sp = spell self.current_sp = spell
@ -401,7 +455,8 @@ class Weapons(Objects):
if touches is not None and isinstance(touches, kills): if touches is not None and isinstance(touches, kills):
touches.hurt(self.damage, objects) touches.hurt(self.damage, objects)
self.hidden = True self.hidden = True
objects[3].remove(self) if self in objects[3]:
objects[3].remove(self)
def move(self, objects): def move(self, objects):
self.moveto.scale_to_length(self.speed) self.moveto.scale_to_length(self.speed)
@ -452,16 +507,3 @@ class Punch(Weapons):
def update(self, objects): def update(self, objects):
self.move(objects) self.move(objects)
self.die(objects, MainCharacter) 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)