Whac-a-mole fun: 0. Game interface
1. Basic routines
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()Copy the code
2. Set the title and icon
Image. Load ('./images/a.png') PyGame.display. Set_icon (iconImg)Copy the code
3. Load the background image
BgImg = pygame.image.load('./images/ background.png ') screen.blit(bgImg, (0,0)) pygame.display.update()Copy the code
4. Set the game frame rate
colok = pygame.time.Clock()
clock.tick(60)
Copy the code
5. Encapsulate the draw()/eventListen() functions
def draw():
screen.blit(bgImg, (0, 0))
def eventListen():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
Copy the code
The complete code
Import pygame import sys pygame.init() screen = pygame.display.set_mode((500,500)) pygame.display.set_mode IconImg = pygame.image.load('./images/a.png') pygame.display.set_icon(iconImg) # Load background image bgImg = Pygame.image.load ('./images/ back.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()Copy the code