pygame一步步实现可视化的幸运大转盘(有趣、有料、好玩、全流程实现)

继续分享pygame有趣的技术知识,欢迎往下看。

一、先搭个架子

(一)黏贴背景图:

在这里插入图片描述

实现代码如下:

import pygame

pygame.init()  # 初始化pygame类
screen = pygame.display.set_mode((600, 600))  # 设置窗口大小
pygame.display.set_caption('幸运大转盘')  # 设置窗口标题
tick = pygame.time.Clock()
fps = 10  # 设置刷新率,数字越大刷新率越高
# 方法一
# bg = pygame.image.load("./幸运大转盘.png").convert()
# 方法二
picture = pygame.transform.scale(pygame.image.load("./幸运大转盘.png"), (600, 600))
bg=picture.convert()

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    # screen.fill((255, 255, 0))  # 设置背景为白色
    screen.blit(bg, (0, 0))
    tick.tick(fps)
    pygame.display.flip()  # 刷新窗口

(二)增加中间的圈圈

核心代码:

pygame.draw.circle(screen,(255,255,0),(300,300),50)

代码解释:

绘制圆形方法:pygame.draw.circle(Surface, color, pos , raduis, width)

其中:
Surfuce参数: 传入需要在该Surface对象上绘制圆形的Surface对象
color参数: 需要绘制圆形的线的颜色,传入一个rgb三原色元组
pos参数:圆心的坐标
raduis 表示圆的半径
width参数:表示绘制圆的线的宽度,当为0时,圆内全部被填充
效果图:
在这里插入图片描述
相关代码如下:

import pygame

pygame.init()  # 初始化pygame类
screen = pygame.display.set_mode((600, 600))  # 设置窗口大小
pygame.display.set_caption('幸运大转盘')  # 设置窗口标题
tick = pygame.time.Clock()
fps = 10  # 设置刷新率,数字越大刷新率越高
# 方法一
# bg = pygame.image.load("./幸运大转盘.png").convert()
# 方法二
picture = pygame.transform.scale(pygame.image.load("./幸运大转盘.png"), (600, 600))
bg=picture.convert()

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    # screen.fill((255, 255, 0))  # 设置背景为白色
    screen.blit(bg, (0, 0))
    pygame.draw.circle(screen,(255,255,0),(300,300),50)
    tick.tick(fps)
    pygame.display.flip()  # 刷新窗口

(三)让大转盘自己动起来

核心代码:

    newbg = pygame.transform.rotate(bg, angle)
    newRect = newbg.get_rect(center=(300,300))
    angle -= 1
    screen.blit(newbg, newRect)

运行效果:
在这里插入图片描述
相关代码如下:

import pygame

pygame.init()  # 初始化pygame类
screen = pygame.display.set_mode((600, 600))  # 设置窗口大小
pygame.display.set_caption('幸运大转盘')  # 设置窗口标题
tick = pygame.time.Clock()
fps = 10  # 设置刷新率,数字越大刷新率越高
# 方法一
# bg = pygame.image.load("./幸运大转盘.png").convert()
# 方法二
picture = pygame.transform.scale(pygame.image.load("./幸运大转盘.png"), (600, 600))
bg=picture.convert()

angle = 0
while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    # screen.fill((255, 255, 0))  # 设置背景为白色
    newbg = pygame.transform.rotate(bg, angle)
    newRect = newbg.get_rect(center=(300,300))
    angle -= 1
    screen.blit(newbg, newRect)

    # screen.blit(bg, (0, 0))
    pygame.draw.circle(screen,(255,255,0),(300,300),50)
    tick.tick(fps)
    pygame.display.flip()  # 刷新窗口

二、再加个指针,幸运的小指针

(一)小指针不动,转盘动

运行效果:
在这里插入图片描述

相关代码如下:

import pygame

pygame.init()  # 初始化pygame类
screen = pygame.display.set_mode((600, 600))  # 设置窗口大小
pygame.display.set_caption('幸运大转盘')  # 设置窗口标题
tick = pygame.time.Clock()
fps = 10  # 设置刷新率,数字越大刷新率越高
picture = pygame.transform.scale(pygame.image.load("./幸运大转盘.png"), (600, 600))
bg=picture.convert()
picture = pygame.transform.scale(pygame.image.load("./hand.png"), (30, 230))
hand = picture.convert_alpha()
angle = 0
while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    newbg = pygame.transform.rotate(bg, angle)
    newRect = newbg.get_rect(center=(300,300))
    angle -= 1
    screen.blit(newbg, newRect)

    newRect = hand.get_rect(center=(300,150))
    screen.blit(hand,newRect)
    
    pygame.draw.circle(screen,(255,255,0),(300,300),50)
    tick.tick(fps)
    pygame.display.flip()  # 刷新窗口

