$ wget http://labfile.oss.aliyuncs.com/courses/940/foundation.zip
$ unzip foundation.zip
$ sudo pip install pygame
import pygame
# -*- coding: UTF-8 -*-
# helloworld.py
# 导入所需的模块
import pygame, sys
# 导入所有pygame.locals里的变量(比如下面大写的QUIT变量)
from pygame.locals import *
# 初始化pygame
pygame.init()
# 设置窗口的大小,单位为像素
screen = pygame.display.set_mode((500, 400))
# 设置窗口标题
pygame.display.set_caption('Hello World')
# 程序主循环
while True:
# 获取事件
for event in pygame.event.get():
# 判断事件是否为退出事件
if event.type == QUIT:
# 退出pygame
pygame.quit()
# 退出系统
sys.exit()
# 绘制屏幕内容
pygame.display.update()
# -*- coding: UTF-8 -*-
# drawing.py
# 导入需要的模块
import pygame, sys
from pygame.locals import *
from math import pi
# 初始化pygame
pygame.init()
# 设置窗口的大小,单位为像素
screen = pygame.display.set_mode((400,300))
# 设置窗口标题
pygame.display.set_caption('Drawing')
# 定义颜色
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
# 设置背景颜色
screen.fill(WHITE)
# 绘制一条线
pygame.draw.line(screen, GREEN, [0, 0], [50,30], 5)
# 绘制一条抗锯齿的线
pygame.draw.aaline(screen, GREEN, [0, 50],[50, 80],True)
# 绘制一条折线
pygame.draw.lines(screen, BLACK, False,
[[0, 80], [50, 90], [200, 80], [220, 30]], 5)
# 绘制一个空心矩形
pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2)
# 绘制一个矩形
pygame.draw.rect(screen, BLACK, [150, 10, 50, 20])
# 绘制一个空心椭圆
pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2)
# 绘制一个椭圆
pygame.draw.ellipse(screen, RED, [300, 10, 50, 20])
# 绘制多边形
pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5)
# 绘制多条弧线
pygame.draw.arc(screen, BLACK,[210, 75, 150, 125], 0, pi/2, 2)
pygame.draw.arc(screen, GREEN,[210, 75, 150, 125], pi/2, pi, 2)
pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi,3*pi/2, 2)
pygame.draw.arc(screen, RED, [210, 75, 150, 125], 3*pi/2, 2*pi, 2)
# 绘制一个圆
pygame.draw.circle(screen, BLUE, [60, 250], 40)
# 程序主循环
while True:
# 获取事件
for event in pygame.event.get():
# 判断事件是否为退出事件
if event.type == QUIT:
# 退出pygame
pygame.quit()
# 退出系统
sys.exit()
# 绘制屏幕内容
pygame.display.update()
# -*- coding: UTF-8 -*-
# animation.py
# 导入需要的模块
import pygame, sys
from pygame.locals import *
# 初始化pygame
pygame.init()
# 设置帧率(屏幕每秒刷新的次数)
FPS = 30
# 获得pygame的时钟
fpsClock = pygame.time.Clock()
# 设置窗口大小
screen = pygame.display.set_mode((500, 400), 0, 32)
# 设置标题
pygame.display.set_caption('Animation')
# 定义颜色
WHITE = (255, 255, 255)
# 加载一张图片(所用到的的图片请参考1.5代码获取)
img = pygame.image.load('resources/shiyanlou.PNG')
# 初始化图片的位置
imgx = 10
imgy = 10
# 初始化图片的移动方向
direction = 'right'
# 程序主循环
while True:
# 每次都要重新绘制背景白色
screen.fill(WHITE)
# 判断移动的方向,并对相应的坐标做加减
if direction == 'right':
imgx += 5
if imgx == 380:
direction = 'down'
elif direction == 'down':
imgy += 5
if imgy == 300:
direction = 'left'
elif direction == 'left':
imgx -= 5
if imgx == 10:
direction = 'up'
elif direction == 'up':
imgy -= 5
if imgy == 10:
direction = 'right'
# 该方法将用于图片绘制到相应的坐标中
screen.blit(img, (imgx, imgy))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# 刷新屏幕
pygame.display.update()
# 设置pygame时钟的间隔时间
fpsClock.tick(FPS)
# -*- coding: UTF-8 -*-
# font.py
# 导入需要的模块
import pygame, sys
from pygame.locals import *
# 初始化pygame
pygame.init()
# 设置窗口的大小,单位为像素
screen = pygame.display.set_mode((500,400))
# 设置窗口的标题
pygame.display.set_caption('Font')
# 定义颜色
WHITE = (255, 255, 255)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 128)
# 通过字体文件获得字体对象
fontObj = pygame.font.Font('resources/ARBERKLEY.ttf', 50)
# 配置要显示的文字
textSurfaceObj = fontObj.render('Pygame', True, BLUE, GREEN)
# 获得要显示的对象的rect
textRectObj = textSurfaceObj.get_rect()
# 设置显示对象的坐标
textRectObj.center = (250, 200)
# 设置背景
screen.fill(WHITE)
# 绘制字体
screen.blit(textSurfaceObj, textRectObj)
# 程序主循环
while True:
# 获取事件
for event in pygame.event.get():
# 判断事件是否为退出事件
if event.type == QUIT:
# 退出pygame
pygame.quit()
# 退出系统
sys.exit()
# 绘制屏幕内容
pygame.display.update()
# -*- coding: UTF-8 -*-
# audio.py
# 导入需要的模块
import pygame, sys
from pygame.locals import *
# 初始化pygame
pygame.init()
# 设置窗口的大小,单位为像素
screen = pygame.display.set_mode((500,400))
# 设置窗口的标题
pygame.display.set_caption('Audio')
# 定义颜色
WHITE = (255, 255, 255)
# 设置背景
screen.fill(WHITE)
# 加载并播放一个特效音频文件(所用到的音频文件请参考1.5代码获取)
sound = pygame.mixer.Sound('resources/bounce.ogg')
sound.play()
# 加载背景音乐文件
pygame.mixer.music.load('resources/bgmusic.mp3')
# 播放背景音乐,第一个参数为播放的次数(-1表示无限循环),第二个参数是设置播放的起点(单位为秒)
pygame.mixer.music.play(-1, 0.0)
# 程序主循环
while True:
# 获取事件
for event in pygame.event.get():
# 判断事件是否为退出事件
if event.type == QUIT:
# 停止播放背景音乐
pygame.mixer.music.stop()
# 退出pygame
pygame.quit()
# 退出系统
sys.exit()
# 绘制屏幕内容
pygame.display.update()
| 事件 | 产生途径 | 参数 |
|---|---|---|
| QUIT | 用户按下关闭按钮 | none |
| ACTIVEEVENT | Pygame被激活或者隐藏 | gain, state |
| KEYDOWN | 键盘被按下 | unicode, key, mod |
| KEYUP | 键盘被放开 | key, mod |
| MOUSEMOTION | 鼠标移动 | pos, rel, buttons |
| MOUSEBUTTONDOWN | 鼠标按下 | pos, button |
| MOUSEBUTTONUP | 鼠标放开 | pos, button |
| VIDEORESIZE | Pygame窗口缩放 | size, w, h |
# -*- coding: UTF-8 -*-
# event.py
# 导入需要的模块
import pygame, sys
from pygame.locals import *
# 定义颜色
WHITE = (255, 255, 255)
# 初始化pygame
pygame.init()
# 设置窗口的大小,单位为像素
screen = pygame.display.set_mode((500,400), 0, 32)
# 设置窗口的标题
pygame.display.set_caption('Event')
# 设置背景
screen.fill(WHITE)
# 程序主循环
while True:
# 获取事件
for event in pygame.event.get():
# 判断事件是否为退出事件
if event.type == QUIT:
# 退出pygame
pygame.quit()
# 退出系统
sys.exit()
# 获得鼠标当前的位置
if event.type ==MOUSEMOTION:
print(event.pos)
# 获得鼠标按下的位置
if event.type ==MOUSEBUTTONDOWN:
print("鼠标按下:",event.pos)
# 获得鼠标抬起的位置
if event.type ==MOUSEBUTTONUP:
print("鼠标抬起:",event.pos)
# 获得键盘按下的事件
if event.type == KEYDOWN:
if(event.key==K_UP or event.key==K_w):
print("上")
if(event.key==K_DOWN or event.key==K_s):
print("下")
if(event.key==K_LEFT or event.key==K_a):
print("左")
if(event.key==K_RIGHT or event.key==K_d):
print("右")
# 按下键盘的Esc键退出
if(event.key==K_ESCAPE):
# 退出pygame
pygame.quit()
# 退出系统
sys.exit()
# 绘制屏幕内容
pygame.display.update()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有