python blit_一、python 的 pygame

1.初次见面

– coding: utf-8 –

import pygame

size = width,height = 600,400

speed = [-2,1]

bg = (255,255,255) # RGB

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(150)

2.pygame记录事件到txt文档

– coding: utf-8 –

import pygame

size = width,height = 600,400

创建制定大小的窗口 surface

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.pygame记录事件并显示到屏幕

– coding: utf-8 –

import pygame

初始化pygame

pygame.init()

size = width,height = 600,400

创建制定大小的窗口 surface

screen = pygame.display.set_mode(size)

设置窗口标题

pygame.display.set_caption(“FishC 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:

f.close()

sys.exit()

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.加键盘控制

– coding: utf-8 –

import pygame

import sys

from pygame.locals import *

size = width,height = 600,400

speed = [-2,1]

bg = (255,255,255) # RGB

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 == pygame.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(150)

5.F11全屏

– coding: utf-8 –

import pygame

import sys

from pygame.locals import *

size = width,height = 600,400

speed = [-2,1]

bg = (255,255,255) # RGB

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 == pygame.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]

全屏(F11)

if event.key == K_F11:

fullscreen = not fullscreen

if fullscreen:

screen = pygame.display.set_mode((1024,600),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(150)

6.窗口尺寸可调整

– coding: utf-8 –

import pygame

import sys

from pygame.locals import *

size = width,height = 600,400

speed = [-2,1]

bg = (255,255,255) # RGB

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 == pygame.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]

全屏(F11)

if event.key == K_F11:

fullscreen = not fullscreen

if fullscreen:

screen = pygame.display.set_mode((1024,600),FULLSCREEN | HWSURFACE)

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(150)

7.放大缩小乌龟图像

– coding: utf-8 –

import pygame

import sys

from pygame.locals import *

size = width,height = 600,400

speed = [-2,1]

bg = (255,255,255) # RGB

fullscreen = False

clock = pygame.time.Clock()

创建制定大小的窗口 surface

screen = pygame.display.set_mode(size,)

设置窗口标题

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 == pygame.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]

全屏(F11)

if event.key == K_F11:

fullscreen = not fullscreen

if fullscreen:

screen = pygame.display.set_mode((1024,600),FULLSCREEN | HWSURFACE)

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)))

用户调整窗口尺寸

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(150)

8.小乌龟跑圈

– coding: utf-8 –

import pygame

import sys

from pygame.locals import *

size = width,height = 600,400

bg = (255,255,255) # RGB

fullscreen = False

clock = pygame.time.Clock()

创建制定大小的窗口 surface

screen = pygame.display.set_mode(size,)

设置窗口标题

pygame.display.set_caption(“初次见面,请大家多多关照!”)

turtle = pygame.image.load(“turtle.png”)

turtle_rect = turtle.get_rect()

position = turtle_rect

speed = [-5,0]

turtle_right = pygame.transform.rotate(turtle,90)

turtle_top = pygame.transform.rotate(turtle,180)

turtle_left = pygame.transform.rotate(turtle,270)

turtle_bottom = turtle

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 == pygame.KEYDOWN:

全屏(F11)

if event.key == K_F11:

fullscreen = not fullscreen

if fullscreen:

screen = pygame.display.set_mode((1024,600),FULLSCREEN | HWSURFACE)

else:

screen = pygame.display.set_mode(size)

移动目标

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(150)

9.选定区域拖动复制

import pygame

import sys

from pygame.locals import *

pygame.init()

size = width, height = 800, 600

bg = (255, 255, 255)

clock = pygame.time.Clock()

screen = pygame.display.set_mode(size)

pygame.display.set_caption(“FishC 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_start[0], pos_stop[1] – pos_start[1]

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.图像透明效果

– coding: utf-8 –

import pygame

import sys

from pygame.locals import *

pygame.init()

size = width, height = 640, 480

bg = (0, 0, 0)

clock = pygame.time.Clock()

screen = pygame.display.set_mode(size)

pygame.display.set_caption(“FishC Demo”)

turtle = pygame.image.load(“turtle.png”).convert_alpha()

background = pygame.image.load(“background.jpg”).convert()

position = turtle.get_rect()

position.center = width // 2, height // 2

def blit_alpha(target, source, location, opacity):# 该函数实现的部分区域的背景和图片的重画,以便于能实现图片的透明效果

x = location[0]

y = location[1]

temp = pygame.Surface((source.get_width(), source.get_height())).convert()

temp.blit(target, (-x, -y)) # 大背景图移动相应的位置,这样与大背景一致(纹路对齐)

temp.blit(source, (0, 0))

temp.set_alpha(opacity)

target.blit(temp, location)

while True:

for event in pygame.event.get():

if event.type == QUIT:

sys.exit()

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

blit_alpha(screen, turtle, position, 200)

pygame.display.flip()

clock.tick(30)

11.pygame绘图1

– coding: utf-8 –

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(“FishC 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()

clock.tick(10) # 1秒钟绘制10帧

12.pygame绘图2

– coding: utf-8 –

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(“FishC 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) # 1秒钟绘制10帧

13.pygame绘图3

– coding: utf-8 –

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(“FishC 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:

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(60) # 1秒钟绘制60帧

14.pygame绘图4

– coding: utf-8 –

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(“FishC Demo”)

clock = pygame.time.Clock()

while True:

for event in pygame.event.get():

if event.type == QUIT:

sys.exit()

screen.fill(WHITE)

pygame.draw.ellipse(screen,BLACK,(100,100,440,100),1)

pygame.draw.ellipse(screen,BLUE,(220,50,200,200),0)

pygame.display.flip()

clock.tick(60) # 1秒钟绘制60帧

15.pygame绘图5

– coding: utf-8 –

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, 480

screen = pygame.display.set_mode(size)

pygame.display.set_caption(“FishC Demo”)

clock = pygame.time.Clock()

while True:

for event in pygame.event.get():

if event.type == QUIT:

sys.exit()

screen.fill(WHITE)

pygame.draw.arc(screen,BLACK,(100,100,440,100),0,math.pi,1)

pygame.draw.arc(screen,BLUE,(220,50,200,200),math.pi,math.pi*2,1)

pygame.display.flip()

clock.tick(60) # 1秒钟绘制60帧

16.pygame绘图6

– coding: utf-8 –

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(“FishC Demo”)

clock = pygame.time.Clock()

while True:

for event in pygame.event.get():

if event.type == QUIT:

sys.exit()

screen.fill(WHITE)

pygame.draw.lines(screen, GREEN, 1, points, 1)

pygame.draw.line(screen, BLACK, (100, 200), (540, 250), 1)

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(60) # 1秒钟绘制60帧

Original: https://blog.csdn.net/weixin_32552195/article/details/113638027
Author: 剥茧
Title: python blit_一、python 的 pygame

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/783442/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球