def main(): #cause I don't want to pass these around global WINDOW, REN, SPRITE_FACTORY, SPRITE_RENDERER, MUSIC SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) WINDOW = ext.Window("Tetromino", size=(WINDOWWIDTH, WINDOWHEIGHT)) REN = ext.Renderer(WINDOW) WINDOW.show() font_file = sysfont.get_font("freesans", sysfont.STYLE_BOLD) font_manager = ext.FontManager(font_file, size=18) #fontmanager=font_manager will be default_args passed to every sprite creation method SPRITE_FACTORY = ext.SpriteFactory(renderer=REN, fontmanager=font_manager, free=True) SPRITE_RENDERER = SPRITE_FACTORY.create_sprite_render_system(WINDOW) sdlmixer.Mix_Init(sdlmixer.MIX_INIT_OGG) sdlmixer.Mix_OpenAudio(22050, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024) #BEEP1 = sdlmixer.Mix_LoadWAV(b"beep1.ogg") showTextScreen("Tetromino") while True: if random.randint(0, 1) == 0: MUSIC = sdlmixer.Mix_LoadMUS(b"tetrisb.mid") else: MUSIC = sdlmixer.Mix_LoadMUS(b"tetrisc.mid") sdlmixer.Mix_PlayMusic(MUSIC, -1) runGame() sdlmixer.Mix_HaltMusic() showTextScreen("Game Over")
def main(): global WINDOW, REN, SPRITE_FACTORY, SPRITE_RENDERER, BG_SPR sdl2.ext.init() WINDOW = sdl2.ext.Window("Flippy", size=(WINDOWWIDTH, WINDOWHEIGHT)) REN = sdl2.ext.Renderer(WINDOW, flags=sdl2.SDL_RENDERER_SOFTWARE) WINDOW.show() font_file = sysfont.get_font("freesans", sysfont.STYLE_BOLD) font_manager = sdl2.ext.FontManager(font_file, size=16) #fontmanager=font_manager will be default_args passed to every sprite creation method SPRITE_FACTORY = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE, renderer=REN, fontmanager=font_manager, free=True) SPRITE_RENDERER = SPRITE_FACTORY.create_sprite_render_system(WINDOW) bg_image = load_n_scale_img('flippybackground.png', WINDOWWIDTH, WINDOWHEIGHT) board_surf = load_n_scale_img('flippyboard.png', BOARDWIDTH*SPACESIZE, BOARDWIDTH*SPACESIZE) SDL_BlitSurface(board_surf, None, bg_image, SDL_Rect(XMARGIN, YMARGIN, 0, 0)) print(bg_image.w, bg_image.h) BG_SPR = SPRITE_FACTORY.from_surface(bg_image) print(BG_SPR.position, BG_SPR.size) while True: if runGame() == False: break
def main(): #cause I don't want to pass these around global WINDOW, REN, SPRITE_FACTORY, SPRITE_RENDERER, MUSIC SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) WINDOW = ext.Window("Tetromino for Idiots", size=(WINDOWWIDTH, WINDOWHEIGHT)) REN = ext.Renderer(WINDOW) WINDOW.show() font_file = sysfont.get_font("freesans", sysfont.STYLE_BOLD) font_manager = ext.FontManager(font_file, size=18) #fontmanager=font_manager will be default_args passed to every sprite creation method SPRITE_FACTORY = ext.SpriteFactory(renderer=REN, fontmanager=font_manager, free=True) SPRITE_RENDERER = SPRITE_FACTORY.create_sprite_render_system(WINDOW) sdlmixer.Mix_Init(sdlmixer.MIX_INIT_OGG) sdlmixer.Mix_OpenAudio(22050, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024) #BEEP1 = sdlmixer.Mix_LoadWAV(b"beep1.ogg") showTextScreen("Tetromino for Idiots") while True: if random.randint(0, 1) == 0: MUSIC = sdlmixer.Mix_LoadMUS(b"tetrisb.mid") else: MUSIC = sdlmixer.Mix_LoadMUS(b"tetrisc.mid") sdlmixer.Mix_PlayMusic(MUSIC, -1) runGame() sdlmixer.Mix_HaltMusic() showTextScreen("Game Over")
def main(): global ren, window, sprite_factory, sprite_renderer sdl2.ext.init() window = sdl2.ext.Window("testing", size=(WIDTH, HEIGHT)) window.show() ren = sdl2.ext.Renderer(window, flags=sdl2.SDL_RENDERER_SOFTWARE) font_file = sysfont.get_font("freesans") print(font_file) font_manager = sdl2.ext.FontManager(font_file, size=24) #fontmanager=font_manager will be default_args passed to every sprite creation method sprite_factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE, renderer=ren, fontmanager=font_manager, free=True) sprite_renderer = sprite_factory.create_sprite_render_system(window) #test1(1000) #test_surface_drawing() #sdl2.SDL_Delay(1000) #test_surface_sprites() ren.present() while True: for event in get_events(): if event.type == sdl2.SDL_QUIT: shutdown() elif event.type == sdl2.SDL_MOUSEBUTTONUP: mousex, mousey = event.button.x, event.button.y elif event.type == sdl2.SDL_KEYDOWN: sym = event.key.keysym.sym if sym == sdl2.SDLK_ESCAPE: shutdown() test_surface_sprites()
def test_get_font(self): fontnames = ["sans", "arial", "helvetica", "times new roman", "serif"] # At least two fonts must be found. success = 0 for fname in fontnames: fontfile = sysfont.get_font(fname) if fontfile is not None: success += 1 self.assertGreaterEqual( success, 2, "could not find the required fonts for get_font()")
def test_get_font(self): fontnames = ["sans", "arial", "helvetica", "times new roman", "serif"] # At least two fonts must be found. success = 0 for fname in fontnames: fontfile = sysfont.get_font(fname) if fontfile is not None: success += 1 self.assertGreaterEqual(success, 2, "could not find the required fonts for get_font()")
def main(): #cause I don't want to pass these around global WINDOW, REN, SPRITE_FACTORY, SPRITE_RENDERER sdl2.ext.init() WINDOW = sdl2.ext.Window("Wormy", size=(WINDOWWIDTH, WINDOWHEIGHT)) REN = sdl2.ext.Renderer(WINDOW, flags=sdl2.SDL_RENDERER_SOFTWARE) WINDOW.show() font_file = sysfont.get_font("freesans", sysfont.STYLE_BOLD) font_manager = sdl2.ext.FontManager(font_file, size=18) #fontmanager=font_manager will be default_args passed to every sprite creation method SPRITE_FACTORY = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE, renderer=REN, fontmanager=font_manager, free=True) SPRITE_RENDERER = SPRITE_FACTORY.create_sprite_render_system(WINDOW) showStartScreen() while True: runGame() showGameOverScreen()
def main(): #cause I don't want to pass these around global REN, SPRITE_FACTORY, SPRITE_RENDERER global CLICKEDBUTTON, BEEP1, BEEP2, BEEP3, BEEP4 sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO|sdl2.SDL_INIT_AUDIO) window = sdl2.ext.Window("Simulate", size=(WINDOWWIDTH, WINDOWHEIGHT)) REN = sdl2.ext.Renderer(window) REN.blendmode = sdl2.SDL_BLENDMODE_BLEND window.show() font_file = sysfont.get_font("freesans", sysfont.STYLE_BOLD) font_manager = sdl2.ext.FontManager(font_file, size=16) #fontmanager=font_manager will be default_args passed to every sprite creation method SPRITE_FACTORY = sdl2.ext.SpriteFactory(renderer=REN, fontmanager=font_manager, free=True) SPRITE_RENDERER = SPRITE_FACTORY.create_sprite_render_system(window) sdlmixer.Mix_Init(sdlmixer.MIX_INIT_OGG) sdlmixer.Mix_OpenAudio(44100, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024) BEEP1 = sdlmixer.Mix_LoadWAV(b"beep1.ogg") BEEP2 = sdlmixer.Mix_LoadWAV(b"beep2.ogg") BEEP3 = sdlmixer.Mix_LoadWAV(b"beep3.ogg") BEEP4 = sdlmixer.Mix_LoadWAV(b"beep4.ogg") #channel = sdlmixer.Mix_PlayChannel(-1, BEEP1, 0) # Initialize some variables for a new game pattern = [] # stores the pattern of colors currentStep = 0 # the color the player must push next lastClickTime = 0 # timestamp of the player's last button push score = 0 # when False, the pattern is playing. when True, waiting for the player to click a colored button: waitingForInput = False #directions text sprite info_text = make_text(SPRITE_FACTORY, "Match the pattern by clicking on the button or using the Q, W, A, S keys.", 10, WINDOWHEIGHT-25) CLICKEDBUTTON = [] while True: REN.fill((0, 0, WINDOWWIDTH, WINDOWHEIGHT), BGCOLOR) drawButtons() score_text = make_text(SPRITE_FACTORY, "Score: "+str(score), WINDOWWIDTH - 100, 10) SPRITE_RENDERER.render([score_text, info_text]) handle_events() if not waitingForInput: # play the pattern sdl2.SDL_Delay(1000) pattern.append(random.choice((YELLOW, BLUE, RED, GREEN))) for button in pattern: handle_events() flashButtonAnimation(button) sdl2.SDL_Delay(FLASHDELAY) waitingForInput = True else: # wait for the player to enter buttons if CLICKEDBUTTON and CLICKEDBUTTON[0] == pattern[currentStep]: # pushed the correct button flashButtonAnimation(CLICKEDBUTTON[0]) currentStep += 1 lastClickTime = sdl2.SDL_GetTicks() #could replace with collections.deque but premature optimizations and all that CLICKEDBUTTON.pop(0) if currentStep == len(pattern): # pushed the last button in the pattern changeBackgroundAnimation() score += 1 waitingForInput = False currentStep = 0 # reset back to first step #CLICKEDBUTTON.clear() clear added in 3.3! ... I'm surprised it hasn't been there forever since it's way better than del l[:] or l[:] = [] #and it parallels other collection clear functions del CLICKEDBUTTON[:] elif (CLICKEDBUTTON and CLICKEDBUTTON[0] != pattern[currentStep]) or (currentStep != 0 and sdl2.SDL_GetTicks() - TIMEOUT*1000 > lastClickTime): # pushed the incorrect button, or has timed out gameOverAnimation() # reset the variables for a new game: pattern = [] #CLICKEDBUTTON.clear() del CLICKEDBUTTON[:] currentStep = 0 waitingForInput = False score = 0 sdl2.SDL_Delay(1000) changeBackgroundAnimation() sdl2.SDL_Delay(1000//FPS) shutdown()
def main(): #cause I don't want to pass these around global REN, SPRITE_FACTORY, SPRITE_RENDERER global RESET_BUTTON, NEW_BUTTON, SOLVE_BUTTON sdl2.ext.init() window = sdl2.ext.Window("Slide Puzzle", size=(WINDOWWIDTH, WINDOWHEIGHT)) REN = sdl2.ext.Renderer(window) window.show() font_file = sysfont.get_font("freesans", sysfont.STYLE_BOLD) font_manager = sdl2.ext.FontManager(font_file, size=BASICFONTSIZE) #fontmanager=font_manager will be default_args passed to every sprite creation method SPRITE_FACTORY = sdl2.ext.SpriteFactory(renderer=REN, fontmanager=font_manager, free=True) SPRITE_RENDERER = SPRITE_FACTORY.create_sprite_render_system(window) RESET_BUTTON, reset_rect = make_text(SPRITE_FACTORY, "Reset", WINDOWWIDTH - 120, WINDOWHEIGHT - 90, bg_color=TILECOLOR) NEW_BUTTON, new_rect = make_text(SPRITE_FACTORY, "New Game", WINDOWWIDTH - 120, WINDOWHEIGHT - 60, bg_color=TILECOLOR) SOLVE_BUTTON, solve_rect = make_text(SPRITE_FACTORY, "Solve", WINDOWWIDTH - 120, WINDOWHEIGHT - 30, bg_color=TILECOLOR) mainBoard, solutionSeq = generateNewPuzzle(NUMSLIDES) SOLVEDBOARD = getStartingBoard() # a solved board is the same as the board in a start state. allMoves = [] # list of moves made from the solved configuration print(NEW_BUTTON.area) running = True while running: slideTo = None # the direction, if any, a tile should slide msg = 'Click tile or press arrow keys to slide.' # contains the message to show in the upper left corner. if mainBoard == SOLVEDBOARD: msg = 'Solved!' drawBoard(mainBoard, msg) for event in sdl2.ext.get_events(): if event.type == sdl2.SDL_QUIT: running = False break elif event.type == sdl2.SDL_WINDOWEVENT: if event.window.event == sdl2.SDL_WINDOWEVENT_EXPOSED: print("window exposed") drawBoard(mainBoard, msg) elif event.type == sdl2.SDL_MOUSEBUTTONUP: pos = event.button.x, event.button.y spotx, spoty = getSpotClicked(mainBoard, pos[0], pos[1]) pt = sdl2.SDL_Point(pos[0], pos[1]) if (spotx, spoty) == (None, None): if sdl2.SDL_PointInRect(pt, reset_rect): resetAnimation(mainBoard, allMoves) allMoves = [] elif sdl2.SDL_PointInRect(pt, new_rect): mainBoard, solutionSeq = generateNewPuzzle(NUMSLIDES) allMoves = [] elif sdl2.SDL_PointInRect(pt, solve_rect): resetAnimation(mainBoard, solutionSeq + allMoves) allMoves = [] else: #check if clicked tile was next to blank blankx, blanky = getBlankPosition(mainBoard) if spotx == blankx + 1 and spoty == blanky: slideTo = LEFT elif spotx == blankx - 1 and spoty == blanky: slideTo = RIGHT elif spotx == blankx and spoty == blanky + 1: slideTo = UP elif spotx == blankx and spoty == blanky - 1: slideTo = DOWN elif event.type == sdl2.SDL_KEYUP: # check if the user pressed a key to slide a tile sym = event.key.keysym.sym if sym in (sdl2.SDLK_LEFT, sdl2.SDLK_a) and isValidMove(mainBoard, LEFT): slideTo = LEFT elif sym in (sdl2.SDLK_RIGHT, sdl2.SDLK_d) and isValidMove(mainBoard, RIGHT): slideTo = RIGHT elif sym in (sdl2.SDLK_UP, sdl2.SDLK_w) and isValidMove(mainBoard, UP): slideTo = UP elif sym in (sdl2.SDLK_DOWN, sdl2.SDLK_s) and isValidMove(mainBoard, DOWN): slideTo = DOWN elif event.key.keysym.sym == sdl2.SDLK_ESCAPE: running = False break if slideTo: slideAnimation(mainBoard, slideTo, 'Click tile or press arrow keys to slide.', 8) # show slide on screen allMoves.append(slideTo) # record the slide REN.present() sdl2.SDL_Delay(1000//FPS) sdl2.ext.quit()
def main(): #cause I don't want to pass these around global REN, SPRITE_FACTORY, SPRITE_RENDERER global RESET_BUTTON, NEW_BUTTON, SOLVE_BUTTON sdl2.ext.init() window = sdl2.ext.Window("Slide Puzzle", size=(WINDOWWIDTH, WINDOWHEIGHT)) REN = sdl2.ext.Renderer(window) window.show() font_file = sysfont.get_font("freesans", sysfont.STYLE_BOLD) font_manager = sdl2.ext.FontManager(font_file, size=BASICFONTSIZE) #fontmanager=font_manager will be default_args passed to every sprite creation method SPRITE_FACTORY = sdl2.ext.SpriteFactory(renderer=REN, fontmanager=font_manager, free=True) SPRITE_RENDERER = SPRITE_FACTORY.create_sprite_render_system(window) RESET_BUTTON, reset_rect = make_text(SPRITE_FACTORY, "Reset", WINDOWWIDTH - 120, WINDOWHEIGHT - 90, bg_color=TILECOLOR) NEW_BUTTON, new_rect = make_text(SPRITE_FACTORY, "New Game", WINDOWWIDTH - 120, WINDOWHEIGHT - 60, bg_color=TILECOLOR) SOLVE_BUTTON, solve_rect = make_text(SPRITE_FACTORY, "Solve", WINDOWWIDTH - 120, WINDOWHEIGHT - 30, bg_color=TILECOLOR) mainBoard, solutionSeq = generateNewPuzzle(NUMSLIDES) SOLVEDBOARD = getStartingBoard( ) # a solved board is the same as the board in a start state. allMoves = [] # list of moves made from the solved configuration print(NEW_BUTTON.area) running = True while running: slideTo = None # the direction, if any, a tile should slide msg = 'Click tile or press arrow keys to slide.' # contains the message to show in the upper left corner. if mainBoard == SOLVEDBOARD: msg = 'Solved!' drawBoard(mainBoard, msg) for event in sdl2.ext.get_events(): if event.type == sdl2.SDL_QUIT: running = False break elif event.type == sdl2.SDL_WINDOWEVENT: if event.window.event == sdl2.SDL_WINDOWEVENT_EXPOSED: print("window exposed") drawBoard(mainBoard, msg) elif event.type == sdl2.SDL_MOUSEBUTTONUP: pos = event.button.x, event.button.y spotx, spoty = getSpotClicked(mainBoard, pos[0], pos[1]) pt = sdl2.SDL_Point(pos[0], pos[1]) if (spotx, spoty) == (None, None): if sdl2.SDL_PointInRect(pt, reset_rect): resetAnimation(mainBoard, allMoves) allMoves = [] elif sdl2.SDL_PointInRect(pt, new_rect): mainBoard, solutionSeq = generateNewPuzzle(NUMSLIDES) allMoves = [] elif sdl2.SDL_PointInRect(pt, solve_rect): resetAnimation(mainBoard, solutionSeq + allMoves) allMoves = [] else: #check if clicked tile was next to blank blankx, blanky = getBlankPosition(mainBoard) if spotx == blankx + 1 and spoty == blanky: slideTo = LEFT elif spotx == blankx - 1 and spoty == blanky: slideTo = RIGHT elif spotx == blankx and spoty == blanky + 1: slideTo = UP elif spotx == blankx and spoty == blanky - 1: slideTo = DOWN elif event.type == sdl2.SDL_KEYUP: # check if the user pressed a key to slide a tile sym = event.key.keysym.sym if sym in (sdl2.SDLK_LEFT, sdl2.SDLK_a) and isValidMove( mainBoard, LEFT): slideTo = LEFT elif sym in (sdl2.SDLK_RIGHT, sdl2.SDLK_d) and isValidMove( mainBoard, RIGHT): slideTo = RIGHT elif sym in (sdl2.SDLK_UP, sdl2.SDLK_w) and isValidMove( mainBoard, UP): slideTo = UP elif sym in (sdl2.SDLK_DOWN, sdl2.SDLK_s) and isValidMove( mainBoard, DOWN): slideTo = DOWN elif event.key.keysym.sym == sdl2.SDLK_ESCAPE: running = False break if slideTo: slideAnimation(mainBoard, slideTo, 'Click tile or press arrow keys to slide.', 8) # show slide on screen allMoves.append(slideTo) # record the slide REN.present() sdl2.SDL_Delay(1000 // FPS) sdl2.ext.quit()