【Python】【Pygame】

background.jpg

hello.png

 

turtle.png

 

 

 

 






"""


#例子1

import pygame
import sys

# 例子1 初次见面
size = width, height = 2000, 2000
speed = [-2, 1]
bg = (255, 255, 255)

clock = pygame.time.Clock()

# 创建指定大小的窗口, surface
screen = pygame.display.set_mode(size)
# 设置窗口标题
pygame.display.set_caption('初次见面,请大家多多关照!')

#加载图片
turtle = pygame.image.load('turtle.png')
#获得图像的位置矩形
position = turtle.get_rect()

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

#移动图像
position = position.move(speed)

if position.left < 0 or position.right > width:
#翻转图像
turtle = pygame.transform.flip(turtle, True, False)
#反方向移动
speed[0] = - speed[0]

if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]

#填充背景
screen.fill(bg)
#更新图像
screen.blit(turtle, position)
#更新界面
pygame.display.flip()
#延迟10秒
pygame.time.delay(10)
#设置一秒钟移动多少像素
clock.tick(200)


# 例子2 事件
import pygame
import sys

#初始化 Pygame
pygame.init()

size = width, height = 600, 400
screen = pygame.display.set_mode(size)
pygame.display.set_caption("初次见面,请多多关照!")

f = open('record.txt', 'w')

while True:
for event in pygame.event.get():
f.write(str(event) + '\n')

if event.type == pygame.QUIT:
f.close()
sys.exit()
# 窗口打开后在页面滑动鼠标 操作下键盘啥的事件 然后发现生成一个文件 打开文件 内容就是刚才的事件详细信息


# 例子3 将事件动态显示在窗口
import pygame
import sys

# 初始化pygame
pygame.init()

size = width, height = 2000, 2000
screen = pygame.display.set_mode(size)
pygame.display.set_caption("snow demo")
bg = (0, 0, 0)

font = pygame.font.Font(None, 20)
line_height = font.get_linesize()
position = 0

screen.fill(bg)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# font.render 把字符串对象转化成surface对象
# screen.blit页面加载的函数,参数是surface对象
screen.blit(font.render(str(event), True, (0,255,0)),
(0, position))
position += line_height

if position > height:
position = 0
screen.fill(bg)

pygame.display.flip()


# 例子4 小乌龟:增加点击键盘事件 来手动触发 向左转向右转
import pygame
from pygame import *
import sys

size = width, height = 2000, 2000
speed = [-2, 1]
bg = (255, 255, 255)

clock = pygame.time.Clock()

# 创建指定大小的窗口, surface
screen = pygame.display.set_mode(size)
# 设置窗口标题
pygame.display.set_caption('初次见面,请大家多多关照!')

#加载图片
turtle = pygame.image.load('turtle.png')
#获得图像的位置矩形
position = turtle.get_rect()

# 设置翻转
l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

if event.type == KEYDOWN:
if event.key == K_LEFT:
turtle = l_head
speed = [-1, 0]
if event.key == K_RIGHT:
turtle = r_head
speed = [1, 0]
if event.key == K_UP:
speed = [0, -1]
if event.key == K_DOWN:
speed = [0, 1]


#移动图像
position = position.move(speed)

if position.left < 0 or position.right > width:
#翻转图像
turtle = pygame.transform.flip(turtle, True, False)
#反方向移动
speed[0] = - speed[0]

if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]

#填充背景
screen.fill(bg)
#更新图像
screen.blit(turtle, position)
#更新界面
pygame.display.flip()
#延迟10秒
pygame.time.delay(10)
#设置一秒钟移动多少像素
clock.tick(200)


# 例子5 设置全屏

import pygame
from pygame import *
import sys

size = width, height = 2000, 2000
speed = [-2, 1]
bg = (255, 255, 255)

fullscreen = False

clock = pygame.time.Clock()

# 创建指定大小的窗口, surface
screen = pygame.display.set_mode(size)
# 设置窗口标题
pygame.display.set_caption('初次见面,请大家多多关照!')

#加载图片
turtle = pygame.image.load('turtle.png')
#获得图像的位置矩形
position = turtle.get_rect()

# 设置翻转
l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

if event.type == KEYDOWN:
if event.key == K_LEFT:
turtle = l_head
speed = [-1, 0]
if event.key == K_RIGHT:
turtle = r_head
speed = [1, 0]
if event.key == K_UP:
speed = [0, -1]
if event.key == K_DOWN:
speed = [0, 1]

# 全屏
if event.key == K_9:
fullscreen = not fullscreen
if fullscreen:
# 下面括号里的像素可以通过
# import pygame
# pygame.init()
# print (pygame.display.list_modes())获取

screen = pygame.display.set_mode((1024, 768), FULLSCREEN | HWSURFACE)
else:
screen = pygame.display.set_mode(size)




#移动图像
position = position.move(speed)

if position.left < 0 or position.right > width:
#翻转图像
turtle = pygame.transform.flip(turtle, True, False)
#反方向移动
speed[0] = - speed[0]

if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]

#填充背景
screen.fill(bg)
#更新图像
screen.blit(turtle, position)
#更新界面
pygame.display.flip()
#延迟10秒
pygame.time.delay(10)
#设置一秒钟移动多少像素
clock.tick(200)

# 例子6 改变窗口大小
import pygame
from pygame import *
import sys

size = width, height = 2000, 2000
speed = [-2, 1]
bg = (255, 255, 255)

fullscreen = False

clock = pygame.time.Clock()

# 创建指定大小的窗口, surface RESIZABLE来设置窗口尺寸可修改
screen = pygame.display.set_mode(size, RESIZABLE)
# 设置窗口标题
pygame.display.set_caption('初次见面,请大家多多关照!')

#加载图片
turtle = pygame.image.load('turtle.png')
#获得图像的位置矩形
position = turtle.get_rect()

# 设置翻转
l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

if event.type == KEYDOWN:
if event.key == K_LEFT:
turtle = l_head
speed = [-1, 0]
if event.key == K_RIGHT:
turtle = r_head
speed = [1, 0]
if event.key == K_UP:
speed = [0, -1]
if event.key == K_DOWN:
speed = [0, 1]

# 全屏
if event.key == K_9:
fullscreen = not fullscreen
if fullscreen:
# 下面括号里的像素可以通过
# import pygame
# pygame.init()
# print (pygame.display.list_modes())获取

screen = pygame.display.set_mode((1024, 768), FULLSCREEN | HWSURFACE)
width, height = 1024, 768
else:
screen = pygame.display.set_mode(size)

# 用户调整窗口尺寸
if event.type == VIDEORESIZE:
size = event.size
width, height = size
print (size)
screen = pygame.display.set_mode(size, RESIZABLE)


#移动图像
position = position.move(speed)

if position.left < 0 or position.right > width:
#翻转图像
turtle = pygame.transform.flip(turtle, True, False)
#反方向移动
speed[0] = - speed[0]

if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]

#填充背景
screen.fill(bg)
#更新图像
screen.blit(turtle, position)
#更新界面
pygame.display.flip()
#延迟10秒
pygame.time.delay(10)
#设置一秒钟移动多少像素
clock.tick(200)



# 例子7 小乌龟 设置放大缩小图像

import pygame
from pygame import *
import sys

size = width, height = 1024, 768
speed = [-2, 1]
bg = (255, 255, 255)

fullscreen = False

clock = pygame.time.Clock()

# 创建指定大小的窗口, surface RESIZABLE来设置窗口尺寸可修改
screen = pygame.display.set_mode(size, RESIZABLE)
# 设置窗口标题
pygame.display.set_caption('初次见面,请大家多多关照!')

#设置放大缩小的比例
ratio = 1.0

oturtle = pygame.image.load('turtle.png')
turtle = oturtle
oturtle_rect = oturtle.get_rect()
position = turtle_rect = oturtle_rect

# 设置翻转
l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

if event.type == KEYDOWN:
if event.key == K_LEFT:
turtle = l_head
speed = [-1, 0]
if event.key == K_RIGHT:
turtle = r_head
speed = [1, 0]
if event.key == K_UP:
speed = [0, -1]
if event.key == K_DOWN:
speed = [0, 1]

# 全屏
if event.key == K_9:
fullscreen = not fullscreen
if fullscreen:
# 下面括号里的像素可以通过
# import pygame
# pygame.init()
# print (pygame.display.list_modes())获取

screen = pygame.display.set_mode((1024, 768), FULLSCREEN | HWSURFACE)
width, height = 1024, 768
else:
screen = pygame.display.set_mode(size)

# 放大 缩小小乌龟(= -)
if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
# 最大只能放大一杯,缩小50%
if event.key == K_EQUALS and ratio < 2:
ratio += 0.1
if event.key == K_MINUS and ratio > 0.5:
ratio -= 0.1
if event.key == K_SPACE:
ratio = 1.0
turtle = pygame.transform.smoothscale(oturtle,
(int(oturtle_rect.width * ratio),
int(oturtle_rect.height * ratio)))

l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)

# 用户调整窗口尺寸
if event.type == VIDEORESIZE:
size = event.size
width, height = size
print (size)
screen = pygame.display.set_mode(size, RESIZABLE)


#移动图像
position = position.move(speed)

if position.left < 0 or position.right > width:
#翻转图像
turtle = pygame.transform.flip(turtle, True, False)
#反方向移动
speed[0] = - speed[0]

if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]

#填充背景
screen.fill(bg)
#更新图像
screen.blit(turtle, position)
#更新界面
pygame.display.flip()
#延迟10秒
pygame.time.delay(10)
#设置一秒钟移动多少像素
clock.tick(100)



# 例子8 小乌龟 贴边跑

import pygame
from pygame import *
import sys

size = width, height = 1000, 1000
bg = (255, 255, 255)

fullscreen = False

clock = pygame.time.Clock()

# 创建指定大小的窗口, surface RESIZABLE来设置窗口尺寸可修改
screen = pygame.display.set_mode(size, RESIZABLE)
# 设置窗口标题
pygame.display.set_caption('初次见面,请大家多多关照!')

#设置放大缩小的比例
ratio = 1.0

oturtle = pygame.image.load('turtle.png')
turtle = oturtle
oturtle_rect = oturtle.get_rect()
position = turtle_rect = oturtle_rect

# rotate第二个参数是 图片向右旋转多少度
speed = [2, 0] # 第一个参数是x轴偏移 此时是向右走 第二个参数是y轴偏移
turtle_right = pygame.transform.rotate(turtle, 90)
turtle_top = pygame.transform.rotate(turtle, 180)
turtle_left = pygame.transform.rotate(turtle, 270)
turtle_bottom = turtle
turtle = turtle_top

# 设置翻转
l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

if event.type == KEYDOWN:
# 全屏
if event.key == K_9:
fullscreen = not fullscreen
if fullscreen:
# 下面括号里的像素可以通过
# import pygame
# pygame.init()
# print (pygame.display.list_modes())获取

screen = pygame.display.set_mode((1024, 768), FULLSCREEN | HWSURFACE)
width, height = 1024, 768
else:
screen = pygame.display.set_mode(size)

# 放大 缩小小乌龟(= -)
if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
# 最大只能放大一杯,缩小50%
if event.key == K_EQUALS and ratio < 2:
ratio += 0.1
if event.key == K_MINUS and ratio > 0.5:
ratio -= 0.1
if event.key == K_SPACE:
ratio = 1.0
turtle = pygame.transform.smoothscale(oturtle,
(int(oturtle_rect.width * ratio),
int(oturtle_rect.height * ratio)))

l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)

# 用户调整窗口尺寸
if event.type == VIDEORESIZE:
size = event.size
width, height = size
print (size)
screen = pygame.display.set_mode(size, RESIZABLE)


#移动图像
position = position.move(speed)

if position.right > width:
turtle = turtle_right
position = turtle_rect = turtle.get_rect()
position.left = width - turtle_rect.width
speed = [0, 5]

if position.bottom > height:
turtle = turtle_bottom
position = turtle_rect = turtle.get_rect()
position.left = width - turtle_rect.width
position.top = height - turtle_rect.height
speed = [-5, 0]

if position.left < 0:
turtle = turtle_left
position = turtle_rect = turtle.get_rect()
position.top = height - turtle_rect.height
speed = [0, -5]

if position.top < 0:
turtle = turtle_top
position = turtle_rect = turtle.get_rect()
speed = [5, 0]

#填充背景
screen.fill(bg)
#更新图像
screen.blit(turtle, position)
#更新界面
pygame.display.flip()
#延迟10秒
pygame.time.delay(10)
#设置一秒钟移动多少像素
clock.tick(100)



# 例子9 剪裁 拖动鼠标可以剪裁大图上的任意部分到另外的位置 这个case我没执行成功
import pygame
from pygame import *
import sys

pygame.init()

size = width, height = 1000, 1000
bg = (255, 255, 255)

clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('snow demo')

turtle = pygame.image.load('turtle.png')

# 0 -> 未选中 1-> 选择中 2-> 完成选择
select = 0
select_rect = pygame.Rect(0, 0, 0, 0)

# 0 -> 未拖拽 1-> 拖拽中 2-> 完成拖拽
drag = 0

position = turtle.get_rect()
position.center = width // 2, height // 2

while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()

elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
# 第一次点击, 选择范围
if select == 0 and drag == 0:
pos_start = event.pos
select = 1
# 第二次点击,拖拽图像
elif select == 2 and drag == 0:
capture = screen.subsurface(select_rect).copy()
cap_rect = capture.get_rect()
drag = 1
#第三次点击,初始化
elif select == 2 and drag == 2:
select = 0
drag = 0
elif event.type == MOUSEBUTTONUP:
if event.button == 1:
# 第一次释放,结束选择
if select == 1 and drag == 0:
pos_stop = event.pos
select = 2
# 第二次释放,结束拖拽
if select == 2 and drag == 1:
drag = 2

screen.fill(bg)
screen.blit(turtle, position)

#实时绘制选择框:
if select:
mouse_pos = pygame.mouse.get_pos()
if select == 1:
pos_stop = mouse_pos
select_rect.left, select_rect.top = pos_start
select_rect.width, select_rect.height = \
pos_stop[0] - pos_stop[0], pos_stop[1] - pos_stop[1]
# 下面screen代表是画在屏幕上,(0, 0, 0)代表是黑线,第四个参数是边框的宽度
pygame.draw.rect(screen, (0, 0, 0), select_rect, 1)


# 拖拽剪裁的图像
if drag:
if drag == 1:
# 捕获的区域中心坐标随鼠标的移动而移动,即获的区域随鼠标的移动而移动
cap_rect.center = mouse_pos
#画到屏幕上去
screen.blit(capture, cap_rect)

pygame.display.flip()

clock.tick(30)


# 例子10 设置小图片加到背景图片中 并且是透明的, 这个case我没执
import pygame
import sys
from pygame.locals import *

pygame.init()

size = width, height = 1000, 800
bg = (0, 0, 0)

clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('snow demo')

# png文件的小图一般背景都是透明的,jpg则不是,这里convert可以设置背景透明
turtle = pygame.image.load('hello.png').convert()
background = pygame.image.load('background.jpg').convert()
position = turtle.get_rect()
position.center = width // 2, height // 2

# 设置背景是白色
print (turtle.get_at(position.left))

while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()

screen.blit(background, (0, 0))
screen.blit(turtle, position)

pygame.display.flip()

clock.tick(30)


# 例子11 绘制简单图形 长方形
import pygame
import sys
from pygame.locals import *

pygame.init()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

size = width, height = 640, 200
screen = pygame.display.set_mode(size)
pygame.display.set_caption('snow demo')

clock = pygame.time.Clock()

while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()

screen.fill(WHITE)

pygame.draw.rect(screen, BLACK, (50, 50, 150, 50), 0)
pygame.draw.rect(screen, BLACK, (250, 50, 150, 50), 1)
pygame.draw.rect(screen, BLACK, (450, 50, 150, 50), 10)

pygame.display.flip()

#一秒种绘制10次 不这么设置的话 CPU会被跑满
clock.tick(10)



# 例子12 绘制多边形 一条绿色的鱼
import pygame
import sys
from pygame.locals import *

pygame.init()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)

points = [(200,75), (300, 25), (400, 75),(450, 25),
(450, 125),(400, 75), (300, 125) ]

size = width, height = 640, 200
screen = pygame.display.set_mode(size)
pygame.display.set_caption('snow demo')

clock = pygame.time.Clock()

while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()

screen.fill(WHITE)

pygame.draw.polygon(screen, GREEN, points, 0)

pygame.display.flip()

clock.tick(10)


# 例子13 三个同心圆
import pygame
import sys
from pygame.locals import *

pygame.init()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

points = [(200,75), (300, 25), (400, 75),(450, 25),
(450, 125),(400, 75), (300, 125) ]

size = width, height = 640, 200
screen = pygame.display.set_mode(size)
pygame.display.set_caption('snow demo')

position = size[0]//2, size[1]//2

clock = pygame.time.Clock()

while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()


screen.fill(WHITE)

# 第三个参数圆心的位置 第四份参数 半径的大小 最后一个参数是一个像素的宽度
pygame.draw.circle(screen, RED, position, 25, 1)
pygame.draw.circle(screen, GREEN, position, 75, 1)
pygame.draw.circle(screen, BLUE, position, 125, 1)

pygame.display.flip()

clock.tick(10)


# 例子14 鼠标点击图片后可拖动 鼠标放开后图片停止拖动
import pygame
import sys
from pygame.locals import *

pygame.init()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

size = width, height = 640, 480
screen = pygame.display.set_mode(size)
pygame.display.set_caption('snow demo')

position = size[0]//2, size[1]//2
moving = False

clock = pygame.time.Clock()

while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()

if event.type == MOUSEBUTTONDOWN:
# event.button == 1 是鼠标左键点击
if event.button == 1:
moving = True
if event.type == MOUSEBUTTONUP:
if event.button == 1:
moving = False
if moving:
position = pygame.mouse.get_pos()

screen.fill(WHITE)

# 第三个参数圆心的位置 第四份参数 半径的大小 最后一个参数是一个像素的宽度
pygame.draw.circle(screen, RED, position, 25, 1)
pygame.draw.circle(screen, GREEN, position, 75, 1)
pygame.draw.circle(screen, BLUE, position, 125, 1)

pygame.display.flip()

# 如果此处设置clock.tick(10)即一秒10次,拖动效果会有点卡
clock.tick(120)



# 例子15 椭圆 圆
import pygame
import sys
from pygame.locals import *

pygame.init()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

size = width, height = 640, 300
screen = pygame.display.set_mode(size)
pygame.display.set_caption('snow demo')

clock = pygame.time.Clock()

while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()

screen.fill(WHITE)

# 坐标(100,100),宽440, 高100 这是一个椭圆(因为外部的限定框是长方形
pygame.draw.ellipse(screen, BLACK, (100, 100, 440, 100), 1)
# 宽和高长度一致 这是一个圆(因为外部的限定框是正方形)
pygame.draw.ellipse(screen, BLACK, (220, 50, 200, 200), 1)

pygame.display.flip()

# 如果此处设置clock.tick(10)即一秒10次,拖动效果会有点卡
clock.tick(120)


# 例子16 弧线
import pygame
import sys
import math
from pygame.locals import *

pygame.init()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

size = width, height = 640, 300
screen = pygame.display.set_mode(size)
pygame.display.set_caption('snow demo')

clock = pygame.time.Clock()

while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()

screen.fill(WHITE)

# 坐标(100,100),宽440, 高100 这是一个椭圆(因为外部的限定框是长方形
# 从0度画到180度(math.pi)
pygame.draw.arc(screen, BLACK, (100, 100, 440, 100),
0, math.pi, 1)
# 宽和高长度一致 这是一个圆(因为外部的限定框是正方形)
pygame.draw.arc(screen, BLACK, (220, 50, 200, 200),
math.pi, math.pi * 2, 1)

pygame.display.flip()

# 如果此处设置clock.tick(10)即一秒10次,拖动效果会有点卡
clock.tick(120)


# 例子16 line&lines
import pygame
import sys
from pygame.locals import *

pygame.init()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)

points = [(200,75), (300, 25), (400, 75),(450, 25),
(450, 125),(400, 75), (300, 125) ]

size = width, height = 640, 480
screen = pygame.display.set_mode(size)
pygame.display.set_caption('snow demo')

clock = pygame.time.Clock()

while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()

screen.fill(WHITE)

# 第三个参数0代表不闭合 画出来的是一条不闭合的鱼
# 【备注】此时最后一个参数"线的宽度"不能是0,需要是像素1,因为线是不会自动填充的
#pygame.draw.lines(screen, GREEN, 0, points, 1)
#第三个参数1代表闭合 画出来的是一条闭合的鱼
pygame.draw.lines(screen, GREEN, 1, points, 1)
pygame.draw.line(screen, BLACK, (100, 200), (540, 250), 1)
# 抗锯齿,此时最后一个参数是1不代表宽度,代表抗锯齿
# 抗锯齿,pygame画出来的东西,不论是线还是多边行,最小颗粒都是
# 像素,一个像素是由一个正方形块,放大来看边缘部分都是由棱角的
# 即"锯齿"
pygame.draw.aaline(screen, BLACK, (100, 250), (540, 300), 1)
# 下面是不抗锯齿的
pygame.draw.aaline(screen, BLACK, (100, 300), (540, 350), 0)

pygame.display.flip()

clock.tick(10)

"""

































posted @ 2021-03-15 19:15  素人渔芙2017  阅读(150)  评论(0编辑  收藏  举报