added some missing spaces lol

added character collisions (not great yet tho)
some simple background elements added and testet WIP
This commit is contained in:
SpagettiFisch 2024-02-29 14:25:23 +01:00
parent c741c8199a
commit 218eb2ea7e
9 changed files with 161 additions and 49 deletions

BIN
art/images/dirt1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

BIN
art/images/dirt2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

BIN
art/images/grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 131 KiB

BIN
art/images/river1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

View file

@ -138,23 +138,93 @@ class DropDown():
class GameObjects():
def __init__(self, name:str, _type:str, bg, objects:list) -> None:
def __init__(self, name:str, _type:str, bg, objects:list, WIDTH, HEIGHT) -> None:
self.name = name
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])
self.objects = objects
class Scene(GameObjects):
def __init__(self, name:str, _type:str, bg, objects:list) -> None:
super().__init__(name, _type, bg, objects)
def __init__(self, name:str, _type:str, bg, objects:list | None, WIDTH, HEIGHT, level:list) -> None:
super().__init__(name, _type, bg, objects, WIDTH, HEIGHT)
self.level = level
self.current_level = 0
def update(self, change:bool):
if change:
self.current_level += 1
self.level[self.current_level].update()
if isinstance(self.objects, list):
for obj in self.objects:
obj.update()
def draw(self, screen):
if isinstance(self.objects, list):
for obj in self.objects:
obj.draw(screen)
self.level[self.current_level].draw(screen)
class Stage(GameObjects):
def __init__(self, name: str, _type: str, bg, objects: list, WIDTH, HEIGHT, stage:str, rooms:list) -> None:
super().__init__(name, _type, bg, objects, WIDTH, HEIGHT)
self.stage = stage
self.rooms = rooms
self.current = 0
def update(self):
for room in self.rooms:
if room.id == self.current:
room.update()
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
self.current += 1
if self.current >= len(self.rooms):
return 1
def draw(self, screen):
for room in self.rooms:
if room.id == self.current:
room.draw(screen)
class Room(GameObjects):
def __init__(self, name:str, _type:str, bg, objects:list, exits:list) -> None:
super().__init__(name, _type, bg, objects)
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)
self.exits = exits
self.id = id
if self.type == 'normal' or self.type == 'boss':
self.locked = True
else:
self.locked = False
self.objects.append(self.genWalls(WIDTH, HEIGHT))
def genWalls(self, WIDTH, HEIGHT):
walls = []
walls.append(pygame.Rect(0, 0, 4, HEIGHT))
walls.append(pygame.Rect(WIDTH - 4, 0, 4, HEIGHT))
walls.append(pygame.Rect(0, 0, WIDTH, 4))
walls.append(pygame.Rect(0, HEIGHT - 4, WIDTH, 4))
return walls
def update(self):
pass
def draw(self, screen):
screen.blit(self.background, (32, 32))
if isinstance(self.objects, list):
for obj in self.objects[0]:
obj.draw(screen)
class Obstacle(GameObjects):
def __init__(self, name: str, _type: str, bg, collision: bool, x: int, y: int, hidden: bool=False, objects: list = None, WIDTH=None, HEIGHT=None) -> None:
super().__init__(name, _type, bg, objects, WIDTH, HEIGHT)
self.collision = collision
self.rect = pygame.Rect((x, y), self.background.get_size())
def draw(self, screen):
screen.blit(self.background, self.rect)

40
main.py
View file

