Pygame之滑稽球壁碰

发布时间:2023-09-19 16:00

安装pygame:

打开cmd,输入

pip install pygame

首先,我们需要一个小球图片,可以自行寻找,我是通过觅元素(免费下载)找到了免抠素材

滑稽设计元素_滑稽免抠素材_觅元素51yuansu.comicon-default.png?t=M666https://www.51yuansu.com/index.php?m=Index&a=fenlei&k=huaji随意下载后,需要对小球的大小进行修改,我用的是PS,大家有其他方法,用就可以了,

Pygame之滑稽球壁碰_第1张图片

Pygame之滑稽球壁碰_第2张图片

 

 要是嫌麻烦,直接从我这里拿就好了,我最懂大家了。

 首先,我们先来看看全屏版本:

import pygame,sys

pygame.init()
vInfo=pygame.display.Info()
size=width,height=vInfo.current_w,vInfo.current_h

speed=[1,1]
BLACK=0,0,0
screen=pygame.display.set_mode(size,pygame.FULLSCREEN)  #全屏
pygame.display.set_caption("pygame壁球")
ball = pygame.image.load("E:\pycharm\picture\滑稽.gif")
ballrect=ball.get_rect()

fps=300
fclock = pygame.time.Clock()   #创建一个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]))  #else后面的代码解决负号问题
            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[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)  #将一个图像绘制在另一个图像上,即将src绘制到dest位置上。通过Rect对象引导对壁球的绘制
    pygame.display.update()
    fclock.tick(fps)       #控制帧速率,即窗口刷新率,其表示每秒钟300次帧刷新
    

全屏的无框版本,没有右上角的"X",不用急,点击ESC键即可退出,如果用的时候没写入

elif event.key == pygame.K_ESCAPE:
                sys.exit()

点击 Windows 键,将窗口关掉就行,不需要再重启电脑。

其次,我们来看看窗口可调版本:

import pygame,sys

pygame.init()
icon = pygame.image.load("E:\pycharm\picture\滑稽图标.gif")  #设置图标
pygame.display.set_icon(icon)
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("E:\pycharm\picture\滑稽.gif")
ballrect=ball.get_rect()

fps=300
fclock = pygame.time.Clock()   #创建一个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]))  #else后面的代码解决负号问题
            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[0],speed[1])
    if pygame.display.get_active():    #窗口感知
        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)  #将一个图像绘制在另一个图像上,即将src绘制到dest位置上。通过Rect对象引导对壁球的绘制
    pygame.display.update()
    fclock.tick(fps)       #控制帧速率,即窗口刷新率,其表示每秒钟300次帧刷新

Pygame之滑稽球壁碰_第3张图片

然后,我们来看看背景色彩版本:

import pygame,sys

pygame.init()
icon = pygame.image.load("E:\pycharm\picture\滑稽图标.gif")  #设置图标
pygame.display.set_icon(icon)
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("E:\pycharm\picture\滑稽.gif")
ballrect=ball.get_rect()

fps=300
fclock = pygame.time.Clock()   #创建一个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]))  #else后面的代码解决负号问题
            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[0],speed[1])
    if pygame.display.get_active():    #窗口感知
        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]

    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)  #将一个图像绘制在另一个图像上,即将src绘制到dest位置上。通过Rect对象引导对壁球的绘制
    pygame.display.update()
    fclock.tick(fps)       #控制帧速率,即窗口刷新率,其表示每秒钟300次帧刷新

背景板的颜色会随位置的移动发生变化。

之后,我们来看看鼠标型版本:

import pygame,sys

pygame.init()
icon = pygame.image.load("E:\pycharm\picture\滑稽图标.gif")  #设置图标
pygame.display.set_icon(icon)
size=width,height=600,400
speed=[1,1]
BLACK=0,0,0
# screen=pygame.display.set_mode(size)
screen=pygame.display.set_mode(size,pygame.RESIZABLE)  #窗口大小可调

pygame.display.set_caption("pygame壁球")
ball = pygame.image.load("E:\pycharm\picture\滑稽.gif")
ballrect=ball.get_rect()

fps=300
fclock = pygame.time.Clock()   #创建一个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]))  #else后面的代码解决负号问题
            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]
    screen.fill(BLACK)
    screen.blit(ball,ballrect)  #将一个图像绘制在另一个图像上,即将src绘制到dest位置上。通过Rect对象引导对壁球的绘制
    pygame.display.update()
    fclock.tick(fps)       #控制帧速率,即窗口刷新率,其表示每秒钟300次帧刷新

点击鼠标左键,滑稽球停止移动,按住移动鼠标,滑稽球跟着移动,松开左键,滑稽球继续移动。

最后,在来看看文字版本的实现:

import pygame, sys
import pygame.freetype

pygame.init()
icon = pygame.image.load("E:\pycharm\picture\滑稽图标.gif")  # 设置图标
pygame.display.set_icon(icon)
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
GOLD = 255, 251, 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=25)
f1surf, f1rect = f1.render("滑稽果", fgcolor=GOLD, size=25)

fps = 300
fclock = pygame.time.Clock()  # 创建一个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)
    f1surf, f1rect = f1.render("滑稽果", fgcolor=GOLD, size=25)
    screen.blit(f1surf, (pos[0], pos[1]))
    pygame.display.update()
    fclock.tick(fps)  # 控制帧速率,即窗口刷新率,其表示每秒钟300次帧刷新

 如下图所示:

Pygame之滑稽球壁碰_第4张图片

 

附加:滑稽图标

icon = pygame.image.load("E:\pycharm\picture\滑稽图标.gif")  # 设置图标
pygame.display.set_icon(icon)

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号