Hello, PyGame

來源:互聯網
上載者:User

 

昨晚上ADSL居然斷線,鬱悶中無所事事。看到以前下載的PyGame庫於是開啟看看,覺得挺簡單的。

於是寫了個指令碼耍耍,呵呵

這是一個很簡單的圖片瀏覽程式,有興趣的自己看代碼吧。

## picView - A simple image viewer written using PyGame library.## Copyleft 2008 Bruce Jia#import osimport pygameimport pygame.imagefrom pygame import displaydef isImageFile(filePath):    lowerFilePath = filePath.lower()    if (lowerFilePath.endswith('.jpg') or        lowerFilePath.endswith('.png') or        lowerFilePath.endswith('.bmp') or        lowerFilePath.endswith('.gif') or        lowerFilePath.endswith('.jpeg')):        return True    return Falseclass ImageFolder:    def CurrentImage(self):        if self.index == -1 or len(self.images) == 0:            return ""        return self.images[self.index]    def NextImage(self):        if 0 == len(self.images) or self.index == len(self.images) - 1:            return ""                self.index = self.index + 1        return self.images[self.index]            def PrevImage(self):        if 0 == len(self.images) or self.index == -1:            return ""                self.index = self.index - 1        if self.index >= 0:            return self.images[self.index]        pass    def __init__(self, folderPath):        self.images = []        # TODO: add recursive search        for file in os.listdir(folderPath):            if (os.path.isfile(os.path.join(folderPath, file))):                if (isImageFile(file)):                    self.images.append(os.path.join(folderPath, file))        self.index = -1        passdef ShowImageWithFitScale(screen, imageFilePath, angle=0):    if (imageFilePath == None or imageFilePath == ""):        return        image = pygame.image.load(imageFilePath)    scrWidth, scrHeight = screen.get_size()    image = pygame.transform.rotate(image, angle)            imgWidth, imgHeight = image.get_size()    ratio = 1.0 * imgWidth / imgHeight    if imgWidth > imgHeight:        if imgWidth > scrWidth:            imgWidth = scrWidth            imgHeight = imgWidth / ratio            if imgHeight > scrHeight:                imgHeight = scrHeight                imgWidth = imgHeight * ratio    else:        if imgHeight > scrHeight:            imgHeight = scrHeight            imgWidth = imgHeight * ratio            if (imgWidth > scrWidth):                imgWidth = scrWidth                imgHeight = imgWidth / ratio    image = pygame.transform.scale(image, (int(imgWidth), int(imgHeight)))    posX = (scrWidth - imgWidth) / 2.0    posY = (scrHeight - imgHeight) / 2.0    screen.fill((0, 0, 0))    screen.blit(image, (posX, posY))    pygame.display.flip()        if __name__ == "__main__":    from sys import argv    if(len(argv) < 2):        print "Usage: picView.py "        sys.exit(1)    folderPath = argv[1]    print "Showing pictures in " + folderPath    imageFolder = ImageFolder(folderPath)    display.init()    screen = display.set_mode((0,0), pygame.FULLSCREEN)    quit = False    angle = 0    while True:        for event in pygame.event.get():            if event.type == pygame.QUIT:                quit = True                break            if event.type == pygame.KEYDOWN:                if event.key == pygame.K_ESCAPE:                    quit = True                    break                elif event.key in [pygame.K_UP, pygame.K_PAGEUP, pygame.K_F7, pygame.K_BACKSPACE]:                    angle = 0                    ShowImageWithFitScale(screen, imageFolder.PrevImage(), angle)                elif event.key in [pygame.K_DOWN, pygame.K_PAGEDOWN, pygame.K_F8, pygame.K_RETURN, pygame.K_SPACE]:                    angle = 0                    ShowImageWithFitScale(screen, imageFolder.NextImage(), angle)                elif event.key == pygame.K_LEFT:                    angle = angle + 90                    ShowImageWithFitScale(screen, imageFolder.CurrentImage(), angle)                elif event.key == pygame.K_RIGHT:                    angle = angle - 90                    ShowImageWithFitScale(screen, imageFolder.CurrentImage(), angle)                elif event.key == pygame.K_F5:                    angle = 0                    ShowImageWithFitScale(screen, imageFolder.CurrentImage(), angle)            elif event.type == pygame.MOUSEBUTTONDOWN:                ShowImageWithFitScale(screen, imageFolder.NextImage())        if quit:            break    print "Byebye!"    display.quit()

 

Coding for fun!

del.icio.us Tags: python,pygame

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.