打地鼠游戏:0.游戏界面
1.基本套路
import pygame import sys pygame.init() screen = pygame.display.set_mode((500,500)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit()
2.设置标题和图标
pygame.display.set_caption(‘打地鼠‘) iconImg = pygame.image.load(‘./images/a.png‘) pygame.display.set_icon(iconImg)
3.加载背景图
bgImg = pygame.image.load(‘./images/背景.png‘) screen.blit(bgImg, (0,0)) pygame.display.update()
4.设置游戏画面帧率
colok = pygame.time.Clock() clock.tick(60)
5.封装draw() / eventListen()函数
def draw(): screen.blit(bgImg, (0, 0)) def eventListen(): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit()
完整代码
import pygame import sys pygame.init() screen = pygame.display.set_mode((500,500)) pygame.display.set_caption(‘打地鼠‘) iconImg = pygame.image.load(‘./images/a.png‘) pygame.display.set_icon(iconImg) #加载背景图片 bgImg = pygame.image.load(‘./images/背景.png‘) def draw(): screen.blit(bgImg, (0, 0)) def eventListen(): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() while True: pygame.time.Clock().tick(60) eventListen() draw() pygame.display.update()