@ -27,6 +27,16 @@ def quitGame():
pygame.quit()
quit()
def genRooms(WIDTH, HEIGHT, type:str):
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', [[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', [], WIDTH - 64, HEIGHT - 64, [True, True, True, False], 1),
Room(type, 'normal', f'art/images/{type}.png', [], 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), 125, 1, 1, 1, 200) for i in range(0,random.randint(2, 8))]
@ -47,7 +57,7 @@ def play(screen, clock, running, background, isblack, WIDTH, HEIGHT):
screen.blit(bg, (0, 0))
"""
for thing in objects[0]:
if thing.update(pygame.key.get_pressed()) ==False:
if not thing.update(pygame.key.get_pressed()):
menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
thing.draw(screen)
@ -126,6 +136,33 @@ def menu(screen, clock, running, background, isblack, WIDTH, HEIGHT):
clock.tick(60) # limits FPS to 60
def test(screen, clock, running, background, isblack, WIDTH, HEIGHT):
level = []
rooms = genRooms(WIDTH, HEIGHT, 'grass')
level.append(Stage('blau', 'normal', None, [], WIDTH, HEIGHT, 'blue', rooms))
level.append(Stage('rot', 'normal', None, [], WIDTH, HEIGHT, 'red', [
Room('red1', 'normal', 'art/images/grass.png', [], WIDTH, HEIGHT, [True, True, True, False], 0),
Room('red2', 'normal', 'art/images/grass.png', [], WIDTH, HEIGHT, [True, True, True, False], 1),
Room('red3', 'normal', 'art/images/grass.png', [], WIDTH, HEIGHT, [True, True, True, False], 2),
]))
scene = Scene('test', 'normal', None, None, WIDTH, HEIGHT, level)
# RENDER YOUR GAME HERE
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
quitGame()
screen.fill('#000000')
scene.update(False)
scene.draw(screen)
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(60) # limits FPS to 60
def main():
config = readConfig()
screen, clock, running, isblack, background, objects = setUp(config["screen"])
@ -133,6 +170,7 @@ def main():
#objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 - 72, 160, 64, '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 + 72, 160, 64, 'medieval', 48, "Exit game", quitGame))
test(screen, clock, running, background, isblack, WIDTH, HEIGHT)
menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
"""while running:
for event in pygame.event.get():

BIN
test1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 KiB

BIN
test2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 KiB

View file

