Compare commits

..

2 commits

Author SHA1 Message Date
9147971be8 added house 2024-03-09 20:14:40 +01:00
52d3ec6e8d fixed that herberts attributes stay the same 2024-03-09 16:26:53 +01:00
6 changed files with 121 additions and 48 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 771 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

View file

Before

Width:  |  Height:  |  Size: 626 B

After

Width:  |  Height:  |  Size: 626 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)

111
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)
if result == 'village':
village(screen, clock, running, background, isblack, WIDTH, HEIGHT) village(screen, clock, running, background, isblack, WIDTH, HEIGHT)
thing.draw(screen) 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)
@ -109,14 +111,17 @@ 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): def village(screen, clock, running, background, isblack, WIDTH, HEIGHT):
main = [MainCharacter('Herbert', 100, 'people/oldman.png', 500, 500, 20, 5, 1, 1, 50)] main = [herbert]
mobs = [] mobs = []
weapons = [] weapons = []
others = [Fire('f1', 0, 200, 300), Portal('p1', 0, 1000, 500)] others = [Obstacle('fireplace', 'interactable', 'art/images/background/fireplace.png', False, 500, 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('name', 100, 'people/oldlady.png', 1, 200, 200)] npcs = [NPC('name', 100, 'people/oldlady.png', 1, 200, 200)]
objects = [main, mobs, npcs, weapons, others] 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) 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 freeze = True #Gameplay is freezed in certain situations
main[0].health.health = 20
while running: while running:
screen.fill('#000000') screen.fill('#000000')
@ -139,8 +144,6 @@ def village(screen, clock, running, background, isblack, WIDTH, HEIGHT):
screen.blit(room.background, (32, 32)) screen.blit(room.background, (32, 32))
for thing in objects[4]: for thing in objects[4]:
if thing.update(objects):
play(screen, clock, running, background, isblack, WIDTH, HEIGHT)
thing.draw(screen) thing.draw(screen)
for weapon in objects[3]: for weapon in objects[3]:
@ -149,9 +152,15 @@ def village(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(), pygame.mouse.get_pos(), objects): result = thing.update(pygame.key.get_pressed(), pygame.mouse.get_pos(), objects)
if result == 'village':
menu(screen, clock, running, background, isblack, WIDTH, HEIGHT) menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
thing.draw(screen) 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]: for mob in objects[1]:
mob.update(objects) mob.update(objects)
@ -171,7 +180,74 @@ def village(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()
clock.tick(fps) # limits FPS to 60 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', 1, 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 = []
@ -329,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

@ -136,6 +136,20 @@ class MainCharacter(Fighter):
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]:
@ -155,7 +169,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':
@ -211,9 +225,9 @@ class MainCharacter(Fighter):
self.attack(objects, vec(mouse)) self.attack(objects, vec(mouse))
self.thinks.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:
@ -418,7 +432,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)
@ -469,28 +484,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)
return False
class Portal(Objects):
def __init__(self, name, ms, x, y, sprite='portal.png') -> None:
super().__init__(name, ms, sprite, x, y)
def update(self, objects):
touches = pg.sprite.spritecollideany(self, objects[0])
if touches is not None and isinstance(touches, MainCharacter):
return True
else:
return False