Python/遊戲編程
外觀
< Python
簡介
[編輯]Pygame是一群Python語言的模組(module),顧名思義便是讓Python方便製作遊戲。等一下我們會用一個簡單的Pygame程式表現遊戲最基本的流程。
安裝
[編輯]Pygame的版本要視Python的版本而定。我們可以到Pygame的網站 下載符合你的Python版本的Pygame。安裝過程十分容易,直接安裝即可
範例
[編輯]以下是一個小程式,Hello world
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
import sys
SCREEN_SIZE = (640, 480)
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Hello, world!")
# 文字格式
font = pygame.font.SysFont(None, 80)
# 列出文字(第1引数列出文字、第2引数寫True文字看的比較順、第3引数彩色、第4引数背景顔色)
hello1 = font.render("Hello, world!", False, (0,0,0))
hello2 = font.render("Hello, world!", True, (0,0,0))
hello3 = font.render("Hello, world!", True, (255,0,0), (255,255,0))
while True:
screen.fill((0,0,255))
# 顕示在印幕上
screen.blit(hello1, (20,50))
screen.blit(hello2, (20,150))
screen.blit(hello3, (20,250))
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()