@ -80,7 +80,7 @@ class MainCharacter(Fighter):
self.shield_spell = shield
self.talking = False
self.level = Level(1000, 38, level, 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)
self.health = Hearts(health, sprite=['fullheart.png', 'fullheart.png', 'fullheart.png', 'fullheart.png', 'fullheart.png'], x=900, y= 50)
def draw(self, screen):
if self.hidden:
@ -90,26 +90,30 @@ class MainCharacter(Fighter):
self.health.draw(screen)
self.level.draw(screen)
def hurt(self,damage):
def hurt(self, damage):
self.health.hurt(damage)
def walk(self,keys):
moveto=vec(0,0)
def walk(self, keys, objects):
moveto = vec(0, 0)
if keys[pg.K_w] or keys[pg.K_UP]:
moveto += vec(0,-1)
moveto += vec(0, -1)
if keys[pg.K_a] or keys[pg.K_LEFT]:
moveto += vec(-1,0)
moveto += vec(-1, 0)
if keys[pg.K_s] or keys[pg.K_DOWN]:
moveto += vec(0,1)
moveto += vec(0, 1)
if keys[pg.K_d] or keys[pg.K_RIGHT]:
moveto += vec(1,0)
if not moveto == vec(0,0):
moveto += vec(1, 0)
if not moveto == vec(0, 0):
moveto.scale_to_length(self.speed)
self.x += moveto[0] / fps
self.y += moveto[1] / fps
touches = pg.sprite.spritecollideany(self, objects[1] + objects[2])
if touches is not None:
self.x -= moveto[0] / fps #change later
self.y -= moveto[1] / fps #change later
def update(self, keys):
self.walk(keys)
def update(self, keys, objects):
self.walk(keys, objects)
if self.health.health <= 0:
return False
else:
@ -127,7 +131,7 @@ class Hearts():
for parts in sprite:
with open(f'art/images/{parts}') as i:
self.sprite.append(pg.image.load(i))
self.rect=[]
self.rect = []
for each in self.sprite:
self.rect.append(pg.Rect(self.x, self.y, each.get_width(), each.get_height()))
@ -140,24 +144,24 @@ class Hearts():
def draw(self, screen):
if self.hidden:
return
for i in range(0,5):
self.rect[i].x, self.rect[i].y = self.x+i*20, self.y
for i in range(0, 5):
self.rect[i].x, self.rect[i].y = self.x + i * 20, self.y
screen.blit(self.sprite[i], self.rect[i])
def update(self):
sprite = []
for i in range(0,5):
if self.health >= 4+4*i:
for i in range(0, 5):
if self.health >= 4 + 4 * i:
sprite.append('fullheart.png')
elif self.health == 3+4*i:
elif self.health == 3 + 4 * i:
sprite.append('dreiviertelheart.png')
elif self.health >= 2+4*i:
elif self.health >= 2 + 4 * i:
sprite.append('halfheart.png')
elif self.health >= 1+4*i:
elif self.health >= 1 + 4 * i:
sprite.append('viertelheart.png')
elif self.health <= 4*i:
elif self.health <= 4 * i:
sprite.append('noheart.png')
self.sprite=[]
self.sprite = []
for parts in sprite:
with open(f'art/images/{parts}') as i:
self.sprite.append(pg.image.load(i))
@ -180,8 +184,8 @@ class Level():
def draw(self, screen):
self.box.blit(self.labelSurf, [
self.labelRect.width/2 - self.labelSurf.get_rect().width/2,
self.labelRect.height/2 - self.labelSurf.get_rect().height/2
self.labelRect.width / 2 - self.labelSurf.get_rect().width / 2,
self.labelRect.height / 2 - self.labelSurf.get_rect().height / 2
])
screen.blit(self.box, self.labelRect)
@ -194,23 +198,23 @@ class Skeleton(Mobs):
def __init__(self, name, ms, sprite, x, y, health, damage, level, asp, atr, drops=0) -> None:
super().__init__(name, ms, sprite, x, y, health, damage, level, asp, atr, drops)
def chase(self,obj):
x=obj[0][0].x
y=obj[0][0].y
moveto = vec(x,y) - vec(self.x,self.y)
if not (moveto).length()<=self.attack_range:
def chase(self, obj):
x = obj[0][0].x
y = obj[0][0].y
moveto = vec(x, y) - vec(self.x, self.y)
if not (moveto).length() <= self.attack_range:
moveto.scale_to_length(self.speed)
self.x += moveto[0]/fps
self.y += moveto[1]/fps
self.x += moveto[0] / fps
self.y += moveto[1] / fps
else:
self.attack(moveto,obj)
self.attack(moveto, obj)
def attack(self,moveto,obj):
if self.lastAttack+self.attack_speed*1000<pg.time.get_ticks():
obj[1].append(Arrow("arrow",200,self.x,self.y,moveto, self.damage))
self.lastAttack=pg.time.get_ticks()
def attack(self, moveto, obj):
if self.lastAttack + self.attack_speed * 1000 < pg.time.get_ticks():
obj[1].append(Arrow("arrow", 200, self.x, self.y, moveto, self.damage))
self.lastAttack = pg.time.get_ticks()
def update(self,obj):
def update(self, obj):
self.chase(obj)
@ -231,15 +235,15 @@ class Arrow(Weapons):
def move(self):
self.moveto.scale_to_length(self.speed)
self.x += self.moveto[0]/fps
self.y += self.moveto[1]/fps
self.x += self.moveto[0] / fps
self.y += self.moveto[1] / fps
def die(self,objects):
touches=pg.sprite.spritecollideany(self,objects[0])
if touches is not None and isinstance(touches, MainCharacter):
def die(self, objects):
touches = pg.sprite.spritecollideany(self, objects[0])
if touches is not None and isinstance(touches, MainCharacter):
touches.hurt(self.damage)
self.hidden=True
self.hidden = True
def update(self,objects):
def update(self, objects):
self.move()
self.die(objects)