#!python # -*- coding:utf8 -*- # # draw image by pygame_sdl2 try: import pygame_sdl2 pygame_sdl2.import_as_pygame() except: print "pygame_sdl2 not available" import pygame import pygame.image # from pygame.locals import * SCREEN_SIZE = (640, 480) def main(): pygame.init() screen = pygame.display.set_mode( SCREEN_SIZE, pygame.HWSURFACE | pygame.DOUBLEBUF ) pygame.display.set_caption('draw image by pygame_sdl2') sysfont = pygame.font.SysFont(None, 64) textsurface = sysfont.render("Hello world", True, (255, 255, 255)) bg = pygame.image.load('bg.png').convert() image = pygame.image.load('ufo.png').convert_alpha() x = 10 y = 10 dx = 2 dy = 1 bgx = 0 clock = pygame.time.Clock() cnt = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): return if x + dx < 0 or x + dx + image.get_width() >= SCREEN_SIZE[0]: dx *= -1 if y + dy < 0 or y + dy + image.get_height() >= SCREEN_SIZE[1]: dy *= -1 x += dx y += dy bgx -= 3 if bgx < -640: bgx = 0 # draw bg image screen.blit(bg, (bgx,0)) screen.blit(bg, (bgx + SCREEN_SIZE[0],0)) screen.blit(image, (x, y)) # draw image screen.blit(textsurface, (0, 0)) # draw text image # pygame.display.update() pygame.display.flip() clock.tick(60) # 60fps cnt += 1 if cnt % 180 == 0: print str(clock.get_fps()) + " FPS" if __name__ == '__main__': main()