Setting up a health system. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: Setting up a health system. (/thread-16897.html) |
Setting up a health system. - ghost0fkarma - Mar-19-2019 import pygame, sys, random from pygame.locals import * from pygame_functions import * class CrocSprite(pygame.sprite.Sprite): def __init__(self, group): pygame.sprite.Sprite.__init__(self, group) self.speed = 5 self.tick = 0 self.interval = 60 self.image = pygame.image.load("croc_nigga.jpg").convert_alpha() self.rect = self.image.get_rect() self.rect.x = random.randint(200,500) self.rect.y = -100 def update(self): if self.rect.y > 480: self.rect.x = random.randint(200, 500) self.rect.y = -100 class PirateSprite(pygame.sprite.Sprite): def __init__(self, group): pygame.sprite.Sprite.__init__(self, group) self.speed = 5 self.tick = 0 self.interval = 60 self.image = pygame.image.load("pirate_boat.png").convert_alpha() self.rect = self.image.get_rect() self.rect.x = random.randint(200, 500) self.rect.y = -100 def update(self): if self.rect.y > 480: self.rect.x = random.randint(200, 500) self.rect.y = -100 class BoatSprite(pygame.sprite.Sprite): def __init__(self, group): pygame.sprite.Sprite.__init__(self, group) self.tick = 0 self.interval = 30 self.image = pygame.image.load("boatIMG.png").convert() self.rect = self.image.get_rect() self.rect.x = random.randint(200, 500) self.rect.y = -100 def update(self): if self.rect.y > 480: self.rect.x = random.randint(200, 500) self.rect.y = -100 class BulletSprite(pygame.sprite.Sprite): def __init__(self, group): # Init parent class pygame.sprite.Sprite.__init__(self, group) self.speed = 5 self.tick = 0 self.interval = 60 # Default sprite variables. # self.image = pygame.image.load("bullet)enemy.png").convert() self.image = pygame.image.load("bullet_enemy.png").convert_alpha() self.rect = self.image.get_rect() self.rect.x = random.randint(200, 500) self.rect.y = -100 def update(self): if self.rect.y > 480: self.rect.x = random.randint(200, 500) self.rect.y = -100 class Scene: def __init__(self, game): self.game = game self.boat_group = pygame.sprite.Group() self.bullet_group = pygame.sprite.Group() self.pirate_group = pygame.sprite.Group() self.croc_group = pygame.sprite.Group() self.boat = BoatSprite(self.boat_group) self.boat.rect.topleft = 400, 400 self.bullet = BulletSprite(self.bullet_group) self.pirate = PirateSprite(self.pirate_group) self.croc = CrocSprite(self.croc_group) # self.background_iamge = pygame.image.load("water_background.png").convert() self.background_image = pygame.image.load("water_background.png").convert() def draw(self, surface): surface.blit(self.background_image, (0, 0)) self.boat_group.update() self.bullet_group.update() self.pirate_group.update() self.croc_group.update() self.boat_group.draw(surface) self.bullet_group.draw(surface) self.pirate_group.draw(surface) self.croc_group.draw(surface) def event(self, event): pass def update(self, ticks): if ticks > self.boat.tick: self.boat.tick += self.boat.interval # Tracking key held down keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.boat.rect.x -= 5 elif keys[pygame.K_RIGHT]: self.boat.rect.x += 5 elif keys[pygame.K_UP]: self.boat.rect.y -= 5 elif keys[pygame.K_DOWN]: self.boat.rect.y += 5 # Keep boat within self.boat.rect.clamp_ip(pygame.Rect(120, 340, 400, 150)) if ticks > self.bullet.tick: self.bullet.tick + self.bullet.interval self.bullet.rect.y += self.bullet.speed if ticks > self.pirate.tick: self.pirate.tick + self.pirate.interval self.pirate.rect.y += self.pirate.speed if ticks > self.croc.tick: self.croc.tick + self.croc.interval self.croc.rect.y += self.croc.speed # Colliding hit = pygame.sprite.spritecollide(self.boat, self.bullet_group, False) if len(hit) > 0: pass hit2 = pygame.sprite.spritecollide(self.boat, self.pirate_group, False) if len(hit2) > 0: pass hit3 = pygame.sprite.spritecollide(self.boat, self.croc_group, False) if len(hit3) > 0: pass class Game: def __init__(self, caption, width, height): # Basic pygame setup pygame.display.set_caption(caption) self.rect = pygame.Rect(0, 0, width, height) self.surface = pygame.display.set_mode(self.rect.size) self.clock = pygame.time.Clock() self.heart = pygame.image.load("heart.png").convert_alpha() # Scene self.scene = Scene(self) def mainloop(self): self.running = True while self.running: for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False else: self.scene.event(event) self.scene.update(pygame.time.get_ticks()) # Draw code here self.scene.draw(self.surface) self.surface.blit(self.heart, (600, 20)) self.surface.blit(self.heart, (600, 50)) self.surface.blit(self.heart, (600, 80)) pygame.display.flip() self.clock.tick(30) if __name__ == '__main__': pygame.init() game = Game('Boat Game', 640, 480) game.mainloop() pygame.quit()My question is, how do I make the boat have 3 health. And when collided with an enemy sprite, a health is lost. RE: Setting up a health system. - Windspar - Mar-19-2019 BoatSprite class add health variable. self.health = 3For your groups you can have on for boat enemies. self.bullet_group = pygame.sprite.Group() self.pirate_group = pygame.sprite.Group() self.croc_group = pygame.sprite.Group()Can be replace by. self.boat_enemies_group = pygame.sprite.Group()Then boat losing a life. Check all objects in the list. Returns all objects that boat collided with. # Colliding hit = pygame.sprite.spritecollide(self.boat, self.boat_enemies_group, False) if len(hit) > 0: self.boat.health -= 1 if self.boat.health == 0: # boat diesAll your draw code for the scene should be under draw method. self.surface.blit(self.heart, (600, 20)) self.surface.blit(self.heart, (600, 50)) self.surface.blit(self.heart, (600, 80)) RE: Setting up a health system. [Solved] - ghost0fkarma - Mar-19-2019 Thank you good sir |