def getMedal(self, medal, pos): if medal == 'bronze': return Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), pos) elif medal == 'silver': return Image(Asset.loadpath('homescreen', 'img', 'silver.png'), pos) elif medal == 'gold': return Image(Asset.loadpath('homescreen', 'img', 'gold.png'), pos) return None
def __init__(self, screen): # Shooter image self.personImg = pygame.image.load(Asset.loadpath('disk_shooting', 'img', 'shooter.png')) # Different rotations of the same image self.left_personImg = pygame.transform.rotate(self.personImg, 45) self.middle_personImg = pygame.transform.rotate(self.personImg, 0) self.right_personImg = pygame.transform.rotate(self.personImg, 315) # Set base settings self.x, self.y = (screen.WindowWidth * 0.40), (screen.WindowHeight * 0.8) self.score = 0 self.bullets = 15 # Gun shot sound self.shot = pygame.mixer.Sound(Asset.loadpath('disk_shooting', 'sound', 'pistol_shot.wav'))
def setButton(self, msg, x, y, w, h, ic, ac, screen, action=None): # Button click action mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() # mouse[0] = x coordinates of mouse, # mouse[1] = y coordinates of mouse, # x = position button | horizontal # y = position button | vertical # w = width button # h = height button # ic = background-color # ac = background-color on hover if x + w > mouse[0] > x and y + h > mouse[1] > y: # You are hovering over the button pygame.draw.rect(screen.gameDisplay, ac, (x, y, w, h)) if click[0] == 1 and action != None: return action else: pygame.draw.rect(screen.gameDisplay, ic, (x, y, w, h)) text_Object = textObject() # Button text smallText = pygame.font.Font(Asset.loadpath('font', 'roboto', 'Roboto-Medium.ttf'), 20) self.textSurf, self.textRect = text_Object.textObjects(msg, smallText, Color.WHITE.getRGB(), (y + (h / 2)), (x + (w / 2))) self.textRect.center = ((x + (w / 2)), (y + (h / 2))) screen.gameDisplay.blit(self.textSurf, self.textRect)
def __init__(self, scale, spawnRange): self.img = transform.scale( image.load( Asset.loadpath('hordelopen', 'img', 'obstacle_48x128.png')), (7 * scale, 15 * scale)) self.spawnRange = spawnRange self.hurdleList = [] self.scale = scale
def init(): load_image(Asset.loadpath('zeilen', 'img', 'player.png'), "spr_player", None) load_image(Asset.loadpath('zeilen', 'img', 'obstacle_0.png'), "spr_obstacle_0", None) load_image(Asset.loadpath('zeilen', 'img', 'obstacle_1.png'), "spr_obstacle_1", None) load_image(Asset.loadpath('zeilen', 'img', 'water.gif'), "spr_water", None) load_image(Asset.loadpath('zeilen', 'img', 'finish.png'), "spr_finish", None) load_sound(Asset.loadpath('zeilen', 'sound', 'death.wav'), "snd_death") load_sound(Asset.loadpath('zeilen', 'sound', 'finish.wav'), "snd_finish")
def game_intro(screen): # Main screen text logo = Image(Asset.loadpath('hordelopen', 'img', 'logo.jpg'), [0, 0]) background = Image( Asset.loadpath('hordelopen', 'img', 'background.jpg'), [0, 0]) intructions = Image( Asset.loadpath('hordelopen', 'img', 'intructions.jpg'), [215, 185]) # Buttons start_btn = Button() quit_btn = Button() clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return 'quit' screen.gameDisplay.blit(background.image, background.rect) screen.gameDisplay.blit(intructions.image, intructions.rect) screen.gameDisplay.blit(logo.image, logo.rect) check_start = start_btn.setButton("Start", 150, 450, 100, 50, Color.GREEN.getRGB(), Color.DARK_GREEN.getRGB(), screen, 'game') check_end = quit_btn.setButton("Return", 550, 450, 100, 50, Color.RED.getRGB(), Color.DARK_RED.getRGB(), screen, 'return') # Return results of button clicked if check_start != None: return 'game' elif check_end != None: return 'return' else: pygame.display.update() clock.tick(15)
def __init__(self, scale, imageChangeSpeed, terminalVelocity): self.run1 = transform.scale( image.load(Asset.loadpath('hordelopen', 'img', 'runner1.png')), (7 * scale, 14 * scale)) self.run2 = transform.scale( image.load(Asset.loadpath('hordelopen', 'img', 'runner2.png')), (7 * scale, 14 * scale)) self.run3 = transform.scale( image.load(Asset.loadpath('hordelopen', 'img', 'runner3.png')), (7 * scale, 14 * scale)) self.run4 = transform.scale( image.load(Asset.loadpath('hordelopen', 'img', 'runner4.png')), (7 * scale, 14 * scale)) self.run5 = transform.scale( image.load(Asset.loadpath('hordelopen', 'img', 'runner5.png')), (7 * scale, 14 * scale)) self.scale = scale self.imageChangeSpeed = imageChangeSpeed self.terminalVelocity = terminalVelocity self.height = 14 * scale self.width = 7 * scale
def __init__(self, person, screen): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load(Asset.loadpath('disk_shooting', 'img', 'disk_pix.png')) self.rect = self.image.get_rect() self.rect.center = (self.rnd()) self.SPRITE_GROUP = pygame.sprite.Group() self.SPRITE_GROUP.add(self) # Amount of disks available = the total of bullets the person starts with self.amount = person.bullets # Direction self.dir_x = 1 self.dir_y = 1 # Speed self.speed = randint(1, 2) # Movement of the disk self.run = True # Used fo threading self.game_end = False self.screen = screen
from classes.Color import Color import classes.Asset as Asset import games.diskShooting.Start import games.hordelopen.Start import games.zeilen.Zeilen import games.gewichtheffen.main import games.zwemmen.game_loop import games.skiing.skien_game import classes.EndScreen as EndScreen from classes.Image import Image pygame.init() games_sprites = pygame.sprite.Group() pygame.mixer.music.load(Asset.loadpath('homescreen', 'sound', 'background.mp3')) pygame.mixer.music.play(-1) # Start Pygame clock clock = pygame.time.Clock() # Set window in a variable window = Screen(800, 600) diskShooting = games.diskShooting.Start.Start(window) game_running = True state = 'start' gamename = '' score = 1
def Start(self, screen): # define colors black = (0, 0, 0) white = (255, 255, 255) blue = (28, 163, 236) green = (0, 200, 0) red = (200, 0, 0) yellow = (255, 255, 0) # display size display_width = 800 display_height = 600 gameDisplay = screen.gameDisplay # images crowdImg = pygame.image.load( Asset.loadpath('zwemmen', 'img', 'crowd.png')) swimmerImg = pygame.image.load( Asset.loadpath('zwemmen', 'img', 'swimmer.png')) finishImg = pygame.image.load( Asset.loadpath('zwemmen', 'img', 'finish.png')) waterImg = pygame.image.load( Asset.loadpath('zwemmen', 'img', 'water.jpg')) tilesImg = pygame.image.load( Asset.loadpath('zwemmen', 'img', 'tiles.jpg')) # transform image sizes to match display crowdImg = pygame.transform.scale(crowdImg, (800, 150)) waterImg = pygame.transform.scale(waterImg, (700, 350)) tilesImg = pygame.transform.scale(tilesImg, (800, 600)) # decorate game window pygame.display.set_caption('Olympische Spelen - Swimming') # text - font largeText = pygame.font.Font( Asset.loadpath('font', 'roboto', 'Roboto-Medium.ttf'), 35) smallText = pygame.font.Font( Asset.loadpath('font', 'roboto', 'Roboto-Medium.ttf'), 20) # players energy bar def energy_bar(player_energy): # energy bar color states if player_energy > 75: player_energy_color = green elif player_energy > 50: player_energy_color = yellow else: player_energy_color = red pygame.draw.rect(gameDisplay, player_energy_color, (680, 25, player_energy, 25)) # trigger to finish the game def win_condition(finish, player): if finish.colliderect(player): print('Finished!') return True # create random array of keys that must be pressed to move player def randomKey(): random = randint(0, 25) if random == 0: return 'q' elif random == 1: return 'w' elif random == 2: return 'e' elif random == 3: return 'r' elif random == 4: return 't' elif random == 5: return 'y' elif random == 6: return 'u' elif random == 7: return 'i' elif random == 8: return 'o' elif random == 9: return 'p' elif random == 10: return 'a' elif random == 11: return 's' elif random == 12: return 'd' elif random == 13: return 'f' elif random == 14: return 'g' elif random == 15: return 'h' elif random == 16: return 'j' elif random == 17: return 'k' elif random == 18: return 'l' elif random == 19: return 'z' elif random == 20: return 'x' elif random == 21: return 'c' elif random == 22: return 'v' elif random == 23: return 'b' elif random == 24: return 'n' elif random == 25: return 'm' # main game loop def game_loop(screen): gameExit = False # begin coordinates of player character lead_x = 20 lead_y = 300 # set level of player's energy player_energy = 101 counter, text = 0, '0'.rjust(3) pygame.time.set_timer(pygame.USEREVENT + 1, 1000) font = pygame.font.SysFont( Asset.loadpath('font', 'roboto', 'Roboto-Medium.tff'), 30) # define score count = 0 # define movement moved = randomKey() key_pressed = None print(moved) while not gameExit: for event in pygame.event.get(): if event.type == pygame.QUIT: gameExit = True if event.type == pygame.KEYDOWN: key_pressed = pygame.key.name(event.key) if key_pressed == moved and player_energy > 15: lead_x += 20 player_energy -= 15 moved = randomKey() # energy regeneration if player_energy < 100: player_energy += 0.4 else: player_energy = 101 pygame.display.update() count += 1 # SCORE # tiles # gameDisplay.fill((169,169,169)) gameDisplay.blit(tilesImg, (0, 0)) # crowd background gameDisplay.blit(crowdImg, (0, 0)) # pool background # pygame.draw.rect(gameDisplay, blue, (50,200,700,350)) gameDisplay.blit(waterImg, (50, 200)) # timer background pygame.draw.rect(gameDisplay, white, (24, 40, 135, 40)) # energy background pygame.draw.rect(gameDisplay, white, (675, 20, 111, 35)) # key_pressed background pygame.draw.rect(gameDisplay, white, (375, 50, 50, 50)) # energy bar energy_bar(player_energy) # finish image finish_rect = gameDisplay.blit(finishImg, (750, 150)) # key must press gameDisplay.blit(font.render(moved, True, (0, 0, 0)), (390, 60)) # swimmer / player player_rect = gameDisplay.blit(swimmerImg, [lead_x, lead_y, 10, 20]) # calling finish trigger function if (win_condition(finish_rect, player_rect)): return ['end', count] # return score (count) # < 2400 SCORE = GOLD MEDAL # < 2750 SCORE = SILVER MEDAL # < 3250 SCORE = BRONZE MEDAL # > 3250 SCORE = NO MEDAL # blitting score on screen textSurface = font.render(str(count), False, (0, 0, 0)) gameDisplay.blit(textSurface, (32, 48)) pygame.display.update() pygame.quit() quit() def game_intro(screen): # Main screen text logo = Image(Asset.loadpath('zwemmen', 'img', 'logo.jpg'), [0, 0]) background = Image( Asset.loadpath('zwemmen', 'img', 'background.jpg'), [0, 0]) intructions = Image( Asset.loadpath('zwemmen', 'img', 'instructions.jpg'), [150, 200]) # Buttons start_btn = Button() quit_btn = Button() clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return 'quit' screen.gameDisplay.blit(background.image, background.rect) screen.gameDisplay.blit(intructions.image, intructions.rect) screen.gameDisplay.blit(logo.image, logo.rect) check_start = start_btn.setButton("Start", 150, 450, 100, 50, Color.GREEN.getRGB(), Color.DARK_GREEN.getRGB(), screen, 'game') check_end = quit_btn.setButton("Return", 550, 450, 100, 50, Color.RED.getRGB(), Color.DARK_RED.getRGB(), screen, 'return') # Return results of button clicked if check_start != None: return 'game' elif check_end != None: return 'return' else: pygame.display.update() clock.tick(15) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.display.quit() pygame.quit() quit() break if self.state == 'intro': result = game_intro(screen) if result == None: break else: self.state = result elif self.state == 'game': result = game_loop(screen) print(result) if result == None: break else: self.state = result[0] elif self.state == 'end': self.state = 'intro' return ['score', 'zwemmen', result[1]] elif self.state == 'quit': pygame.display.quit() pygame.quit() elif self.state == 'return': return 'start' pygame.display.update() self.clock.tick(60) pygame.quit() quit()
def game_loop(screen): gameExit = False # begin coordinates of player character lead_x = 20 lead_y = 300 # set level of player's energy player_energy = 101 counter, text = 0, '0'.rjust(3) pygame.time.set_timer(pygame.USEREVENT + 1, 1000) font = pygame.font.SysFont( Asset.loadpath('font', 'roboto', 'Roboto-Medium.tff'), 30) # define score count = 0 # define movement moved = randomKey() key_pressed = None print(moved) while not gameExit: for event in pygame.event.get(): if event.type == pygame.QUIT: gameExit = True if event.type == pygame.KEYDOWN: key_pressed = pygame.key.name(event.key) if key_pressed == moved and player_energy > 15: lead_x += 20 player_energy -= 15 moved = randomKey() # energy regeneration if player_energy < 100: player_energy += 0.4 else: player_energy = 101 pygame.display.update() count += 1 # SCORE # tiles # gameDisplay.fill((169,169,169)) gameDisplay.blit(tilesImg, (0, 0)) # crowd background gameDisplay.blit(crowdImg, (0, 0)) # pool background # pygame.draw.rect(gameDisplay, blue, (50,200,700,350)) gameDisplay.blit(waterImg, (50, 200)) # timer background pygame.draw.rect(gameDisplay, white, (24, 40, 135, 40)) # energy background pygame.draw.rect(gameDisplay, white, (675, 20, 111, 35)) # key_pressed background pygame.draw.rect(gameDisplay, white, (375, 50, 50, 50)) # energy bar energy_bar(player_energy) # finish image finish_rect = gameDisplay.blit(finishImg, (750, 150)) # key must press gameDisplay.blit(font.render(moved, True, (0, 0, 0)), (390, 60)) # swimmer / player player_rect = gameDisplay.blit(swimmerImg, [lead_x, lead_y, 10, 20]) # calling finish trigger function if (win_condition(finish_rect, player_rect)): return ['end', count] # return score (count) # < 2400 SCORE = GOLD MEDAL # < 2750 SCORE = SILVER MEDAL # < 3250 SCORE = BRONZE MEDAL # > 3250 SCORE = NO MEDAL # blitting score on screen textSurface = font.render(str(count), False, (0, 0, 0)) gameDisplay.blit(textSurface, (32, 48)) pygame.display.update() pygame.quit() quit()
def endscreen(screen, game, score): textScore = '' def text_objects(text, font): textSurface = font.render(text, True, Color.WHITE.getRGB()) return textSurface, textSurface.get_rect() def button(msg, x, y, w, h, ic, ac, action=None): # Button click action mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() # mouse[0] = x coordinates of mouse, # mouse[1] = y coordinates of mouse, # x = position button | horizontal # y = position button | vertical # w = width button # h = height button # ic = background-color # ac = background-color on hover if x + w > mouse[0] > x and y + h > mouse[1] > y: # You are hovering over the button pygame.draw.rect(screen.gameDisplay, ac, (x, y, w, h)) if click[0] == 1 and action != None: return action else: pygame.draw.rect(screen.gameDisplay, ic, (x, y, w, h)) # Button text smallText = pygame.font.Font(Asset.loadpath('font', 'roboto', 'Roboto-Medium.ttf'), 20) textSurf, textRect = text_objects(msg, smallText) textRect.center = ((x + (w / 2)), (y + (h / 2))) screen.gameDisplay.blit(textSurf, textRect) def get_medal(game): textScore = '' if game == 'diskShooting': if score >= 8 and score <= 10: medal = Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), [350, 250]) textScore = 'bronze' elif score > 10 and score < 15: medal = Image(Asset.loadpath('homescreen', 'img', 'silver.png'), [350, 250]) textScore = 'silver' elif score == 15: medal = Image(Asset.loadpath('homescreen', 'img', 'gold.png'), [350, 250]) textScore = 'gold' else: return "better luck next time" if game == 'hordelopen': if score >= 1000 and score <= 1200: medal = Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), [350, 250]) textScore = 'bronze' elif score > 1200 and score < 1400: medal = Image(Asset.loadpath('homescreen', 'img', 'silver.png'), [350, 250]) textScore = 'silver' elif score >= 1400: medal = Image(Asset.loadpath('homescreen', 'img', 'gold.png'), [350, 250]) textScore = 'gold' else: return "better luck next time" if game == 'zeilen': if score == 3 or score == 2: medal = Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), [350, 250]) textScore = 'bronze' elif score == 1: medal = Image(Asset.loadpath('homescreen', 'img', 'silver.png'), [350, 250]) textScore = 'silver' elif score == 0: medal = Image(Asset.loadpath('homescreen', 'img', 'gold.png'), [350, 250]) textScore = 'gold' else: return "better luck next time" if game == 'skiing': if score >= 4 and score <= 7: medal = Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), [350, 250]) textScore = 'bronze' elif score > 7 and score < 14: medal = Image(Asset.loadpath('homescreen', 'img', 'silver.png'), [350, 250]) textScore = 'silver' elif score >= 14: medal = Image(Asset.loadpath('homescreen', 'img', 'gold.png'), [350, 250]) textScore = 'gold' else: return "better luck next time" if game == 'zwemmen': if score < 2000 and score >= 1500: medal = Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), [350, 250]) textScore = 'bronze' elif score < 1500 and score >= 1200: medal = Image(Asset.loadpath('homescreen', 'img', 'silver.png'), [350, 250]) textScore = 'silver' elif score < 1200: medal = Image(Asset.loadpath('homescreen', 'img', 'gold.png'), [350, 250]) textScore = 'gold' else: return "better luck next time" if game == 'gewichtheffen': if score == 1: medal = Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), [350, 250]) textScore = 'bronze' elif score == 2: medal = Image(Asset.loadpath('homescreen', 'img', 'silver.png'), [350, 250]) textScore = 'silver' elif score == 3: medal = Image(Asset.loadpath('homescreen', 'img', 'gold.png'), [350, 250]) textScore = 'gold' else: return "better luck next time" return [medal, textScore] medal_array = get_medal(game) if isinstance(medal_array, str): medal = medal_array else: medal = medal_array[0] textScore = medal_array[1] if game == 'diskShooting': background = Image(Asset.loadpath('disk_shooting', 'img', 'background_intro.jpg'), [0, 0]) if game == 'hordelopen': background = Image(Asset.loadpath('hordelopen', 'img', 'background.jpg'), [0, 0]) if game == 'zeilen': background = Image(Asset.loadpath('zeilen', 'img', 'background.jpg'), [0, 0]) if game == 'skiing': background = Image(Asset.loadpath('skiing', 'img', 'background.jpg'), [0, 0]) if game == 'zwemmen': background = Image(Asset.loadpath('zwemmen', 'img', 'background.jpg'), [0, 0]) if game == 'gewichtheffen': background = Image(Asset.loadpath('gewichtheffen', 'img', 'homescherm.jpg'), [0, 0]) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return None screen.gameDisplay.blit(background.image, background.rect) largeText = pygame.font.Font(Asset.loadpath('font', 'roboto', 'Roboto-Medium.ttf'), 50) TextSurf, TextRect = text_objects("Your score: " + str(score), largeText) TextRect.center = ((800 / 2), (600 / 4)) screen.gameDisplay.blit(TextSurf, TextRect) largeText = pygame.font.Font(Asset.loadpath('font', 'roboto', 'Roboto-Medium.ttf'), 50) if isinstance(medal, str): TextSurf, TextRect = text_objects("Your Medal: " + medal, largeText) else: TextSurf, TextRect = text_objects("Your Medal: ", largeText) screen.gameDisplay.blit(medal.image, medal.rect) TextRect.center = ((800 / 2), (600 / 3)) screen.gameDisplay.blit(TextSurf, TextRect) retry_btn = button("Retry", 150, 450, 100, 50, Color.GREEN.getRGB(), Color.DARK_GREEN.getRGB(), game) quit_btn = button("Go back", 550, 450, 100, 50, Color.RED.getRGB(), Color.DARK_RED.getRGB(), 'start') # Return results of button clicked if retry_btn != None: return [retry_btn, game, textScore] elif quit_btn != None: return [quit_btn, game, textScore] else: pygame.display.update() clock.tick(15)
def Start(self, screen): display_width = 800 display_height = 600 black = (0, 0, 0) white = (255, 255, 255) red = (200, 0, 0) green = (0, 200, 0) bright_red = (255, 0, 0) bright_green = (0, 255, 0) skier_width = 73 skierPng = pygame.image.load( Asset.loadpath('skiing', 'img', 'skier3.png')) BackgroundIntro = pygame.image.load( Asset.loadpath('skiing', 'img', 'background_intro.png')) gamebackground = pygame.image.load( Asset.loadpath('skiing', 'img', 'gamebackground.png')) BoomPng = pygame.image.load(Asset.loadpath('skiing', 'img', 'boom.png')) # crash = True # Bomen ontwijken en score optellen def poortje_dodged(screen, count): font = pygame.font.SysFont(None, 25) text = font.render("Dodged: " + str(count), True, black) screen, screen.gameDisplay.blit(text, (100, 0)) # Boom image def poortje(screen, poortjex, poortjey, poortjew, poortjeh, color): screen.gameDisplay.blit(BoomPng, (poortjex, poortjey, poortjew, poortjeh)) # skier image def skier(screen, x, y): screen.gameDisplay.blit(skierPng, (x, y)) # background image def background(screen, x, y): screen.gameDisplay.blit(gamebackground, (0, 0)) def text_objects(text, font): textSurface = font.render(text, True, black) return textSurface, textSurface.get_rect() def game_loop(screen): x = (display_width * 0.45) y = (display_height * 0) x_change = 0 poortje_startx = random.randrange(100, display_width - 100) poortje_starty = 600 poortje_speed = 6 poortje_width = 110 poortje_height = 98 dodged = 0 gameExit = False clock = pygame.time.Clock() while not gameExit: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x_change = -5 elif event.key == pygame.K_RIGHT: x_change = 5 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: x_change = 0 x += x_change screen.gameDisplay.fill(white) # poortje(poortjex, poortjey, poortjew, poortjeh, color) background(screen, x, y) poortje(screen, poortje_startx, poortje_starty, poortje_width, poortje_height, black) poortje_starty -= poortje_speed skier(screen, x, y) poortje_dodged(screen, dodged) if x > display_width - skier_width or x < 0: return ['end', dodged] if poortje_starty < -300: poortje_starty = 600 + poortje_height poortje_startx = random.randrange(100, display_width - 200) dodged += 1 poortje_speed += 0.75 # wanneer je tegen poortje aan crasht if y < poortje_starty + poortje_height and y > poortje_starty: if x > poortje_startx and x < poortje_startx + poortje_width or x + skier_width > poortje_startx and x + skier_width < poortje_startx + poortje_width: return ['end', dodged] pygame.display.update() clock.tick(50) def game_intro(screen): # Main screen text logo = Image(Asset.loadpath('skiing', 'img', 'logo.jpg'), [0, 0]) background = Image( Asset.loadpath('skiing', 'img', 'background.jpg'), [0, 0]) intructions = Image( Asset.loadpath('skiing', 'img', 'background_intro.png'), [150, 50]) # Buttons start_btn = Button() quit_btn = Button() clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return 'quit' screen.gameDisplay.blit(background.image, background.rect) screen.gameDisplay.blit(intructions.image, intructions.rect) screen.gameDisplay.blit(logo.image, logo.rect) check_start = start_btn.setButton("Start", 150, 450, 100, 50, Color.GREEN.getRGB(), Color.DARK_GREEN.getRGB(), screen, 'game') check_end = quit_btn.setButton("Return", 550, 450, 100, 50, Color.RED.getRGB(), Color.DARK_RED.getRGB(), screen, 'return') # Return results of button clicked if check_start != None: return 'game' elif check_end != None: return 'return' else: pygame.display.update() clock.tick(15) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.display.quit() pygame.quit() quit() break if self.state == 'intro': result = game_intro(screen) if result == None: break else: self.state = result elif self.state == 'game': result = game_loop(screen) print(result) if result == None: break else: self.state = result[0] elif self.state == 'end': self.state = 'intro' return ['score', 'skiing', result[1]] elif self.state == 'quit': pygame.display.quit() pygame.quit() elif self.state == 'return': return 'start' pygame.display.update() self.clock.tick(60) pygame.quit() quit()
def onScreen(self): if self.x + self.width > 0: return True else: return False def mainEventLoop(): for events in event.get(): if events.type == KEYDOWN: if events.key == K_ESCAPE: quit() groundImg = transform.scale( image.load(Asset.loadpath('hordelopen', 'img', '491830606.jpg')), (windowX, int(windowY))) font1 = font.Font(Asset.loadpath('font', 'roboto', 'Roboto-Medium.ttf'), 20) font2 = font.Font(Asset.loadpath('font', 'roboto', 'Roboto-Medium.ttf'), 20) deathMessage1 = font1.render('NO MEDAL!', True, (0, 0, 0)) deathMessage2 = font2.render('Press Space to continue', True, (0, 0, 0)) deathMessage3 = font2.render('Press Escape to quit', True, (0, 0, 0)) bronzeMsg = font1.render('Bronze Medal!', True, (205, 127, 50)) silverMsg = font1.render('Silver Medal!', True, (192, 192, 192)) goldMsg = font1.render('Gold Medal!', True, (255, 215, 0)) message1Rect = deathMessage1.get_rect() message1x = windowX / 2 - message1Rect.width / 2
def startingScreen(self, screen): pygame.display.set_caption("Olympische Spelen") imgBackground = Image( Asset.loadpath('homescreen', 'img', 'background.png'), [0, 0]) scoreBackground = Image( Asset.loadpath('homescreen', 'img', 'scoreboard.png'), [0, 235]) # Zeilen btnZeilen = Image(Asset.loadpath('homescreen', 'img', 'btnZeilen.png'), [241, 350]) btnZeilenHover = Image( Asset.loadpath('homescreen', 'img', 'btnZeilenHover.png'), [241, 350]) # Disk shooting btnDiskshooting = Image( Asset.loadpath('homescreen', 'img', 'btnDiskShooting.png'), [102, 253]) btnDiskshootingHover = Image( Asset.loadpath('homescreen', 'img', 'btnDiskShootingHover.png'), [102, 253]) # Hordelopen btnHordelopen = Image( Asset.loadpath('homescreen', 'img', 'btnHordelopen.png'), [622, 160]) btnHordelopenHover = Image( Asset.loadpath('homescreen', 'img', 'btnHordelopenHover.png'), [622, 160]) # Skiing btnSkiing = Image(Asset.loadpath('homescreen', 'img', 'btnBoksen.png'), [320, 202]) btnSkiingHover = Image( Asset.loadpath('homescreen', 'img', 'btnBoksenHover.png'), [320, 202]) btnZwemmen = Image( Asset.loadpath('homescreen', 'img', 'btnZwemmen.png'), [39, 75]) btnZwemmenHover = Image( Asset.loadpath('homescreen', 'img', 'btnZwemmenHover.png'), [39, 75]) btnGewichtsheffen = Image( Asset.loadpath('homescreen', 'img', 'btnGewichtsheffen.png'), [392, 81]) btnGewichtsheffenHover = Image( Asset.loadpath('homescreen', 'img', 'btnGewichtsheffenHover.png'), [392, 81]) while True: self.clicked = False ev = pygame.event.get() for event in ev: if event.type == pygame.MOUSEBUTTONDOWN: global clicked self.clicked = True screen.gameDisplay.fill((Color.BLACK.getRGB())) screen.gameDisplay.blit(imgBackground.image, imgBackground.rect) screen.gameDisplay.blit(scoreBackground.image, scoreBackground.rect) # Working games btnHordelopenRect = screen.gameDisplay.blit( btnHordelopen.image, btnHordelopen.rect) btnDiskshootingRect = screen.gameDisplay.blit( btnDiskshooting.image, btnDiskshooting.rect) btnZeilenRect = screen.gameDisplay.blit(btnZeilen.image, btnZeilen.rect) btnSkiingRect = screen.gameDisplay.blit(btnSkiing.image, btnSkiing.rect) btnZwemmenRect = screen.gameDisplay.blit(btnZwemmen.image, btnZwemmen.rect) # Games that will be tzken out of the game if needed btnGewichtsheffenRect = screen.gameDisplay.blit( btnGewichtsheffen.image, btnGewichtsheffen.rect) self.displayScores(screen) # Check if button is clicked if btnHordelopenRect.collidepoint( pygame.mouse.get_pos()) and self.clicked: return 'hordelopen' elif btnDiskshootingRect.collidepoint( pygame.mouse.get_pos()) and self.clicked: return 'diskShooting' elif btnZeilenRect.collidepoint( pygame.mouse.get_pos()) and self.clicked: return 'zeilen' elif btnSkiingRect.collidepoint( pygame.mouse.get_pos()) and self.clicked: return 'skiing' elif btnZwemmenRect.collidepoint( pygame.mouse.get_pos()) and self.clicked: return 'zwemmen' elif btnGewichtsheffenRect.collidepoint( pygame.mouse.get_pos()) and self.clicked: return 'gewichtheffen' # On hover effect if btnDiskshootingRect.collidepoint(pygame.mouse.get_pos()): btnDiskshootingRect = screen.gameDisplay.blit( btnDiskshootingHover.image, btnDiskshootingHover.rect) elif btnHordelopenRect.collidepoint(pygame.mouse.get_pos()): btnHordelopenRect = screen.gameDisplay.blit( btnHordelopenHover.image, btnHordelopenHover.rect) elif btnZeilenRect.collidepoint(pygame.mouse.get_pos()): btnZeilenRect = screen.gameDisplay.blit( btnZeilenHover.image, btnZeilenHover.rect) elif btnZwemmenRect.collidepoint(pygame.mouse.get_pos()): btnZwemmenRect = screen.gameDisplay.blit( btnZwemmenHover.image, btnZwemmenHover.rect) elif btnSkiingRect.collidepoint(pygame.mouse.get_pos()): btnSkiingRect = screen.gameDisplay.blit( btnSkiingHover.image, btnSkiingHover.rect) elif btnGewichtsheffenRect.collidepoint(pygame.mouse.get_pos()): btnGewichtsheffenRect = screen.gameDisplay.blit( btnGewichtsheffenHover.image, btnGewichtsheffenHover.rect) # If no button has been pressed then stay in start screen return 'start'
import pygame import classes.Asset as Asset background = pygame.image.load( Asset.loadpath('gewichtheffen', 'img', 'background.jpg')) backgroundrect = pygame.transform.scale(background, (800, 600)) homescherm = pygame.image.load( Asset.loadpath('gewichtheffen', 'img', 'homescherm.jpg')) homeschermrect = pygame.transform.scale(homescherm, (800, 600)) personstart = pygame.image.load( Asset.loadpath('gewichtheffen', 'img', 'personstart.png')) personlift = pygame.image.load( Asset.loadpath('gewichtheffen', 'img', 'personlift.png')) personlifted = pygame.image.load( Asset.loadpath('gewichtheffen', 'img', 'personlifted.png')) personstartrect = pygame.transform.scale(personstart, (100, 200)) personliftrect = pygame.transform.scale(personlift, (100, 200)) personliftedrect = pygame.transform.scale(personlifted, (100, 200))
def get_medal(game): textScore = '' if game == 'diskShooting': if score >= 8 and score <= 10: medal = Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), [350, 250]) textScore = 'bronze' elif score > 10 and score < 15: medal = Image(Asset.loadpath('homescreen', 'img', 'silver.png'), [350, 250]) textScore = 'silver' elif score == 15: medal = Image(Asset.loadpath('homescreen', 'img', 'gold.png'), [350, 250]) textScore = 'gold' else: return "better luck next time" if game == 'hordelopen': if score >= 1000 and score <= 1200: medal = Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), [350, 250]) textScore = 'bronze' elif score > 1200 and score < 1400: medal = Image(Asset.loadpath('homescreen', 'img', 'silver.png'), [350, 250]) textScore = 'silver' elif score >= 1400: medal = Image(Asset.loadpath('homescreen', 'img', 'gold.png'), [350, 250]) textScore = 'gold' else: return "better luck next time" if game == 'zeilen': if score == 3 or score == 2: medal = Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), [350, 250]) textScore = 'bronze' elif score == 1: medal = Image(Asset.loadpath('homescreen', 'img', 'silver.png'), [350, 250]) textScore = 'silver' elif score == 0: medal = Image(Asset.loadpath('homescreen', 'img', 'gold.png'), [350, 250]) textScore = 'gold' else: return "better luck next time" if game == 'skiing': if score >= 4 and score <= 7: medal = Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), [350, 250]) textScore = 'bronze' elif score > 7 and score < 14: medal = Image(Asset.loadpath('homescreen', 'img', 'silver.png'), [350, 250]) textScore = 'silver' elif score >= 14: medal = Image(Asset.loadpath('homescreen', 'img', 'gold.png'), [350, 250]) textScore = 'gold' else: return "better luck next time" if game == 'zwemmen': if score < 2000 and score >= 1500: medal = Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), [350, 250]) textScore = 'bronze' elif score < 1500 and score >= 1200: medal = Image(Asset.loadpath('homescreen', 'img', 'silver.png'), [350, 250]) textScore = 'silver' elif score < 1200: medal = Image(Asset.loadpath('homescreen', 'img', 'gold.png'), [350, 250]) textScore = 'gold' else: return "better luck next time" if game == 'gewichtheffen': if score == 1: medal = Image(Asset.loadpath('homescreen', 'img', 'bronze.png'), [350, 250]) textScore = 'bronze' elif score == 2: medal = Image(Asset.loadpath('homescreen', 'img', 'silver.png'), [350, 250]) textScore = 'silver' elif score == 3: medal = Image(Asset.loadpath('homescreen', 'img', 'gold.png'), [350, 250]) textScore = 'gold' else: return "better luck next time" return [medal, textScore]