屏幕绘制的函数:
屏幕尺寸和模式:
pygame.display.set_mode()设置相关屏幕模式
pygame.display.Info()生成屏幕相关信息
窗口标题和图标:
pygame.display.set_caption() 设置标题信息
pygame.display.set_icon()设置图标信息
pygame.display.get_caption()获得图标
窗口感知和刷新:
pygame.display.get_active()
pygame.display.flip()全部窗口刷新
pygame.display.update()仅刷新变化的区域
pygame.display.set_mode(r=(0,0),flags=0)
r分辨率(width,height)
flags:
pygame.RESIZABLE 窗口大小可调
pygame.NOFRAME 窗口没有边界显示
pygame.FULLSCREEN 窗口全屏显示
pygame.display.Info()
显示当前屏幕信息,在set_mode前使用
current_w:当前显示模式或窗口的像素宽度
current_h:当前显示模式或窗口的像素高度
pygame.display.set_caption(title,icontitle=None)
title设置窗口的标题内容
icontitle设置图标化的小标题
pygame.display.get_caption()
获得信息为一个元组(title, icontitle)
pygame.display.set_icon(surface)
设置窗口的图标效果
pygame.display.get_active()
窗口在系统中显示返回True,最小化时返回False
键盘事件:
键盘按下:pygame.event.KEYDOWN
返回值:
event.unicode 案件的unicode码,不推荐
event.key 按键常量名称
event.mod 按键修饰符的组合
键盘释放:pygame.event.KEYUP
返回值:
event.key 按键常量名称
event.mod 按键修饰符的组合 |符号
鼠标事件:
pygame.event.MOUSEMOTION鼠标移动事件
event.pos 鼠标当前坐标值(x,y)
event.rel 鼠标相对运动距离(x,y),相对于上次事件
event.buttons 鼠标按钮状态(a,b,c),对应鼠标三个键,按下为1
pygame.event.MOUSEBUTTONUP鼠标释放事件
event.pos 鼠标当前坐标值
event.button 鼠标按下键编号n,取值0/1/2,分别对应三个键
pygame.event.MOUSEBUTTONDOWN鼠标释放事件
event.pos 鼠标当前坐标值
event.button 鼠标按下键编号n,左键1,右键3
事件处理函数:
处理函数:
pygame.event.get()获得事件列表
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.event.poll()从事件队列中获得一个事件:
while True:
event = pygame.evwnt.poll()
时间获取将从事件队列中删除,如果队列为空则返回event.NOEVENT
pygame.event.clear()从事件队列中删除事件,默认删除所有事件
操作事件队列:
pygame.event.set_blocked(type or typelist)
控制哪些事件不允许被保存在事件队列中
pygame.event.set_allowed(type or typelist)
控制哪些事件允许被保存在事件队列中
pygame.event.get_blocked(type)
测试某个事件类型是否被事件队列所禁止,返回True,False
事件操作:
pygame.event.post(Event)
产生一个事件,并将其放入事件队列
一般用于防止用户自定义事件(pygame.USEREVENT)
也可以用于放置系统定义事件(键鼠)
pygame.event.Event(type,dict)
创建一个给定类型的事件
事件的属性和值采用字典类型复制属性名采用字符串形式
色彩表示:
pygame.Color.r
pygame.Color.g
pygame.Color.b
pygame.Color.a alpha值
pygame.Color.normalize 将RGBA各通道值归一到0-1之间
图形绘制:
pygame.draw.rect(Surface,color,Rect,width=0)矩形
Surface矩形绘制屏幕
Color 矩形绘制颜色
Rect 矩形的绘制区域
width=0 绘制边缘的宽度,默认为0
pygame.draw.polygon(Surface,color,pointlist,width=0)多边形
Surface多边形形绘制屏幕
Color 多边形形绘制颜色
pointlist 多边形顶点坐标列表
width=0 绘制边缘的宽度,默认为0
pygame.draw.circle(Surface,color,pos,radius,width=0)圆形
Surface圆形绘制屏幕
Color 圆形绘制颜色
pos 圆形圆心坐标
radius 圆形半径
width=0 绘制边缘的宽度,默认为0
pygame.draw.ellipse(Surface,color,Rect,width=0)椭圆形
Surface椭圆形形绘制屏幕
Color 椭圆形绘制颜色
Rect 椭圆形的绘制区域
width=0 绘制边缘的宽度,默认为0
pygame.draw.arc(Surface,color,Rect,start_angle,stop_angle,width=0)圆弧
Surface椭圆形形绘制屏幕
Color 椭圆形绘制颜色
Rect 椭圆形的绘制区域
start_angle,stop_angle:弧形绘制其实和结束弧度制
width=0 绘制边缘的宽度,默认为0
pygame.draw.line(Surface,color,start_pos,end_pos,width=1)直线
Surface直线绘制屏幕
Color 直线绘制颜色
start_pos,end_pos直线起始和结束坐标
width=1直线宽度
pygame.draw.lines(Surface,color,closed,pointlist,width=1)多直线
Surface多直线绘制屏幕
Color 多直线绘制颜色
closed 如果为True起止节点间自动增加封闭直线
pointlist 顶点坐标列表
width=1直线宽度
pygame.draw.aaline(Surface,color,start_pos,end_pos,blend=1)无锯齿直线
Surface无锯齿线绘制屏幕
Color 无锯齿线绘制颜色
start_pos,end_pos无锯齿线起始和结束坐标
blend=1不为0时,与线条所在背景颜色混合
pygame.draw.lines(Surface,color,closed,pointlist,width=1)多无锯齿直线
Surface多直线绘制屏幕
Color 多直线绘制颜色
closed 如果为True起止节点间自动增加封闭直线
pointlist 顶点坐标列表
blend=1不为0时,与线条所在背景颜色混合
文字绘制:
Font.render_to(surf,dest,text,fgcolor=None,bgcolor=None,rotation=0,size=0) →rect
surf 绘制字体的平面
dest 平面中位值,(x,y)
test 绘制文字内容
fgcolor 文字颜色
bgcolor背景颜色
rotation 逆时针旋转角度,取值0-359
size文字大小
Font.render(text,fgcolor=None,bgcolor=None,rotation=0,size=0)→(surface,rect)
screen.blit(f1surf, (200,160))
# pygame最小框架
# Pygame Hello World Game
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Python游戏之旅")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()
'''框架为:
引入pygame和sys
初始化init()及设置
获取事件并逐类响应
刷新屏幕
'''
# 案件信息检测
# Pygame Hello World Game
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Python事件处理")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.unicode == "":
print("[KETDOWN]:", "#", event.key, event.mod)
else:
print("[KETDOWN]:", event.unicode, event.key, event.mod)
elif event.type == pygame.MOUSEMOTION:
print("[MOUSEMOTION]:", event.pos, event.rel, event.buttons)
elif event.type == pygame.MOUSEBUTTONUP:
print("[MOUSEBUTTONUP]:", event.pos, event.button)
elif event.type == pygame.MOUSEBUTTONDOWN:
print("[MOUSEBUTTONDOWN]:", event.pos, event.button)
pygame.display.update()
# 事件处理
# Unit PYG04: Pygame Event Post
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame事件处理")
fps = 1
fclock = pygame.time.Clock()
num = 1
while True:
uevent = pygame.event.Event(pygame.KEYDOWN, {
"unicode": 123, "key": pygame.K_SPACE, "mod": pygame.KMOD_ALT})
pygame.event.post(uevent)
num = num + 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.unicode == "":
print("[KEYDOWN {}]:".format(num), "#", event.key, event.mod)
else:
print("[KEYDOWN {}]:".format(num), event.unicode, event.key, event.mod)
pygame.display.update()
fclock.tick(fps)
# 图形绘制
# Unit PYG05: Pygame Shape Draw Test
from math import pi
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame图形绘制")
GOLD = 255, 251, 0
RED = pygame.Color('red')
WHITE = 255, 255, 255
GREEN = pygame.Color('green')
# r1rect = pygame.draw.rect(screen, GOLD, (100,100,200,100), 5)
# r2rect = pygame.draw.rect(screen, RED, (210,210,200,100), 0)
e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 300), 3)
c1rect = pygame.draw.circle(screen, GOLD, (200, 180), 30, 5)
c2rect = pygame.draw.circle(screen, GOLD, (400, 180), 30)
r1rect = pygame.draw.rect(screen, RED, (170, 130, 60, 10), 3)
r2rect = pygame.draw.rect(screen, RED, (370, 130, 60, 10))
plist = [(295, 170), (285, 250), (260, 280), (340, 280), (315, 250), (305, 170)]
l1rect = pygame.draw.lines(screen, GOLD, True, plist, 2)
a1rect = pygame.draw.arc(screen, RED, (200, 220, 200, 100), 1.4 * pi, 1.9 * pi, 3)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()
# 文字绘制1
# Unit PYG05: Pygame Font Draw
import pygame, sys
import pygame.freetype
import os
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame文字绘制")
GOLD = 255, 251, 0
f1 = pygame.freetype.Font("C://Windows//Fonts//msyh.ttc", 36)
f1rect = f1.render_to(screen, (200, 160), "世界和平", fgcolor=GOLD, size=50)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()
# 文字绘制2
# Unit PYG05: Pygame Font Draw
import pygame, sys
import pygame.freetype
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame文字绘制")
GOLD = 255, 251, 0
f1 = pygame.freetype.Font("C://Windows//Fonts//msyh.ttc", 36)
f1surf, f1rect = f1.render("世界和平", fgcolor=GOLD, size=50)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(f1surf, (200, 160))
pygame.display.update()
# 小球碰壁1原始
import pygame
import sys
import os
#初始化
pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load(("ball.gif"))
ballrect = ball.get_rect()# 返回一个覆盖图像的矩形Rect对象
#事件处理
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
ballrect = ballrect.move(speed[0],speed[1])
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom>height:
speed[1]=-speed[1]
#窗口刷新
screen.fill(BLACK)
screen.blit(ball,ballrect)
pygame.display.update()
# 小球碰壁2速度帧率调节
import pygame
import sys
import os
#初始化
pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
fps = 144#设置帧率
fclock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load(os.path.join(r'C:\Users\zhj72\iCloudDrive\files\program\Python\pygame', "ball.gif"))
ballrect = ball.get_rect()# 返回一个覆盖图像的矩形Rect对象
#事件处理
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
ballrect = ballrect.move(speed[0],speed[1])
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
#窗口刷新
screen.fill(BLACK)
screen.blit(ball,ballrect)
pygame.display.update()
fclock.tick(fps)
# 小球碰壁3键盘可控制速度
import pygame
import sys
import os
#初始化
pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
fps = 144#设置帧率
fclock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load(os.path.join(r'C:\Users\zhj72\iCloudDrive\files\program\Python\pygame', "ball.gif"))
ballrect = ball.get_rect()# 返回一个覆盖图像的矩形Rect对象
#事件处理
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0]==0 else(abs(speed[0] - 1) * int(speed[0] / abs(speed[0])))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0]+1 if speed[0]>0 else speed[0]-1
elif event.key == pygame.K_UP:
speed[1] = speed[1]+1 if speed[1]>0 else speed[1]-1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1]==0 else(abs(speed[1] - 1) * int(speed[1] / abs(speed[1])))
ballrect = ballrect.move(speed[0],speed[1])
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
#窗口刷新
screen.fill(BLACK)
screen.blit(ball,ballrect)
pygame.display.update()
fclock.tick(fps)
# 小球碰壁4全屏
#Unit PYG03: Pygame Wall Ball Game version 4 全屏型
import pygame,sys
import os
pygame.init()
vInfo = pygame.display.Info() #窗口全屏显示
size = width, height = vInfo.current_w, vInfo.current_h
speed = [1,1]
BLACK = 0, 0, 0
#print(pygame.display.Info())
#screen = pygame.display.set_mode(size, pygame.RESIZABLE)
##窗口大小可调
#screen = pygame.display.set_mode(size, pygame.NOFRAME)
#窗口无边框
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
#窗口全屏显示
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load(os.path.join(r'C:\Users\zhj72\iCloudDrive\files\program\Python\pygame', "ball.gif"))
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = - speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)
# 小球碰壁5伸缩型
#Unit PYG03: Pygame Wall Ball Game version 4 全屏型
import pygame,sys
import os
pygame.init()
size = width, height = 600, 400
speed = [1,1]
BLACK = 0, 0, 0
#print(pygame.display.Info())
#screen = pygame.display.set_mode(size, pygame.RESIZABLE)
#窗口大小可调
#screen = pygame.display.set_mode(size, pygame.NOFRAME)
#窗口无边框
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
#窗口全屏显示
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load(os.path.join(r'C:\Users\zhj72\iCloudDrive\files\program\Python\pygame', "ball.gif"))
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = - speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)
# 小球碰壁6设置图标感知窗口
#Unit PYG03: Pygame Wall Ball Game version 4 全屏型
import os
import pygame
import sys
pygame.init()
size = width, height = 600, 400
speed = [1,1]
BLACK = 0, 0, 0
#print(pygame.display.Info())
#screen = pygame.display.set_mode(size, pygame.RESIZABLE)
#窗口大小可调
#screen = pygame.display.set_mode(size, pygame.NOFRAME)
#窗口无边框
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
#窗口全屏显示
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load(os.path.join("ball.gif"))
pygame.display.set_icon(ball)
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
if pygame.display.get_active():
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = - speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)
# 小球碰壁7鼠标型
# Unit PYG03: Pygame Wall Ball Game version 8 鼠标型
import pygame
import sys
import os
pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.RESIZABLE) # 窗口大小可调
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load(os.path.join(r'C:\Users\zhj72\iCloudDrive\文件\程序\Python\pygame', "ball.gif"))
pygame.display.set_icon(ball)
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
still = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1) * int(speed[0] / abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1) * int(speed[1] / abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
still = True
elif event.type == pygame.MOUSEBUTTONUP:
still = False
if event.button == 1:
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
elif event.type == pygame.MOUSEMOTION:
if event.buttons[0] == 1:
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
if pygame.display.get_active() and not still:
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = - speed[0]
if ballrect.right > width and ballrect.right + speed[0] > ballrect.right:
speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = - speed[1]
if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
speed[1] = - speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)
# 小球碰壁8色彩型
# Unit PYG05: Pygame Wall Ball Game Version 9
import pygame
import sys
import os
pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load(os.path.join(r'C:\Users\zhj72\iCloudDrive\文件\程序\Python\pygame', "ball.gif"))
pygame.display.set_icon(ball)
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
still = False
bgcolor = pygame.Color("black")
def RGBChannel(a):
return 0 if a < 0 else (255 if a > 255 else int(a))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1) * int(speed[0] / abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1) * int(speed[1] / abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
still = True
elif event.type == pygame.MOUSEBUTTONUP:
still = False
if event.button == 1:
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
elif event.type == pygame.MOUSEMOTION:
if event.buttons[0] == 1:
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
if pygame.display.get_active() and not still:
ballrect = ballrect.move(speed[0], speed[1])
if ballrect.left < 0 or ballrect.right > width:
speed[0] = - speed[0]
if ballrect.right > width and ballrect.right + speed[0] > ballrect.right:
speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = - speed[1]
if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
speed[1] = - speed[1]
bgcolor.r = RGBChannel(ballrect.left * 255 / width)
bgcolor.g = RGBChannel(ballrect.top * 255 / height)
bgcolor.b = RGBChannel(min(speed[0], speed[1]) * 255 / max(speed[0], speed[1], 1))
screen.fill(bgcolor)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)
#小球碰壁9文字型1
# Unit PYG05: Pygame Wall Ball Game Version 10
import pygame, sys
import pygame.freetype
pygame.init()
size = width, height = 600, 400
speed = [1, 1]
GOLD = 255, 251, 0
BLACK = 0, 0, 0
pos = [230, 160]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame文字绘制")
f1 = pygame.freetype.Font("C://Windows//Fonts//msyh.ttc", 36)
f1rect = f1.render_to(screen, pos, "世界和平", fgcolor=GOLD, size=50)
fps = 300
fclock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if pos[0] < 0 or pos[0] + f1rect.width > width:
speed[0] = - speed[0]
if pos[1] < 0 or pos[1] + f1rect.height > height:
speed[1] = - speed[1]
pos[0] = pos[0] + speed[0]
pos[1] = pos[1] + speed[1]
screen.fill(BLACK)
f1rect = f1.render_to(screen, pos, "世界和平", fgcolor=GOLD, size=50)
pygame.display.update()
fclock.tick(fps)
#小球碰壁10文字型2
# Unit PYG05: Pygame Wall Ball Game Version 11
import pygame, sys
import pygame.freetype
pygame.init()
size = width, height = 600, 400
speed = [1, 1]
GOLD = 255, 251, 0
BLACK = 0, 0, 0
pos = [230, 160]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame文字绘制")
f1 = pygame.freetype.Font("C://Windows//Fonts//msyh.ttc", 36)
f1surf, f1rect = f1.render("世界和平", fgcolor=GOLD, size=50)
fps = 300
fclock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if pos[0] < 0 or pos[0] + f1rect.width > width:
speed[0] = - speed[0]
if pos[1] < 0 or pos[1] + f1rect.height > height:
speed[1] = - speed[1]
pos[0] = pos[0] + speed[0]
pos[1] = pos[1] + speed[1]
screen.fill(BLACK)
f1surf, f1rect = f1.render("世界和平", fgcolor=GOLD, size=50)
screen.blit(f1surf, (pos[0], pos[1]))
pygame.display.update()
fclock.tick(fps)