用Python做个小游戏:环境篇

一、安装Python和pygame

1、在Windows环境下,安装Python

2、安装pygame,网址: http://pygame.org

使用Python自带pip工具即可快速安装pygame:

python3 -m pip install -U pygame --user

查看是否安装成功,进入Python命令行,看是否能导入pygame:

>>> import pygame
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> 

能成功导入,说明安装成功了

二、pygame基础

pygame框架包含几个模块,。诸如:绘制图形、处理鼠标输入、播放声音等等,要了解更多关于pygame,见 http://pygame.org/docs

下面我们实现一个简单的用pygame输出标题为helloworld的图形框:

import pygame,sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400,300))
pygame.display.set_caption('Hello World!')
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

实际效果如下:

这个代码主要部分在于那个while循环,我们称之为game loop,一个game loop通常做三件事:

The QUIT Event and pygame.quit() Function

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值