(二)转盘不动,小指针动

思路:转盘指针的框的中心点按照圆的轨迹进行移动,然后在移动的过程中同步旋转对应的角度,这样就整个指针一方面移动,一方面转动。实现了自然的按照中心旋转的效果了。

1、先自己画指针矩形框的中心点移动

代码如下:

import pygame,sys
import math

pygame.init()  # 初始化pygame类
screen = pygame.display.set_mode((600, 600))  # 设置窗口大小
pygame.display.set_caption('幸运大转盘')  # 设置窗口标题
tick = pygame.time.Clock()
fps = 100  # 设置刷新率,数字越大刷新率越高
picture = pygame.transform.scale(pygame.image.load("./幸运大转盘.png"), (600, 600))
bg=picture.convert()
picture = pygame.transform.scale(pygame.image.load("./hand.png"), (30, 230))
hand = picture.convert_alpha()
angle = 0
pos_list = []
while True:
    posx = 300+int(150*math.sin(135+angle/360))
    posy = 300+int(150*math.cos(135+angle/360))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    screen.blit(bg,(0,0))
    pos_list.append((posx,posy))
    for pos in pos_list:
        pygame.draw.circle(screen, (0, 0, 0), pos, 1)
    angle -= 2
    pygame.draw.circle(screen,(255,255,0),(300,300),80)
    tick.tick(fps)
    pygame.display.flip()  # 刷新窗口

效果如下:
在这里插入图片描述

2、增加指针转动(不完善的版本)

代码如下:

import pygame,sys
import math

pygame.init()  # 初始化pygame类
screen = pygame.display.set_mode((600, 600))  # 设置窗口大小
pygame.display.set_caption('幸运大转盘')  # 设置窗口标题
tick = pygame.time.Clock()
fps = 100  # 设置刷新率,数字越大刷新率越高
picture = pygame.transform.scale(pygame.image.load("./幸运大转盘.png"), (600, 600))
bg=picture.convert()
picture = pygame.transform.scale(pygame.image.load("./hand.png"), (30, 230))
hand = picture.convert_alpha()
angle = 0
pos_list = []
while True:
    posx = 300+int(150*math.sin(135+angle/360))
    posy = 300+int(150*math.cos(135+angle/360))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    screen.blit(bg,(0,0))
    pos_list.append((posx,posy))
    for pos in pos_list:
        pygame.draw.circle(screen, (0, 0, 0), pos, 1)
    newhand = pygame.transform.rotate(hand, angle/6)
    # old_center = rect.center
    newRect = newhand.get_rect(center=(posx,posy))
    screen.blit(newhand,newRect)
    pygame.draw.rect(screen, (255,0,0), newRect, 1)
    # if angle>-10:
    angle -= 2
    print(angle)
    if angle < -2250:
        angle = 0

    pygame.draw.circle(screen,(255,255,0),(300,300),80)
    tick.tick(fps)
    pygame.display.flip()  # 刷新窗口

效果如下:
在这里插入图片描述

3、增加指针转动(修正版)

发现原来是math类库中的sin和cos函数传递的参数问题。

Math.Sin()里面的是弧度制。 如果是sin(30),就用Math.Sin(Math.PI*30.0/180.0);

因此主要修改的代码如下:

    posx = 300 + int(150 * math.sin(angle * math.pi / 180))
    posy = 300 - int(150 * math.cos(angle * math.pi / 180))

对应的运行效果如下:
在这里插入图片描述

想运行快点的话,就把angle的参数变大就好。
完整代码如下:

import pygame
import math
pygame.init()  # 初始化pygame类
screen = pygame.display.set_mode((600, 600))  # 设置窗口大小
pygame.display.set_caption('幸运大转盘')  # 设置窗口标题
tick = pygame.time.Clock()
fps = 10  # 设置刷新率,数字越大刷新率越高
picture = pygame.transform.scale(pygame.image.load("./幸运大转盘.png"), (600, 600))
bg=picture.convert()
picture = pygame.transform.scale(pygame.image.load("./hand.png"), (30, 230))
hand = picture.convert_alpha()
angle = 0
while True:
    posx = 300 + int(150 * math.sin(angle * math.pi / 180))
    posy = 300 - int(150 * math.cos(angle * math.pi / 180))
    print(posx, posy, math.sin(angle * math.pi / 180))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    screen.blit(bg,(0,0))
    newhand = pygame.transform.rotate(hand, -angle)
    newRect = newhand.get_rect(center=(posx,posy))
    screen.blit(newhand,newRect)
    angle += 10
    pygame.draw.circle(screen,(255,255,0),(300,300),50)
    tick.tick(fps)
    pygame.display.flip()  # 刷新窗口

三、增加随时数算法,实现随机事件

直接借用了这个算法:

轮盘分为三部分: 一等奖, 二等奖和三等奖;
轮盘转的时候是随机的,
如果范围在[0,0.08)之间,代表一等奖,
如果范围在[0.08,0.3)之间,代表2等奖,
如果范围在[0.3, 1.0)之间,代表3等奖,

把该算法封装成函数,相关代码如下:

rewardDict = {
    '一等奖': (0, 0.03),
    '二等奖': (0.03, 0.2),
    '三等奖': (0.2, 1)
}
def rewardFun():
    """用户的得奖等级"""
    # 生成一个0~1之间的随机数
    number = random.random()
    # 判断随机转盘是几等奖
    for k, v in rewardDict.items():
        if v[0] <= number < v[1]:
            return k

四、增加开始函数

def start():
    while True:
        for event in pygame.event.get():

            # 处理退出事件
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_ESCAPE):
                    pygame.quit()
                    sys.exit()
                else:
                    return
        screen.blit(bg,(0,0))
        newRect = hand.get_rect(center=(300,150))
        screen.blit(hand,newRect)

        pygame.draw.circle(screen,(255,255,0),(300,300),50)

        textFont = pygame.font.Font("./font/font.ttf", 80)
        textSurface = textFont.render("go", True, (110, 55, 155))
        screen.blit(textSurface, (270, 230))
        pygame.display.update()

五、增加结束函数

def end(k):
    textFont = pygame.font.Font("./font/font.ttf", 50)
    textSurface = textFont.render("your awards is :%s" % k, True, (110, 55, 155))
    screen.fill((155, 155, 0))
    screen.blit(textSurface, (30, 230))

六、最终完整效果及代码

import pygame,sys
import math
import random

pygame.init()  # 初始化pygame类
screen = pygame.display.set_mode((600, 600))  # 设置窗口大小
pygame.display.set_caption('幸运大转盘')  # 设置窗口标题
tick = pygame.time.Clock()
fps = 10  # 设置刷新率,数字越大刷新率越高
picture = pygame.transform.scale(pygame.image.load("./幸运大转盘.png"), (600, 600))
bg=picture.convert()
picture = pygame.transform.scale(pygame.image.load("./hand.png"), (30, 230))
hand = picture.convert_alpha()

rewardDict = {
    'first level': (0, 0.03),
    'second level': (0.03, 0.2),
    'third level': (0.2, 1)
}
def rewardFun():
    """用户的得奖等级"""
    # 生成一个0~1之间的随机数
    number = random.random()
    # 判断随机转盘是几等奖
    for k, v in rewardDict.items():
        if v[0] <= number < v[1]:
            return k

def start():
    while True:
        for event in pygame.event.get():

            # 处理退出事件
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_ESCAPE):
                    pygame.quit()
                    sys.exit()
                else:
                    return
        screen.blit(bg,(0,0))
        newRect = hand.get_rect(center=(300,150))
        screen.blit(hand,newRect)

        pygame.draw.circle(screen,(255,255,0),(300,300),50)

        textFont = pygame.font.Font("./font/font.ttf", 80)
        textSurface = textFont.render("go", True, (110, 55, 155))
        screen.blit(textSurface, (270, 230))
        pygame.display.update()

