Lec 1: Introductions, Games, Architecture, Engine⚓︎
Introduction⚓︎
游戏的分类:
- 按平台分类:PC、控制台、浏览器、移动端、VR、AR
- 按目的分类:严肃的、教育意义的、随意的
- 按设备分类:触摸屏、动作捕捉、舞蹈垫
Pygame⚓︎
本课程采用 Pygame 来开发游戏。正如它的字面意思所示,我们会用 Python 语言来编写代码,所以也可以将 Pygame 简单理解为 Python 的一个(开源的)库。Pygame 具备以下特点:
- 建立在 SDL 之上
- 可以进行低级访问,即可以访问帧缓存 (framebuffer)、音频设备、鼠标、键盘和手柄等设备
- 对大多数平台而言是便携的
Pygame 下有多个模块 (modules),用于控制游戏环境的各方面,包括:
Pygame 安装:
例子
#! /usr/bin/env python3
''' A simple pygame intro example '''
import pygame
from pathlib import Path
pygame.init()
size = width, height = 1024, 768
speed = [3,2]
black = (0, 0, 0)
screen = pygame.display.set_mode(size) # screen is a "surface" object
logo = pygame.image.load(logo_file) # so is logo
logo_width, logo_height = logo.get_size() # surface methods let you get the size(width, height)
logo_x = logo_y = 0
running = True
clock = pygame.time.Clock()
while running:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT: # check if the user closed the window
running = False
# move the logo position
logo_x += speed[0]
logo_y += speed[1]
# check if going off screen
if logo_x < 0 or logo_x + logo_width > width:
speed[0] = -speed[0]
if logo_y < 0 or logo_y + logo_height > height:
speed[1] = -speed[1]
screen.fill(black) # draw all black
# now copy/paste in the logo at its (x, y) location
screen.blit(logo, (logo_x, logo_y))
pygame.display.flip()
运行该程序,结果是一个 Pygame 的 Logo 在黑色画布上弹跳,类似 Windows XP 的屏保。
Game Loop⚓︎
Frame⚓︎
Event Queue⚓︎
Game Architecture⚓︎
评论区
如果大家有什么问题或想法,欢迎在下方留言~