【问题标题】:Can pygame recognize simple geometric shapes?pygame 可以识别简单的几何形状吗?
【发布时间】:2016-01-27 07:30:48
【问题描述】:

我想使用 pygame 创建一堆简单的几何形状(彩色矩形、三角形、正方形...),然后分析它们的关系和特征。我首先尝试了turtle,但显然这只是一个图形库,无法跟踪它创建的形状,我想知道 Pygame 是否也是如此。为了说明这一点,假设我有这个脚本:

# Import a library of functions called 'pygame'
import pygame
from math import pi

# Initialize the game engine
pygame.init()

# Define the colors we will use in RGB format
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
BLUE =  (  0,   0, 255)
GREEN = (  0, 255,   0)
RED =   (255,   0,   0)

# Set the height and width of the screen
size = [800, 600]
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Example code for the draw module")

#Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()

while not done:

    # This limits the while loop to a max of 10 times per second.
    # Leave this out and we will use all CPU we can.
    clock.tick(10)

    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop

    screen.fill(WHITE)
    # Draw a rectangle outline
    pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2)
    # Draw a solid rectangle
    pygame.draw.rect(screen, BLACK, [150, 10, 50, 20])
    # Draw an ellipse outline, using a rectangle as the outside boundaries
    pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2) 
    # Draw a circle
    pygame.draw.circle(screen, BLUE, [60, 250], 40)
    # Go ahead and update the screen with what we've drawn.
    # This MUST happen after all the other drawing commands.
    pygame.display.flip()

# Be IDLE friendly
pygame.quit()

它创建了这个图像:

现在,假设我保存了 Pygame 创建的图像。 Pygame 有没有办法从图像中检测形状、颜色和坐标?

【问题讨论】:

    标签: python image image-processing pygame


    【解决方案1】:

    PyGame 是一个游戏库 - 它有助于为游戏制作图形和音频以及控制器。它不支持检测预先存在的图像中的对象。

    你想要的是 OpenCV(它有 Python 绑定)——这是为了“理解”关于图像的事情。

    一种用于检测任何形状(或边缘)的流行数学算法是霍夫变换。你可以在这里阅读更多关于它的信息 - http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

    OpenCV 内部有非常有用的霍夫变换函数。


    您可以尝试制作自己的 Hough 变换代码并使用它……但库使这更容易。

    【讨论】:

      【解决方案2】:

      是的,可以,但是 pygame 也适合制作游戏,但不幸的是,您无法将它们转换为 IOS 或 Android,过去有一个名为 PGS4A 的程序可以让您将 pygame 项目转换为 android 但是可悲的是,该计划已停止,现在没有办法了。在这种情况下,我的建议是,如果您想这样做,请从此链接“http://developer.android.com/sdk/index.html#top”下载 Android Studio,并在 Google 上了解如何将 libgdx 与 Android Studio 一起使用,这个人有一个非常有用的教程,其中有很多部分,但如果您的目标是制作商业应用程序,我强烈建议您查看本教程“https://www.youtube.com/watch?v=_pwJv1QRSPM”非常有帮助。祝你的目标好运,希望这有助于你做出决定,但是 python 是一种很好的编程语言,它会给你关于编程的基本概念。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-29
        • 1970-01-01
        • 1970-01-01
        • 2011-08-06
        • 1970-01-01
        • 2011-04-17
        • 2012-07-25
        • 1970-01-01
        相关资源
        最近更新 更多