def main(): #global donePlaying #global titleScreen #global howToPlay #global mainGame #global gameOver #global creditScreen #global startMenu #global menu #global clearSprites while Globals.donePlaying != True: print Globals.donePlaying if Globals.titleScreen == True: Title.title() elif Globals.howToPlay == True: Instructions.instructions() elif Globals.mainGame == True: Game.game() elif Globals.pauseMenu == True: PauseMenu.pausemenu() elif Globals.gameOver == True: GameOver.gameover() elif Globals.creditScreen == True: Credits.credits() elif Globals.winScreen == True: Win.win()
checkOn = None dragging = False #If we're dragging then we update the check we clicked on if dragging == True: checkOn.rect.center = pygame.mouse.get_pos() #Re-render our text incase score changed scoreScreen = scoreText.render(str(score), False, (255, 255, 255)) #Update MainSprites.bars.update() # Draws everything screen.blit(image, [0, 0]) drawAll() screen.blit(scoreScreen, (300, 25)) pygame.display.flip() #Shows the camera _, frame = cap.read() frame = frame[0:240, 0:320] cv2.imshow("Camera", frame) #If we've won we stop running if score >= 30: running = False #And we call the win screen Win.main(images) pygame.quit()
def start(self): score = 0 running = True while running: self.clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False all_sprites.update() hits = pygame.sprite.groupcollide(mobs, bullets, True, True) for hit in hits: score += 50 - hit.radius random.choice(expl_sounds).play() expl = Explosion(hit.rect.center, 'lg') all_sprites.add(expl) if random.random() > 0.9: pow = Pow(hit.rect.center) all_sprites.add(pow) powerups.add(pow) newmob() hits = pygame.sprite.spritecollide(player, mobs, True, pygame.sprite.collide_circle) for hit in hits: player.shield -= hit.radius * 2 expl = Explosion(hit.rect.center, 'sm') all_sprites.add(expl) newmob() if player.shield <= 0: death_explosion = Explosion(player.rect.center, 'player') all_sprites.add(death_explosion) player.hide() player.lives -= 1 player.shield = 100 hits = pygame.sprite.spritecollide(player, powerups, True) for hit in hits: if hit.type == 'shield': player.shield += random.randrange(10, 30) if player.shield >= 100: player.shield = 100 if hit.type == 'gun': player.powerup() power_sound.play() if player.lives == 0 and not death_explosion.alive(): Win.ShowGoScreen(score) cur.execute("INSERT INTO Space VALUES(1, {}, {})".format( score, datetime.now())) con.commit() if score >= 1000: Win.VictoryScreen(score) cur.execute("INSERT INTO Space VALUES(1, {}, {})".format( score, datetime.now())) con.commit() self.screen.fill(BLACK) self.screen.blit(background, background_rect) all_sprites.draw(self.screen) draw_text(self.screen, str(score), 18, WIDTH / 2, 10) draw_shield_bar(self.screen, 5, 5, player.shield) draw_lives(self.screen, WIDTH - 100, 5, player.lives, player_mini_img) pygame.display.flip() con.close() pygame.quit()
def __init__(self): #Model #Display class self.__board = Display() #Turn class self.__turn = Turn() #Win class self.__win = Win() #list of boxes on board self.list_frames = [" ", " ", " ", " ", " ", " ", " ", " ", " "] #Describes basic rules of tic tac toe game rules = print("This is tictactoe! Get three X's or O's in a row," " column, or diaganol to win! Enter in a number 1-9" " to place your X or O. Enter 1 for row one, column" " 1, Enter 2 for row one, column 2, and so on!") #Ask user whether they want to play or not decision = decision = input( "Enter 'play' to start. Or anything else to exit: ") while decision == 'play': #Displays board that players play on self.display_screen(self.list_frames) #Ask player 1 to input where they want to place their x x_placement = int(input("What box would you like your X in? ")) #Validates that player entered valid value while not self.__is_valid_placement(x_placement): print("Invalid Value Pick something in range 1-9 inclusive") self.display_screen(self.list_frames) x_placement = int(input("What box would you like your X in? ")) #Places X based on value given self.place_player(self.list_frames, x_placement, 'X') #Checks to see if X has won the game self.player_win(self.list_frames, 'X') #Updates screen display self.display_screen(self.list_frames) #If X has not won then ask player two(o) for their placement if self.__win.current_message() != 'X has won the game!': o_placement = int(input("What box would you like your O in? ")) #Validates range while not self.__is_valid_placement(o_placement): print("Invalid Value pick something in range 1-9 inclusive") self.display_screen(self.list_frames) o_placement = int(input("What box would you like your O in")) #Places O where user inputted self.place_player(self.list_frames, o_placement, 'O') #Checks to see if player O won self.player_win(self.list_frames, 'O') #Updates current winner self.__win.current_message() #If there is no winner continue the loop if self.__win.current_message() == '': continue else: decision = input( "Would you like to play again? Type Y to continue or \ #Enter to quit: ").upper() if decision == 'Y': self.reset_board() self.__win.reset_message() tictactoeCommand()
explosion_anim['player'].append(img) powerup_images = {} powerup_images['shield'] = pygame.image.load( path.join(img_dir, 'shield_gold.png')).convert_alpha() powerup_images['gun'] = pygame.image.load(path.join( img_dir, 'bolt_gold.png')).convert_alpha() shoot_sound = pygame.mixer.Sound(path.join(snd_dir, 'pew.wav')) shield_sound = pygame.mixer.Sound(path.join(snd_dir, 'sheald.ogg')) power_sound = pygame.mixer.Sound(path.join(snd_dir, 'power.ogg')) expl_sounds = [] for snd in ['expl3.wav', 'expl6.wav']: expl_sounds.append(pygame.mixer.Sound(path.join(snd_dir, snd))) pygame.mixer.music.load(path.join(snd_dir, 'flying.mp3')) pygame.mixer.music.set_volume(0.4) all_sprites = pygame.sprite.Group() mobs = pygame.sprite.Group() bullets = pygame.sprite.Group() powerups = pygame.sprite.Group() player = player.Player() all_sprites.add(player) for i in range(8): newmob() score = 0 pygame.mixer.music.play(loops=-1) if __name__ == '__main__': obj = Win.MainWindow()
class tictactoeCommand: def __init__(self): #Model #Display class self.__board = Display() #Turn class self.__turn = Turn() #Win class self.__win = Win() #list of boxes on board self.list_frames = [" ", " ", " ", " ", " ", " ", " ", " ", " "] #Describes basic rules of tic tac toe game rules = print("This is tictactoe! Get three X's or O's in a row," " column, or diaganol to win! Enter in a number 1-9" " to place your X or O. Enter 1 for row one, column" " 1, Enter 2 for row one, column 2, and so on!") #Ask user whether they want to play or not decision = decision = input( "Enter 'play' to start. Or anything else to exit: ") while decision == 'play': #Displays board that players play on self.display_screen(self.list_frames) #Ask player 1 to input where they want to place their x x_placement = int(input("What box would you like your X in? ")) #Validates that player entered valid value while not self.__is_valid_placement(x_placement): print("Invalid Value Pick something in range 1-9 inclusive") self.display_screen(self.list_frames) x_placement = int(input("What box would you like your X in? ")) #Places X based on value given self.place_player(self.list_frames, x_placement, 'X') #Checks to see if X has won the game self.player_win(self.list_frames, 'X') #Updates screen display self.display_screen(self.list_frames) #If X has not won then ask player two(o) for their placement if self.__win.current_message() != 'X has won the game!': o_placement = int(input("What box would you like your O in? ")) #Validates range while not self.__is_valid_placement(o_placement): print("Invalid Value pick something in range 1-9 inclusive") self.display_screen(self.list_frames) o_placement = int(input("What box would you like your O in")) #Places O where user inputted self.place_player(self.list_frames, o_placement, 'O') #Checks to see if player O won self.player_win(self.list_frames, 'O') #Updates current winner self.__win.current_message() #If there is no winner continue the loop if self.__win.current_message() == '': continue else: decision = input( "Would you like to play again? Type Y to continue or \ #Enter to quit: ").upper() if decision == 'Y': self.reset_board() self.__win.reset_message() tictactoeCommand() #Events #If the box is empty then the player can put an X or O #If the box is not empty user will get an error message which will #allow them to play again def place_player(self, list_frames, player_placement, player): if list_frames[player_placement - INDEX] == " ": self.__board.place_x_o(list_frames, player_placement, player) else: if self.__win.current_message() == '': placement = int(input("Box taken! Try another value: ")) self.place_player(self.list_frames, placement, player) def reset_board(self): self.list_frames = [" ", " ", " ", " ", " ", " ", " ", " ", " "] self.__win.reset_winner() self.__win.reset_counter() #Checks to see if X or O won def player_win(self, list_buttons, letter): self.__win.decider_winner(list_buttons, letter) self.__win.decider_message() if self.__win.current_message(): print(self.__win.current_message()) #self.display_screen(self.list_frames) #Displays board using display class def display_screen(self, list_box): self.__board.game_board(list_box) #Private helper #Makes sure number given is 1-9 inclusive def __is_valid_placement(self, num): return (int(num) > MIN_INDEX and int(num) <= MAX_INDEX) #Describes game def game_description(): print("This is a command line version of the game tic-tac-toe")
msg = "High Level Event: %s %s" % \ (`code(message)`, `code(h | (v<<16))`) try: AE.AEProcessAppleEvent(event) except AE.Error, err: EasyDialogs.Message(msg + "\015AEProcessAppleEvent error: %s" % str(err)) traceback.print_exc() else: EasyDialogs.Message(msg + "\015OK!") elif what == keyDown: c = chr(message & charCodeMask) if c == '.' and modifiers & cmdKey: raise KeyboardInterrupt, "Command-period" MacOS.HandleEvent(event) elif what == mouseDown: partcode, window = Win.FindWindow(where) if partcode == inMenuBar: result = Menu.MenuSelect(where) id = (result>>16) & 0xffff # Hi word item = result & 0xffff # Lo word if id == self.appleid: if item == 1: EasyDialogs.Message("Echo -- echo AppleEvents") elif item > 1: name = self.applemenu.GetItem(item) Qd.OpenDeskAcc(name) elif what <> autoKey: print "Event:", (eventname(what), message, when, (h, v), modifiers) ## MacOS.HandleEvent(event) def aehandler(self, request, reply):
import Board # import other files import Player import Win import Move from Bcolors import bcolors player = 2 # this player starts not print( f"{bcolors.BOLD}\nHello! This is a TicTacToe Game! Have Fun!{bcolors.ENDC}" ) # first dialog while not Win.check_win() and not Win.check_tie( ): # game will run until someone wins or ties player = Player.switch_player(player) # switch the active player turn_dialog = "\nPlayer " + str( player) + " its your turn!\n" # generating the dialog before each turn print(turn_dialog) # tell the player that it is his turn Board.print_board() # show the current state of the board Board.print_help() # print out the board map as help Move.make_move(player) # let the player choose a field if Win.check_win(): # check if sm1 won print(f"{bcolors.BOLD}\nYou won player " + str(player) + f"! Congrats!\n{bcolors.ENDC}") # winning dialog Board.print_board() # show winning Board elif Win.check_tie(): # check if tie
class tictactoeGUI: OUTPUT_X = 'X' OUTPUT_O = 'O' def __init__(self): #window and class objects self.__w = Tk() self.__turn = Turn() self.__win = Win() #creation of buttons self.b1 = Button(text='Empty', command=lambda: self.win_check(self.b1), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b2 = Button(text='Empty', command=lambda: self.win_check(self.b2), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b3 = Button(text='Empty', command=lambda: self.win_check(self.b3), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b4 = Button(text='Empty', command=lambda: self.win_check(self.b4), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b5 = Button(text='Empty', command=lambda: self.win_check(self.b5), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b6 = Button(text='Empty', command=lambda: self.win_check(self.b6), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b7 = Button(text='Empty', command=lambda: self.win_check(self.b7), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b8 = Button(text='Empty', command=lambda: self.win_check(self.b8), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b9 = Button(text='Empty', command=lambda: self.win_check(self.b9), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.breset = Button(text='Reset', font="Arial 14 bold", fg='black', bg='red', width=5, height=3, command=self.__reset) self.help = Button(text='Help', font='Arial 14 bold', fg='black', bg='yellow', width=5, height=3, command=self.__helper) self.exit = Button(text='Exit', font='Arial 14 bold', fg='black', bg='white', width=5, height=3, command=self.quit) self.__button_list = [ self.b1, self.b2, self.b3, self.b4, self.b5, self.b6, self.b7, self.b8, self.b9 ] #grid to organize buttons self.b1.grid(row=0, column=0) self.b2.grid(row=0, column=1) self.b3.grid(row=0, column=2) self.b4.grid(row=1, column=0) self.b5.grid(row=1, column=1) self.b6.grid(row=1, column=2) self.b7.grid(row=2, column=0) self.b8.grid(row=2, column=1) self.b9.grid(row=2, column=2) self.breset.grid(row=3, column=1) self.help.grid(row=3, column=0) self.exit.grid(row=3, column=2) mainloop() #----Event Handlers------------------------------------------------------- #resets buttons, and instances from other classes def __reset(self): self.__reset_buttons() self.__turn.reset() self.__win.reset_counter() self.__win.reset_message() self.__win.reset_winner() #checks for win every turn def win_check(self, button): if button['text'] == 'Empty': self.__player1_player2(button) list_button_text = self.__list_converter() self.__win.decider_winner(list_button_text, button['text']) self.__win.decider_message() if self.__win.current_message(): messagebox.showinfo(message=self.__win.current_message()) self.__stop_buttons() #displays how to play def __helper(self): messagebox.showinfo(message="Tic-Tac-Toe\nThis is a game in which" " two players face off and attempt to have three" " X's or O's in a row before the other player. The" " person who does so first wins the game. Should " " there be no winner, then the game is a CAT" " (AKA a draw).\n" "--How to use Buttons--\n" "Reset: Reset the board at anytime\n" "Exit: Exit the window at any time\n") #exits window def quit(self): self.__w.destroy() #----Private Class helpers------------------------------------------------ #resets buttons def __reset_buttons(self): for button in self.__button_list: button['text'] = 'Empty' button['bg'] = 'black' self.b1['command'] = lambda: self.win_check(self.b1) self.b2['command'] = lambda: self.win_check(self.b2) self.b3['command'] = lambda: self.win_check(self.b3) self.b4['command'] = lambda: self.win_check(self.b4) self.b5['command'] = lambda: self.win_check(self.b5) self.b6['command'] = lambda: self.win_check(self.b6) self.b7['command'] = lambda: self.win_check(self.b7) self.b8['command'] = lambda: self.win_check(self.b8) self.b9['command'] = lambda: self.win_check(self.b9) #decides player turn def __player1_player2(self, box1): if box1['text'] == 'Empty' and self.__turn.get_turn_decider() == True: self.__turn.turn() box1['text'] = self.OUTPUT_X box1['bg'] = 'light blue' elif box1['text'] == 'Empty' and self.__turn.get_turn_decider( ) == False: self.__turn.turn() box1['text'] = self.OUTPUT_O box1['bg'] = 'green' #converts buttons texts to list def __list_converter(self): li = [] for button in self.__button_list: li = li + [button['text']] return li #stops buttons from being clicked once game is over def __stop_buttons(self): for button in self.__button_list: button['command'] = lambda: messagebox.showinfo( message="Press" " Reset to start new" " game. Or Exit")
def __init__(self): #window and class objects self.__w = Tk() self.__turn = Turn() self.__win = Win() #creation of buttons self.b1 = Button(text='Empty', command=lambda: self.win_check(self.b1), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b2 = Button(text='Empty', command=lambda: self.win_check(self.b2), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b3 = Button(text='Empty', command=lambda: self.win_check(self.b3), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b4 = Button(text='Empty', command=lambda: self.win_check(self.b4), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b5 = Button(text='Empty', command=lambda: self.win_check(self.b5), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b6 = Button(text='Empty', command=lambda: self.win_check(self.b6), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b7 = Button(text='Empty', command=lambda: self.win_check(self.b7), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b8 = Button(text='Empty', command=lambda: self.win_check(self.b8), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.b9 = Button(text='Empty', command=lambda: self.win_check(self.b9), font="Arial 14 bold", fg='white', bg='black', width=5, height=3) self.breset = Button(text='Reset', font="Arial 14 bold", fg='black', bg='red', width=5, height=3, command=self.__reset) self.help = Button(text='Help', font='Arial 14 bold', fg='black', bg='yellow', width=5, height=3, command=self.__helper) self.exit = Button(text='Exit', font='Arial 14 bold', fg='black', bg='white', width=5, height=3, command=self.quit) self.__button_list = [ self.b1, self.b2, self.b3, self.b4, self.b5, self.b6, self.b7, self.b8, self.b9 ] #grid to organize buttons self.b1.grid(row=0, column=0) self.b2.grid(row=0, column=1) self.b3.grid(row=0, column=2) self.b4.grid(row=1, column=0) self.b5.grid(row=1, column=1) self.b6.grid(row=1, column=2) self.b7.grid(row=2, column=0) self.b8.grid(row=2, column=1) self.b9.grid(row=2, column=2) self.breset.grid(row=3, column=1) self.help.grid(row=3, column=0) self.exit.grid(row=3, column=2) mainloop()