def __init__(self): """Initialize the game, create game resources.""" pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) pygame.display.set_caption("Alien Invasion") # Create an instance to store game stats and create a scoreboard self.stats = GameStats(self) self.sb = Scoreboard(self) self.ship = Ship(self) self.bullets = pygame.sprite.Group() self.aliens = pygame.sprite.Group() self._create_fleet() # Make the title banner and play button self.play_button = Button(self, "START") self.title_banner = Banner(self, "Alien Invasion") # Make a countdown object for displaying count on game start / new ship self.countdown = Countdown(self) # Make an object for a banner displaying "GAME OVER" when no ships # remain self.gameover = GameOver(self)
def testTheMostAnnoyingSoundInTheWorld(self): #setup sound = Countdown.soundFactory(Countdown, Countdown.S1500, Countdown.S2000) #action result = Countdown.theMostAnnoyingSoundInTheWorld(Countdown, 0) #assert self.assertEqual(sound, result)
def __init_executors(): logger.init() Countdown.make_and_start("backup logs", 25 * 60, __check_for_new_characters) if should_capture_screenshots: Countdown.make_and_start("screenshots", screenshot_interval * 60, __take_screenshot) if should_capture_webcam: Countdown.make_and_start("cam capture", webcam_capture_interval * 60, __make_cam_capture) if should_capture_screenshots or should_capture_webcam: Countdown.make_and_start("mouse idle", 7.3 * 60, __make_mouse_idle)
def initialize(self): self.player = Player(self, batch=self.batch) self.gravitoids.append(self.player) self.sprites.append(self.player) # a countdown clock. Countdown to success. self.timer = Countdown(self, batch=self.batch) self.sprites.append(self.timer) # winning and losing ghosts. # will be a list of GhostOutcome tuples (ghost, win? True/False) self.ghosts_of_christmas_past = []
def __init__(self, modes): GPIO.setmode( GPIO.BCM) #set up to use gpio port numbers (instead of pin #s) self.buttons = Buttons() self.optos = Optos() self.lights = Lights() self.outlets = Outlets() self.sounds = Sounds() self.clock = Countdown(12) self.modes = modes self.last_time = time.localtime()
def levelUp(self): #set the current level self.levelcnt += 1 self.curLevel = Level(self.levelcnt) #create a new level self.player.__init__() #load level self.curLevel.loadLevel() self.levelRows, self.levelCols = self.curLevel.levelRows, self.curLevel.levelCols self.playerStartRow, self.playerStartCol = self.curLevel.playerStartRow, self.curLevel.playerStartCol self.targetRow, self.targetCol = self.curLevel.targetRow, self.curLevel.targetCol self.walls = self.curLevel.walls self.iceNum = self.curLevel.iceNum self.switches = self.curLevel.switches self.levelMin, self.levelSec = self.curLevel.levelMin, self.curLevel.levelSec self.countdown = Countdown(self.levelMin, self.levelSec) self.iceMelted = 0 self.keyObtained = False #level loaded; set player data self.player.setPos(self.playerStartRow, self.playerStartCol) self.player.setBoundaries(self.walls) self.player.setTargetPos(self.targetRow, self.targetCol) if self.curLevel.hasKey: self.player.setDoorLock(self.curLevel.lockPos)
def main(): subprocess.Popen(['pixlet', 'serve', 'build/display.star', '--watch'], shell=False, stdin=None, stdout=None, stderr=None, close_fds=False) print('here') disp = [ Countdown( arrow.get('2022-05-15 10:00:00'), 'AP Physics Exam', MAKO_DIR / 'countdown.star.mako', BUILD_DIR / 'display.star') ] while True: disp[0].render() time.sleep(60)
class Scene: def __init__(self, player, levelcnt): self.levelcnt = levelcnt self.player = player self.gameOver = False self.timeOut = False self.score = 0 self.levelUp() self.dontMeltLastPos = False #True if the last position had a switch self.iceLastPos = False #True if last position had a portal def levelUp(self): #set the current level self.levelcnt += 1 self.curLevel = Level(self.levelcnt) #create a new level self.player.__init__() #load level self.curLevel.loadLevel() self.levelRows, self.levelCols = self.curLevel.levelRows, self.curLevel.levelCols self.playerStartRow, self.playerStartCol = self.curLevel.playerStartRow, self.curLevel.playerStartCol self.targetRow, self.targetCol = self.curLevel.targetRow, self.curLevel.targetCol self.walls = self.curLevel.walls self.iceNum = self.curLevel.iceNum self.switches = self.curLevel.switches self.levelMin, self.levelSec = self.curLevel.levelMin, self.curLevel.levelSec self.countdown = Countdown(self.levelMin, self.levelSec) self.iceMelted = 0 self.keyObtained = False #level loaded; set player data self.player.setPos(self.playerStartRow, self.playerStartCol) self.player.setBoundaries(self.walls) self.player.setTargetPos(self.targetRow, self.targetCol) if self.curLevel.hasKey: self.player.setDoorLock(self.curLevel.lockPos) def meltOldPos(self, oldRow, oldCol): #checks if the last position was a switch #if it was a switch, change boolean #if it was not a switch, melt the old position if self.dontMeltLastPos == False: self.curLevel.meltIce(oldRow, oldCol) self.iceMelted += 1 else: self.dontMeltLastPos = False def updateAlivePlayer(self): for event in pygame.event.get(): if event.type == QUIT: return 1 elif event.type == MOUSEBUTTONUP: x, y = event.pos restartX1, restartY1, restartX2, restartY2 = (4, 515, 105, 538) if restartX1 <= x <= restartX2 and restartY1 <= y <= restartY2: return 2 elif event.type == KEYDOWN: if event.key == K_RIGHT: self.player.move(0, +1) elif event.key == K_LEFT: self.player.move(0, -1) elif event.key == K_UP: self.player.move(-1, 0) elif event.key == K_DOWN: self.player.move(+1, 0) else: return 0 #break for loop if a different key pressed oldRow, oldCol = self.player.getOldPos() curRow, curCol = self.player.getCurPos() if (oldRow != None and oldCol != None) and (oldRow != curRow or oldCol != curCol): #only melt ice and increase score when the player has made a valid move coinRow, coinCol = self.curLevel.coinPos keyRow, keyCol = self.curLevel.keyPos lockRow, lockCol = self.curLevel.lockPos pos = (curRow, curCol) if pos in self.switches: self.score += 10 self.curLevel.turnToIce( curRow, curCol) #once stepped on a switch, it becomes ice self.switches.remove(pos) #switch no longer exists self.player.visited.remove( pos) #player can step on switch tiles twice self.meltOldPos(oldRow, oldCol) self.dontMeltLastPos = True #if "cur" pos was a switch, it was turned into ice; do not melt it break elif pos in self.curLevel.portals: self.score += 10 for portal in self.curLevel.portals: if pos != portal: newRow, newCol = portal self.curLevel.turnToIce(curRow, curCol) self.player.setPos(newRow, newCol) self.player.visited.remove(pos) self.curLevel.portals.clear() self.iceLastPos = True break elif self.iceLastPos: #if the last position was a portal, scene should turn last pos to self.curLevel.turnToIce(oldRow, oldCol) self.iceLastPos = False break elif (coinRow != None and coinCol != None) and (coinRow == curRow and coinCol == curCol): self.score += 50 #bonus points bc hit a coin elif self.curLevel.hasKey: if keyRow == curRow and keyCol == curCol: self.score += 20 self.keyObtained = self.player.keyObtained = True elif lockRow == curRow and lockCol == curCol: if self.keyObtained: self.score += 20 self.curLevel.unlockDoor() else: print("key not obtained; cannot unlock door") self.score += 8 #not a special case tile self.meltOldPos(oldRow, oldCol) def update(self): self.playerState = self.player.getPlayerState() self.playerWins = self.curLevel.playerWins timerUpdate = self.countdown.update() if not self.playerWins and timerUpdate == "game over": self.timeOut = self.gameOver = True elif self.playerWins: self.gameOver = True elif self.playerState == 1: #alive return self.updateAlivePlayer() elif self.playerState == 2: #reached target if self.iceMelted == self.iceNum: self.score += self.iceNum * 2 #bonus point for melting all ice on that level self.levelUp() elif self.playerState == 0: #dead row, col = self.player.getDiedPos() self.curLevel.meltIce(row, col) self.gameOver = True pygame.time.delay(600) return 0 def drawText(self, screen): font = pygame.font.Font("Krungthep.ttf", 20) font.set_bold(True) #score score = font.render("score: %d" % self.score, True, BLUE) screen.blit(score, [445, 515]) #level level = font.render("level: %d" % self.levelcnt, True, BLUE) screen.blit(level, [5, 5]) #ice block count iceMelted = str(self.iceMelted) if len(iceMelted) == 1: iceMelted = " " + iceMelted blocktxt = iceMelted + "/" + str(self.iceNum) blocks = font.render(blocktxt, True, BLUE) screen.blit(blocks, [520, 5]) #key needed self.drawKeyText(screen) def drawKeyText(self, screen): font = pygame.font.Font("Krungthep.ttf", 15) font.set_bold(True) key = font.render("KEY: ", True, BLUE) screen.blit(key, [222, 518]) if self.curLevel.hasKey: #needs key if self.keyObtained: status = "obtained" else: status = "unobtained" else: #does not need key status = "unavailable" keytxt = "KEY: " + status key = font.render(status, True, RED) screen.blit(key, [265, 518]) def display(self, screen): BG, null = load_image("base.gif") screen.blit(BG, (0, 0)) self.drawText(screen) self.countdown.draw(screen) self.curLevel.draw(screen) self.player.draw(screen) pygame.display.flip()
def testGetArt(self): #action result = Countdown.getArt(Countdown, "art3") #assert self.assertEqual(Countdown.Art3, result)
def __init__(self): # initialize the pygame module pygame.init() logo_name = os.path.join('assets', "logo.png") logo = pygame.image.load(logo_name) pygame.display.set_icon(logo) pygame.display.set_caption("Escape") self.screen = pygame.display.set_mode(SCREEN_SIZE) self.black_surface = self.screen.copy() self.black_surface.fill(pygame.Color("black")) # default font used for the timer self.font = pygame.font.Font(None, 100) self.subtext_font = pygame.font.Font(None, 25) self.notification_font = pygame.font.Font(None, 25) # specifies the middle of the screen self.character = Character("avatar/MC-front.png", 0, 0) self.camera = Coords(CHARACTER_START[X], CHARACTER_START[Y]) # saves the camera for when enering the building self.camera_save = None self.user_input = UserInput() # sound self.ambient = pygame.mixer.Sound("assets/ambient.wav") self.ambient_channel = pygame.mixer.Channel(1) self.wind = pygame.mixer.Sound("assets/wind.wav") self.wind.play(-1) # a shit ton of time related stuff self.clock = pygame.time.Clock() self.countdown = Countdown() self.ticker = Ticker() self.fade_in_timer = Timer() self.display_timer = Timer() self.notification_timer = Timer() self.fade_out_timer = Timer() # variables for when you're in some building self.current_building = None self.buildings = create_buildings() self.well_area = ScaledRect(208, 354, 60, 51) # define a variable to control the main loop # where it is really only set to false if the player exits or the X button is pressed self.running = True self.state = None self.fade_into_main_menu() self.wins = create_wins() self.subtext_value = "" self.notification_value = "" # self.ended = False # self.show_ending = False self.end_image = None self.begin_image = load_image("beginlol.png", use_scale=False, return_rect=False) self.begin_image = pygame.transform.scale(self.begin_image, SCREEN_SIZE) self.default_background = load_image('background.png', return_rect=False) self.building_wall_mask = load_image('background_outline.png', convert_alpha=True, return_rect=False) # background can change to default background or house background self.background = self.default_background # creates the start menus self.pause_menu = PauseMenu(self.screen) self.main_menu = MainMenu(self.screen) self.temp = []
class Game: def __init__(self): # initialize the pygame module pygame.init() logo_name = os.path.join('assets', "logo.png") logo = pygame.image.load(logo_name) pygame.display.set_icon(logo) pygame.display.set_caption("Escape") self.screen = pygame.display.set_mode(SCREEN_SIZE) self.black_surface = self.screen.copy() self.black_surface.fill(pygame.Color("black")) # default font used for the timer self.font = pygame.font.Font(None, 100) self.subtext_font = pygame.font.Font(None, 25) self.notification_font = pygame.font.Font(None, 25) # specifies the middle of the screen self.character = Character("avatar/MC-front.png", 0, 0) self.camera = Coords(CHARACTER_START[X], CHARACTER_START[Y]) # saves the camera for when enering the building self.camera_save = None self.user_input = UserInput() # sound self.ambient = pygame.mixer.Sound("assets/ambient.wav") self.ambient_channel = pygame.mixer.Channel(1) self.wind = pygame.mixer.Sound("assets/wind.wav") self.wind.play(-1) # a shit ton of time related stuff self.clock = pygame.time.Clock() self.countdown = Countdown() self.ticker = Ticker() self.fade_in_timer = Timer() self.display_timer = Timer() self.notification_timer = Timer() self.fade_out_timer = Timer() # variables for when you're in some building self.current_building = None self.buildings = create_buildings() self.well_area = ScaledRect(208, 354, 60, 51) # define a variable to control the main loop # where it is really only set to false if the player exits or the X button is pressed self.running = True self.state = None self.fade_into_main_menu() self.wins = create_wins() self.subtext_value = "" self.notification_value = "" # self.ended = False # self.show_ending = False self.end_image = None self.begin_image = load_image("beginlol.png", use_scale=False, return_rect=False) self.begin_image = pygame.transform.scale(self.begin_image, SCREEN_SIZE) self.default_background = load_image('background.png', return_rect=False) self.building_wall_mask = load_image('background_outline.png', convert_alpha=True, return_rect=False) # background can change to default background or house background self.background = self.default_background # creates the start menus self.pause_menu = PauseMenu(self.screen) self.main_menu = MainMenu(self.screen) self.temp = [] @property def in_building(self): return self.current_building is not None def play(self): while self.running: if DEBUG: print(STATE_DICT[self.state]) self.handle_event() self.update() self.draw() self.clock.tick(60) if DEBUG: print("debug: temp =", self.temp) print("collected {}".format(self.character.items)) def handle_event(self): self.user_input.update() self.ticker.update() # can only pause when you're in the f*****g game matey lmao if self.state == GAME_PLAY and self.user_input.clicked_pause( self.ticker.tick): self.pause() elif self.state == GAME_PAUSE: if self.user_input.clicked_mouse(): pos = pygame.mouse.get_pos() if self.pause_menu.resume_button.border.collidepoint(pos): self.unpause() if self.pause_menu.exit_button.border.collidepoint(pos): self.running = False if self.user_input.clicked_pause(self.ticker.tick): self.unpause() # checks for main menu clicks if self.state == MAIN_MENU: if self.user_input.clicked_mouse(): pos = pygame.mouse.get_pos() if self.main_menu.play_button.border.collidepoint(pos): self.fade_outof_main_menu() if self.main_menu.exit_button.border.collidepoint(pos): self.running = False return if self.user_input.clicked_interact(self.ticker.tick): self.fade_outof_main_menu() # only do something if the event is of type QUIT if self.user_input.clicked_quit(): # change the value to False, to exit the main loop self.running = False return # if the player presses interact and it's during one of the display screens if self.user_input.clicked_interact(self.ticker.tick): if BEGIN_FADE_IN <= self.state <= BEGIN_FADE_OUT: self.start_game_fade_in() if END_FADE_IN <= self.state <= END_FADE_OUT: self.fade_into_main_menu() if self.state in STATES_DISPLAY: if self.display_timer.ended: if self.state == BEGIN_DISPLAY: # begin display -> fade out begin display self.state = BEGIN_FADE_OUT self.fade_out_timer.start(BEGIN_FADE_OUT_TIME) elif self.state == END_DISPLAY: # end display -> fade out end display self.state = END_FADE_OUT self.fade_out_timer.start(END_FADE_OUT_TIME) if self.state in STATES_FADE_IN: if self.fade_in_timer.ended: if self.state == MAIN_MENU_FADE_IN: # menu fade in to main menu self.to_main_menu() elif self.state == BEGIN_FADE_IN: # end instruction fade in self.state = BEGIN_DISPLAY self.display_timer.start(BEGIN_DISPLAY_TIME) elif self.state == GAME_FADE_IN: # starts the game here (end fade in) self.countdown.start() self.ambient_channel.play(self.ambient) self.state = GAME_PLAY elif self.state == END_FADE_IN: # end the ending fade in self.state = END_DISPLAY self.display_timer.start(END_DISPLAY_TIME) if self.state in STATES_FADE_OUT: if self.fade_out_timer.ended: if self.state == MAIN_MENU_FADE_OUT: # menu fade out -> begin fade in self.state = BEGIN_FADE_IN self.fade_in_timer.start(BEGIN_FADE_IN_TIME) elif self.state == BEGIN_FADE_OUT: # begin fade out -> game fade in self.start_game_fade_in() elif self.state == GAME_FADE_OUT: # already ended the game, shows display self.state = END_FADE_IN self.fade_in_timer.start(END_FADE_IN_TIME) elif self.state == END_FADE_OUT: self.fade_into_main_menu() # countdown ends: game ends if self.state == GAME_PLAY and self.countdown.ended: self.end() def update(self): if self.state == GAME_PLAY: self.countdown.update() velocity = self.user_input.get_velocity() self.camera.store_previous() self.camera += velocity if DEBUG: coords = self.character.get_pixel_at_feet(self.camera) print(coords) if self.user_input.clicked_debug(self.ticker.tick): self.temp.append(coords) self.character.update(velocity, self.ticker.tick) if not self.in_building: # detects collision with walls and buildings pixel = (self.building_wall_mask.get_at( tuple(self.character.get_pixel_at_feet(self.camera)))) if pixel[3] > ALPHA_THRESHOLD: # TODO make less sticky if possible if COLLIDES: # checks for each x and y if they can work current_x = self.camera.x current_y = self.camera.y self.camera.x = self.camera.previous_x self.camera.y = self.camera.previous_y coords = Coords(current_x, current_y) coords.x = self.camera.previous_x # gets the new y coordinate and keeps x constant pixel = (self.building_wall_mask.get_at( tuple(self.character.get_pixel_at_feet(coords)))) if not (pixel[3] > ALPHA_THRESHOLD ): # if the new y coordinate is free self.camera.y = coords.y coords = Coords(current_x, current_y) coords.y = self.camera.previous_y pixel = (self.building_wall_mask.get_at( tuple(self.character.get_pixel_at_feet(coords)))) if not pixel[3] > ALPHA_THRESHOLD: self.camera.x = coords.x # checks for collecting water at the well if (self.well_area.collide(self.camera, self.character.proper_size) and self.character.items["water_skin"] >= 1): self.subtext_value = "Press {} to fill your water skin".format( INTERACT_KEY) if self.user_input.clicked_interact(self.ticker.tick): self.character.items["water_skin"] -= 1 self.character.items["filled_water_skin"] += 1 self.notification_value = "Filled your water skin" self.notification_timer.start(NOTIFICATION_TIME) # checks for the building shit for building in self.buildings: if building.enters(self.character, self.camera): self.subtext_value = "Press {} to enter ".format( INTERACT_KEY) + building.name if self.user_input.clicked_interact(self.ticker.tick): self.notification_value = "Entered " + building.name self.notification_timer.start(NOTIFICATION_TIME) self.current_building = building self.background = building.background self.camera_save = self.camera.copy() # resets the camera building_exit_coords = building.camera_exit_coords building_exit_coords.y -= self.character.rect.height // 2 self.camera = building_exit_coords # collides with any win for win in self.wins: if win.collide(self.camera, self.character.get_rect_at_feet()): # set player to escaped self.character.escaped = True # ambient fades out only here self.ambient_channel.fadeout(AMBIENT_FADE_OUT) self.end() else: # detects collision with the building walls by checking # if the player leaves the given rect coords = tuple(self.character.get_pixel_at_feet(self.camera)) if not self.current_building.walls.rect.collidepoint(coords): # TODO make less sticky if possible if COLLIDES: # checks for each x and y if they can work current_x = self.camera.x current_y = self.camera.y self.camera.x = self.camera.previous_x self.camera.y = self.camera.previous_y coords2 = Coords(current_x, current_y) coords2.x = self.camera.previous_x # gets the new y coordinate and keeps x constant if self.current_building.walls.rect.collidepoint( tuple(self.character.get_pixel_at_feet( coords2))): self.camera.y = coords2.y coords2 = Coords(current_x, current_y) coords2.y = self.camera.previous_y if self.current_building.walls.rect.collidepoint( tuple(self.character.get_pixel_at_feet( coords2))): self.camera.x = coords2.x # detects collision for furniture for furniture in self.current_building.furniture.values(): if furniture.rect.collidepoint(coords): # TODO make less sticky if possible if COLLIDES: # checks for each x and y if they can work current_x = self.camera.x current_y = self.camera.y self.camera.x = self.camera.previous_x self.camera.y = self.camera.previous_y coords2 = Coords(current_x, current_y) coords2.x = self.camera.previous_x # gets the new y coordinate and keeps x constant if not furniture.rect.collidepoint( tuple( self.character.get_pixel_at_feet( coords2))): self.camera.y = coords2.y coords2 = Coords(current_x, current_y) coords2.y = self.camera.previous_y if not furniture.rect.collidepoint( tuple( self.character.get_pixel_at_feet( coords2))): self.camera.x = coords2.x # checks for collectibles for collectible in self.current_building.collectibles: if collectible.collides(self.character, self.camera): self.subtext_value = "Collect " + collectible.display_name if self.user_input.clicked_interact(self.ticker.tick): collectible.pick_up() self.notification_value = "Collected " + collectible.display_name self.notification_timer.start(NOTIFICATION_TIME) self.character.items[collectible.type] += 1 #self.character.points += collectible.points #self.character.weight += collectible.weight # checks whether they have left the building if self.current_building.exit_area.collide( self.camera, self.character.get_rect_at_feet()): self.subtext_value = "Press {} to leave ".format( INTERACT_KEY) + self.current_building.name if self.user_input.clicked_interact(self.ticker.tick): self.notification_value = "Left " + self.current_building.name self.notification_timer.start(NOTIFICATION_TIME) self.camera = self.camera_save self.current_building = None self.background = self.default_background def draw(self): self.screen.fill((0, 0, 0)) # draws the background relative to the character and countdown if GAME_FADE_IN <= self.state <= GAME_FADE_OUT: self.screen.blit(self.background, (SCREEN_SIZE[X] // 2 - self.camera.x, SCREEN_SIZE[Y] // 2 - self.camera.y)) self.countdown.draw(self.screen, self.font) if not self.in_building: if DEBUG: for building in self.buildings: building.debug_draw_position(self.screen, self.camera) for win in self.wins: win.debug_draw(self.camera, self.screen) self.well_area.debug_draw(self.camera, self.screen, "pink") else: # in building if DEBUG: self.current_building.debug_draw_inner( self.screen, self.camera) # draws all collectibles within a building self.current_building.collectibles.draw( self.screen, self.camera) # draws character at the last lmao self.character.draw(self.screen) if DEBUG: self.character.draw_pixel(self.screen, self.camera) if self.state == GAME_PAUSE: self.pause_menu.draw(self.screen) # renders the display name on the bottom right of the screen if self.subtext_value: text_render = self.subtext_font.render(self.subtext_value, True, pygame.Color("orange")) # text_render = pygame.transform.scale(text_render, (text_render.get_width()*3, text_render.get_height()*3)) text_pos = text_render.get_rect() text_pos.bottomleft = self.screen.get_rect().bottomleft self.screen.blit(text_render, text_pos) self.subtext_value = "" if self.notification_value: if self.notification_timer.ended: self.notification_value = "" else: text_render = self.notification_font.render( self.notification_value, True, pygame.Color("orange")) # text_render = pygame.transform.scale(text_render, (text_render.get_width()*3, text_render.get_height()*3)) text_pos = text_render.get_rect() text_pos.bottomleft = self.screen.get_rect().bottomleft text_pos.y -= 25 self.screen.blit(text_render, text_pos) # displays the beginning image if BEGIN_FADE_IN <= self.state <= BEGIN_FADE_OUT: self.screen.blit(self.begin_image, (0, 0)) # displays the ending image if END_FADE_IN <= self.state <= END_FADE_OUT: self.screen.blit(self.end_image, (0, 0)) # displays the main menu if self.state in (MAIN_MENU_FADE_IN, MAIN_MENU, MAIN_MENU_FADE_OUT): self.main_menu.draw(self.screen) if self.state in STATES_FADE_IN or self.state in STATES_FADE_OUT: if self.state in STATES_FADE_IN: # goes from black to normal alpha_value = int(255 * (self.fade_in_timer.get())) else: # goes from normal to black alpha_value = int(255 - 255 * (self.fade_out_timer.get())) self.black_surface.set_alpha(alpha_value) self.screen.blit(self.black_surface, (0, 0)) pygame.display.flip() def start_game_fade_in(self): self.state = GAME_FADE_IN self.fade_in_timer.start(GAME_FADE_IN_TIME) def pause(self): #pygame.mixer.pause() self.ambient_channel.pause() self.countdown.pause() self.state = GAME_PAUSE def unpause(self): #pygame.mixer.unpause() self.ambient_channel.unpause() self.countdown.unpause() self.state = GAME_PLAY def fade_into_main_menu(self): # does the majority of resetting here if necessary # fades out if there's still sound for some reason self.countdown.start() self.countdown.pause() self.ambient_channel.fadeout(2) self.character.reset() for building in self.buildings: for collectible in building.collectibles: collectible.reset() self.camera = Coords(CHARACTER_START[X], CHARACTER_START[Y]) self.fade_in_timer.start(MAIN_MENU_FADE_IN_TIME) self.state = MAIN_MENU_FADE_IN def to_main_menu(self): self.state = MAIN_MENU def fade_outof_main_menu(self): self.state = MAIN_MENU_FADE_OUT self.fade_out_timer.start(MAIN_MENU_FADE_OUT_TIME) def end(self): if not self.countdown.ended: self.countdown.pause() self.state = GAME_FADE_OUT self.fade_out_timer.start(GAME_FADE_OUT_TIME) # gets the end image if not self.character.escaped: self.end_image = load_image("endings/stay_death.png", return_rect=False) elif sum(self.character.items.values()) <= 2: self.end_image = load_image("endings/missing_lots_death.png", return_rect=False) elif self.character.has_no_items("filled_water_skin"): self.end_image = load_image("endings/thirst_death.png", return_rect=False) elif self.character.has_no_items("ugly_green_scarf"): self.end_image = load_image("endings/cold_death.png", return_rect=False) elif self.character.has_no_items("bread", "cheese", "jerky"): self.end_image = load_image("endings/starvation_death.png", return_rect=False) elif self.character.has_no_items("bandages", "bow_arrow", "cheese"): self.end_image = load_image("endings/bandits_death.png", return_rect=False) elif self.character.has_no_items("pileogold"): self.end_image = load_image("endings/no_money_death.png", return_rect=False) else: self.end_image = load_image("endings/win_happy.png", return_rect=False) self.end_image = pygame.transform.scale(self.end_image, SCREEN_SIZE) if DEBUG: print("ended!")
class AlienInvasion: """Overall class to manage game assets and behaviour.""" def __init__(self): """Initialize the game, create game resources.""" pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) pygame.display.set_caption("Alien Invasion") # Create an instance to store game stats and create a scoreboard self.stats = GameStats(self) self.sb = Scoreboard(self) self.ship = Ship(self) self.bullets = pygame.sprite.Group() self.aliens = pygame.sprite.Group() self._create_fleet() # Make the title banner and play button self.play_button = Button(self, "START") self.title_banner = Banner(self, "Alien Invasion") # Make a countdown object for displaying count on game start / new ship self.countdown = Countdown(self) # Make an object for a banner displaying "GAME OVER" when no ships # remain self.gameover = GameOver(self) def run_game(self): """Start the main loop of the game.""" while True: self._check_events() if self.stats.game_active: self.ship.update() self._update_bullets() self._update_aliens() self._update_screen() def _check_events(self): """ Watch for keyboard and mouse events""" for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() self._check_play_button(mouse_pos) elif event.type == pygame.KEYDOWN: self._check_keydown_events(event) elif event.type == pygame.KEYUP: self._check_keyup_events(event) def _check_keydown_events(self, event): """Respond to keypresses.""" if event.key == pygame.K_RIGHT: self.ship.moving_right = True elif event.key == pygame.K_LEFT: self.ship.moving_left = True elif event.key == pygame.K_q: sys.exit() elif event.key == pygame.K_SPACE: self._fire_bullet() def _check_keyup_events(self, event): """Respond to key releases""" if event.key == pygame.K_RIGHT: self.ship.moving_right = False elif event.key == pygame.K_LEFT: self.ship.moving_left = False def _update_screen(self): # Redraw the screen during each pass through the loop self.screen.fill(self.settings.bg_color) self.ship.blitme() for bullet in self.bullets.sprites(): bullet.draw_bullet() self.aliens.draw(self.screen) # Draw the score information self.sb.show_score() # Draw the play button if the game is inactive if not self.stats.game_active: self.title_banner.draw_banner() self.play_button.draw_button() # Make the most recently drawn screen visible. pygame.display.flip() def _reset_game(self): """Clears game statistics and resets all sprites.""" self.stats.reset_stats() self.settings.initialize_dynamic_settings() self.sb.prep_score() self.sb.prep_level() self.sb.prep_ships() # Delete remaining ships and bullets self.aliens.empty() self.bullets.empty() # Create new alien fleet and center the ship self._create_fleet() self.ship.center_ship() def _check_play_button(self, mouse_pos): """Start a new game when the player clicks START.""" button_clicked = self.play_button.rect.collidepoint(mouse_pos) if button_clicked and not self.stats.game_active: # Reset the game statistics and settings self._reset_game() self.stats.game_active = True self._count_down(3) # Hide the mouse cursor pygame.mouse.set_visible(False) def _count_down(self, start): """Counts down from start and calls current count to be rendered.""" count = start while count > 0: self._update_screen() self.countdown.draw_count(count) pygame.display.flip() sleep(1) count -= 1 def _fire_bullet(self): """Create a new bullet and add it to the bullets group.""" if len(self.bullets) < self.settings.bullets_allowed: new_bullet = Bullet(self) self.bullets.add(new_bullet) def _update_bullets(self): """Update position of bullets and delete old bullets.""" self.bullets.update() for bullet in self.bullets.copy(): if bullet.rect.bottom <= 0: self.bullets.remove(bullet) self._check_bullet_alien_collisions() def _create_fleet(self): """Create a fleet of aliens.""" # Create an alien and find the number of aliens in a row # Spacing between each alien is equal to one alien width # Make an alien alien = Alien(self) alien_width, alien_height = alien.rect.size available_space_x = self.settings.screen_width - (2 * alien_width) number_aliens_x = available_space_x // (2 * alien_width) # Determine the number of rows of aliens that fit on the screen ship_height = self.ship.rect.height available_space_y = (self.settings.screen_height - (3 * alien_height) - ship_height) number_rows = available_space_y // (2 * alien_height) # Create the full fleet of aliens for row_number in range(number_rows): for alien_number in range(number_aliens_x): self._create_alien(alien_number, row_number) def _create_alien(self, alien_number, row_number): """Create an alien and place it in the row.""" alien = Alien(self) alien_width, alien_height = alien.rect.size alien.x = alien_width + 2 * alien_width * alien_number alien.rect.x = alien.x alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number self.aliens.add(alien) def _check_fleet_edges(self): """Respond appropriately if any aliens have reached an edge.""" for alien in self.aliens.sprites(): if alien.check_edges(): self._change_fleet_direction() break def _change_fleet_direction(self): """Drop the entire fleet and change the fleet's direction.""" for alien in self.aliens.sprites(): alien.rect.y += self.settings.fleet_drop_speed self.settings.fleet_direction *= -1 def _update_aliens(self): """Check if the fleet is at an edge, then update the positions of all aliens in the fleet.""" self._check_fleet_edges() self.aliens.update() # Look for alien-ship collisions if pygame.sprite.spritecollideany(self.ship, self.aliens): self._ship_hit() # Look for aliens hitting the bottom of the screen self._check_aliens_bottom() def _check_bullet_alien_collisions(self): """Respond to bullet-alien collisions.""" # Check for bullets that have hit aliens. # If so, get rid of the bullet and the aliens. collisions = pygame.sprite.groupcollide(self.bullets, self.aliens, True, True) if collisions: for aliens in collisions.values(): self.stats.score += self.settings.alien_points * len(aliens) self.sb.prep_score() self.sb.check_high_score() if not self.aliens: # Destroy existing bullets and create new fleet self.bullets.empty() self._create_fleet() # Increase level self.settings.increase_speed() self.stats.level += 1 self.sb.prep_level() def _ship_hit(self): """Respond to the ship being hit by an alien.""" # Decrement ships left and update scoreboard self.stats.ships_left -= 1 self.sb.prep_ships() if self.stats.ships_left > 0: # Get rid of any remaining aliens and bullets self.aliens.empty() self.bullets.empty() # Create a new fleet and center the ship self._create_fleet() self.ship.center_ship() # Pause sleep(1) self._count_down(3) else: self.stats.game_active = False self._game_over() pygame.mouse.set_visible(True) def _check_aliens_bottom(self): """Check if any aliens have reached the bottom of the screen.""" screen_rect = self.screen.get_rect() for alien in self.aliens.sprites(): if alien.rect.bottom >= screen_rect.bottom: # Treat this the same as if the ship got hit self._ship_hit() break def _game_over(self): """Displays a GAME OVER banner for three seconds when no ships remain.""" self.gameover.draw_game_over() pygame.display.flip() sleep(3)
def check_buttons(): global buttons, optos x = buttons.all_pressed() if x: print(x) x = optos.all_broken() if x: print(x) lights.turn_off_all() outlets.turn_off_all() print("Starting countdown") countdown = Countdown(12) countdown.start() time.sleep(3) countdown.stop() time.sleep(2) countdown.reset() time.sleep(3) print("Done") # Sleep time variables sleeptime = 0.1 # MAIN LOOP ===== # =============== try:
def __exit_safely(): m_listener.stop() k_listener.stop() logger.shutdown() Countdown.stop_all() logger.queue_callback(__exit_immediately)
# Charlie Conneely # Anagram game from countdown import Countdown from score_system import ScoreKeeper from player import Player countdown = Countdown() score_keeper = ScoreKeeper() """ Call play_countdown in Countdown class Keep track of score Check rankings """ def main(name): rounds = 3 player = Player(name, 0) print("\nWe're going for best of " + str(rounds) + " rounds!") for x in range(rounds): points = countdown.play_countdown(player.name) player.score += points print("Total Score: " + str(player.score) + "\n") score_keeper.check_ranking(player) """
s = Stopwatch(w, stopwatch_lap_count, stopwatch_current_time, stopwatch_lap) except: # Initial the class of Stopwatch s = Stopwatch(w) try: # Read the operation history of countdown countdown_history = open(os.path.abspath(os.path.dirname(__file__)) + '/save/countdown_save.bin', 'rb') countdown_lines = countdown_history.readlines() countdown_set_time = countdown_lines[0].decode().strip('\n').split(':') countdown_current_time = countdown_lines[1].decode().split(':') for i in range(0, len(countdown_set_time)): countdown_set_time[i] = int(countdown_set_time[i]) countdown_current_time[i] = int(countdown_current_time[i]) # Initial the class of Countdown c = Countdown(w, countdown_set_time, countdown_current_time) except: # Initial the class of Countdown c = Countdown(w) # Declare timer for stopwatch and countdown w.stopwatch_timer = QTimer() w.countdown_timer = QTimer() # Load beep voice w.beep_sound = QSound(os.path.abspath(os.path.dirname(__file__)) + '/sound/Old-clock-ringing-sound-effect.wav') w.beep_sound.setLoops(10) # Connect to slot function of about widget w.actionAbout.triggered.connect(lambda: actionAbout(w_about))
class Level(event.EventDispatcher): def __init__(self, p_window): self.p_window = p_window winx, winy = self.p_window.get_size() self.camera = camera.Camera(0,0, winx, winy, 50) self.over = False # Setup background, draw batch, spritelist, player self.background = load(fp('background.png')) self.batch = graphics.Batch() self.sprites = [] self.player = None self.surface_y = 195 self.gravitoids = [] self.enemies = [] self.initialize() # Setup the level def initialize(self): self.player = Player(self, batch=self.batch) self.gravitoids.append(self.player) self.sprites.append(self.player) # a countdown clock. Countdown to success. self.timer = Countdown(self, batch=self.batch) self.sprites.append(self.timer) # winning and losing ghosts. # will be a list of GhostOutcome tuples (ghost, win? True/False) self.ghosts_of_christmas_past = [] # Connect the level's handlers, to the window's dispatchers def connect(self): self.p_window.push_handlers( self.on_update, self.on_draw ) # Pop the window's newest added handlers, hopefully this level's! def disconnect(self): self.p_window.pop_handlers() # Gets called once per tick by the game_window def on_update(self, dt): player_box = self.player.get_collision_box() for e in self.enemies: if e.get_collision_box().isCollide(*player_box.get_values()): try: self.handle_the_dead(e) except ValueError: # lost the game! self.batch = graphics.Batch() self.sprites[:] = [] self.background = load(fp('loser.png')) self.over = True self.do_gravity(dt) if not self.over: self.dispatch_event('on_level_update', dt, self.camera) self.game_strategy(dt) def game_strategy(self, dt): """ Game strategising comes into play here. A countdown is created and then once the time is up, a new ghost appears and countdown clock begins ticking again. """ if self.timer.alarm: if self.enemies and self.enemies[-1].x == 700: return self.timer.reset(random.randint(2, 5)) enemy = Enemy(self, batch=self.batch) self.sprites.append(enemy) self.enemies.append(enemy) def do_gravity(self, dt): for g in self.gravitoids: if g.y <= self.surface_y: g.velocity_y = 0 g.y = self.surface_y g.touch_ground = True else: g.velocity_y -= settings.GRAVITY * dt def char_punch(self, attack_box): """ If a character punches an enemy, the enemy dies and goes to the naughty corner. """ if not self.player.orientation_right: return for e in self.enemies: if attack_box.isCollide(*e.get_collision_box().get_values()): try: self.player_is_victorious_with_punch(e) except TypeError: self.batch = graphics.Batch() self.sprites[:] = [] self.over = True self.background = load(fp('winner.png')) # Gets called once per tick by the game loop def on_draw(self): self.p_window.clear() self.background.blit(0,0) self.batch.draw() # Called by dispatcher on game_window, when required def on_key_press(self, symbol, modifiers): """ Look for left and right arrows. """ handlers = { key.ESCAPE: self.handle_quit, pyglet.window.key.UP: self.handle_up_down, pyglet.window.key.LEFT: self.handle_left_down, pyglet.window.key.RIGHT: self.handle_right_down} try: handler = handlers[symbol] except KeyError: pass else: handler() def handle_up_down(self): """ Make character jump. """ if self.player: if not self.player.movement: self.player.jump() def handle_left_down(self): """ Move character left. """ if self.player: if not self.player.movement: self.player.step_left() def handle_right_down(self): """ Move character right. """ if self.player: if not self.player.movement: self.player.step_right() def handle_quit(self): self.p_window.reset() def player_is_victorious_with_punch(self, ghost): """ The player has successfully punched the ghost! """ ghost.set_dead() punched_ghosts = filter(lambda x: not x.won, self.ghosts_of_christmas_past) if len(punched_ghosts) >= 3: raise TypeError try: maxx = punched_ghosts[-1].ghost.x except IndexError: maxx = 0 newx = maxx + ghost.width + 10 ghost.x, ghost.y = newx, 50 self.ghosts_of_christmas_past.append( GhostOutcome(ghost, False)) def handle_the_dead(self, ghost): """ The ghost has hit our player! thats a bad thing. Put the ghost up on a victory tally somewhere.. If there has been three dead already. Exit the game. """ ghost.set_dead() victorious_ghosts = filter(lambda x: x.won, self.ghosts_of_christmas_past) if len(victorious_ghosts) >= 3: raise ValueError try: minx = victorious_ghosts[-1].ghost.x except IndexError: minx = settings.RESOLUTION[0] newx = minx - ghost.width - 10 ghost.x, ghost.y = newx, 50 self.ghosts_of_christmas_past.append( GhostOutcome(ghost, True))