def middle():
    angle = 0
    while True:
        posx = 300 + int(150 * math.sin(angle * math.pi / 180))
        posy = 300 - int(150 * math.cos(angle * math.pi / 180))
        print(posx, posy, math.sin(angle * math.pi / 180))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        screen.blit(bg,(0,0))

        newhand = pygame.transform.rotate(hand, -angle)

        newRect = newhand.get_rect(center=(posx,posy))
        screen.blit(newhand,newRect)
        pygame.draw.circle(screen,(255,255,0),(300,300),50)

        angle += 10

        if angle > 500:
            k = rewardFun()
            end(k)

        tick.tick(fps)
        pygame.display.flip()  # 刷新窗口


def end(k):
    textFont = pygame.font.Font("./font/font.ttf", 50)
    textSurface = textFont.render("your awards is :%s" % k, True, (110, 55, 155))
    screen.fill((155, 155, 0))
    screen.blit(textSurface, (30, 230))


if __name__ == '__main__':
    start()
    middle()

运行效果如下:
在这里插入图片描述

七、尾声

总算完整写完了整个案例,发现通过在上班过程中抽空写这些博客,真的很难,码字不易、输出不易,且行且珍惜。
里面有很多细节么有优化、自己都发现很多bug,没有很多的时间去完善和改进,欢迎各位多多提出宝贵意见。

感谢各位的支持,比心!

  • 20
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
要在pygame中使用Matplotlib实现数据可视化,需要按照以下步骤进行操作: 1. 安装Matplotlib库和pygame库: 首先,需要确保已经在Python环境中安装了Matplotlib库和pygame库。可以使用以下命令安装这些库: ``` pip install matplotlib pip install pygame ``` 2. 创建pygame窗口: 使用pygame库创建一个窗口,可以使用以下代码: ``` import pygame # 初始化pygame pygame.init() # 创建窗口 screen = pygame.display.set_mode((640, 480)) # 游戏循环 while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: # 退出游戏 pygame.quit() sys.exit() # 刷新屏幕 pygame.display.update() ``` 这个代码会创建一个640x480的窗口,并启动一个游戏循环,可以用于展示数据可视化效果。 3. 使用Matplotlib绘制图形: 然后,使用Matplotlib库绘制所需的图形。一般来说,可以使用Matplotlib绘制直方图、折线图、散点图等。例如,以下代码可以绘制一个简单的散点图: ``` import numpy as np import matplotlib.pyplot as plt # 生成随机数据 x = np.random.normal(size=100) y = np.random.normal(size=100) # 绘制散点图 plt.scatter(x, y) plt.show() ``` 4. 将Matplotlib图形绘制到pygame窗口中: 最后,在pygame游戏循环中,将Matplotlib绘制的图形转换成一个pygame Surface,并将其显示在pygame窗口中。可以使用以下代码将Matplotlib图形转换成pygame Surface: ``` import pygame import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure # 创建Matplotlib图形 fig = Figure() ax = fig.add_subplot(111) ax.plot([1, 2, 3], [4, 5, 6]) # 将Matplotlib图形转换成pygame Surface canvas = FigureCanvas(fig) canvas.draw() renderer = canvas.get_renderer() raw_data = renderer.tostring_rgb() # 将Surface显示在pygame窗口中 surf = pygame.image.fromstring(raw_data, fig.canvas.get_width_height(), "RGB") screen.blit(surf, (0, 0)) pygame.display.update() ``` 这个代码可以将Matplotlib图形绘制到pygame窗口中,实现数据可视化的效果。 完整的实现代码如下: ``` import pygame import numpy as np import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure # 初始化pygame pygame.init() # 创建窗口 screen = pygame.display.set_mode((640, 480)) # 创建Matplotlib图形 fig = Figure() ax = fig.add_subplot(111) ax.plot([1, 2, 3], [4, 5, 6]) # 游戏循环 while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: # 退出游戏 pygame.quit() sys.exit() # 将Matplotlib图形转换成pygame Surface canvas = FigureCanvas(fig) canvas.draw() renderer = canvas.get_renderer() raw_data = renderer.tostring_rgb() # 将Surface显示在pygame窗口中 surf = pygame.image.fromstring(raw_data, fig.canvas.get_width_height(), "RGB") screen.blit(surf, (0, 0)) pygame.display.update() ``` 这个代码可以在pygame窗口中显示一个由Matplotlib绘制的简单图形。可以根据需要修改Matplotlib代码,以生成不同类型的图形。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值