- 首先引入pygame 和 游戏最常用的 random 方法
- 运行后达到以下结果:
# pygame template - skeleton for a new python project
import pygame
import random
具体pygame 版本可到官网安装:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
- window初始化, 默认为黑色
WIDTH = 600 # 360
HEIGHT = 400 # 480
# FRAME PER SECOND
FPS = 30
# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 255)
BLUE = (0, 0, 255)
# initialize pygame and create wiindow
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
all_sprites = pygame.sprite.Group()
# Game loop
running = True
while running:
# keep loop running at the right speed
clock.tick(FPS)
# process input (event)
for event in pygame.event.get():
# check for closing windows
if event.type == pygame.QUIT:
running = False
# update
all_sprites.update()
# Draw / render
screen.fill(BLACK)
# after drawing everything, flip the display
# TWO SIDE OF SCREEN ONE FOR PROGRAMMER ONE FOR PLAYER
all_sprites.draw(screen)