如何修改此代码以向星形添加条纹图案?

huangapple go评论68阅读模式
英文:

How do I modify this code to add a striped pattern to the star?

问题

我有这个创建Pygame中星星的脚本。

import pygame

# 窗口大小
窗口宽度 = 400
窗口高度 = 400

深蓝色 = (3, 5, 54)
星星颜色 = (230, 255, 80)

### 初始化
pygame.init()
窗口 = pygame.display.set_mode((窗口宽度, 窗口高度))
pygame.display.set_caption("星星")

# 定义一个星星
中心坐标 = (窗口宽度 // 2, 窗口高度 // 2)
星星点 = [(165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
          (306, 346), (200, 260), (94, 346), (143, 219), (29, 144)]

# 主循环
时钟 = pygame.time.Clock()
完成 = False
while not 完成:

    # 处理用户输入
    for 事件 in pygame.event.get():
        if 事件.type == pygame.QUIT:
            完成 = True

    # 重新绘制窗口
    窗口.fill(深蓝色)                             # 填充背景
    pygame.draw.polygon(窗口, 星星颜色, 星星点)   # 画星星
    pygame.display.flip()                        # 更新显示

    # 限制FPS
    时钟.tick_busy_loop(60)

pygame.quit()

是否有办法修改这个脚本以在星星内部创建条纹状形状?我已经查找了很多资源,但找不到如何做到这一点的方法。我尝试过在pygame和psychopy中都尝试过这个。如果有更好的方法,我可以重写整个脚本。

英文:

I have this script that creates a star in Pygame.

import pygame

# Window size
WINDOW_WIDTH    = 400
WINDOW_HEIGHT   = 400

DARK_BLUE  = (   3,   5,  54)
STARRY     = ( 230, 255, 80 )


### initialisation
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
pygame.display.set_caption("Star")


# Define a star
centre_coord = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
star_points  = [ (165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
                 (306, 346), (200, 260), (94, 346), (143, 219), (29, 144)   ]

# Main Loop
clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    # Re-draw the window
    window.fill( DARK_BLUE )                             # background fill
    pygame.draw.polygon( window, STARRY, star_points )   # Draw the star
    pygame.display.flip()                                # Update the display

    # Clamp FPS
    clock.tick_busy_loop(60)

pygame.quit()

Is there a way I can modify this to have a striped shape within the star. I've looked and I can't find many resourced to do this. I've tried this in both pygame and psychopy. I'm open to rewriting the entire script if there is a better way to do this.

答案1

得分: 1

创建一个与星星大小相同的条纹图像。您可以根据喜好排列和着色条纹。例如:

stripes = pygame.Surface(window.get_size())
stripes.fill(STARRY)
offset_x = 400
for x in range(-offset_x, 400, 20):
    if (x//20) % 2 == 0:
        pts = [(x, 0), (x+20, 0), (x+20+offset_x, 400), (x+offset_x, 400)]
        pygame.draw.polygon(stripes, "red", pts)

然后,您必须使用纹理来绘制多边形。有两种方法可以做到这一点。


选项1

您可以使用 pygame.gfxdraw.textured_polygon(),它可以绘制带纹理的多边形。导入 pygame.gfxdraw 模块:

import pygame.gfxdraw

最后,使用 pygame.gfxdraw.textured_polygon() 绘制形状:

pygame.gfxdraw.textured_polygon(window, star_points, stripes, 0, 0)

选项2

如果您不想使用 gfxdraw 模块,您可以裁剪图像。从星星点创建与图像大小相同的蒙版:

mask_image = pygame.Surface(stripes.get_size(), pygame.SRCALPHA)
pygame.draw.polygon(mask_image, (255, 255, 255), star_points)
mask = pygame.mask.from_surface(mask_image)

使用条纹图像的蒙版创建一个裁剪的条纹图像。这也在我之前的一个答案中有解释:如何在pygame中自由变换图像?

star_stripes = mask.to_surface(setsurface = stripes.convert_alpha(), unsetcolor = (0, 0, 0, 0))

两种解决方案的最小示例:

如何修改此代码以向星形添加条纹图案?

选项1

import pygame
import pygame.gfxdraw

# 窗口大小
WINDOW_WIDTH    = 400
WINDOW_HEIGHT   = 400

DARK_BLUE  = (   3,   5,  54)
STARRY     = ( 230, 255, 80 )

### 初始化
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
pygame.display.set_caption("Star")

# 定义一个星星
centre_coord = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
star_points  = [ (165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
                 (306, 346), (200, 260), (94, 346), (143, 219), (29, 144)   ]

stripes = pygame.Surface(window.get_size())
stripes.fill(STARRY)
offset_x = 400
for x in range(-offset_x, 400, 20):
    if (x//20) % 2 == 0:
        pts = [(x, 0), (x+20, 0), (x+20+offset_x, 400), (x+offset_x, 400)]
        pygame.draw.polygon(stripes, "red", pts)

# 主循环
clock = pygame.time.Clock()
done = False
while not done:

    # 处理用户输入
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    # 重新绘制窗口
    window.fill( DARK_BLUE )                             # 背景填充
    pygame.gfxdraw.textured_polygon(window, star_points, stripes, 0, 0)
    pygame.display.flip()                                # 更新显示

    # 限制帧率
    clock.tick_busy_loop(60)

pygame.quit()

选项2

import pygame

# 窗口大小
WINDOW_WIDTH    = 400
WINDOW_HEIGHT   = 400

DARK_BLUE  = (   3,   5,  54)
STARRY     = ( 230, 255, 80 )

### 初始化
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
pygame.display.set_caption("Star")

# 定义一个星星
centre_coord = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
star_points  = [ (165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
                 (306, 346), (200, 260), (94, 346), (143, 219), (29, 144)   ]

stripes = pygame.Surface(window.get_size())
stripes.fill(STARRY)
offset_x = 400
for x in range(-offset_x, 400, 20):
    if (x//20) % 2 == 0:
        pts = [(x, 0), (x+20, 0), (x+20+offset_x, 400), (x+offset_x, 400)]
        pygame.draw.polygon(stripes, "red", pts)

mask_image = pygame.Surface(stripes.get_size(), pygame.SRCALPHA)
pygame.draw.polygon(mask_image, (255, 255, 255), star_points)
mask = pygame.mask.from_surface(mask_image)
star_stripes = mask.to_surface(setsurface = stripes.convert_alpha(), unsetcolor = (0, 0, 0, 0))

# 主循环
clock = pygame.time.Clock()
done = False
while not done:

    # 处理用户输入
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    # 重新绘制窗口
    window.fill( DARK_BLUE )                             # 背景填充
    window.blit(star_stripes, (0, 0))
    pygame.display.flip()                                # 更新显示

    # 限制帧率
    clock.tick_busy_loop(60)

pygame.quit()
英文:

Create a striped image of the size of the star. You can arrange and color the strips as you like. For example:

stripes = pygame.Surface(window.get_size())
stripes.fill(STARRY)
offset_x = 400
for x in range(-offset_x, 400, 20):
    if (x//20) % 2 == 0:
        pts = [(x, 0), (x+20, 0), (x+20+offset_x, 400), (x+offset_x, 400)]
        pygame.draw.polygon(stripes, "red", pts)

After that you have to use the texture to draw the polygon. There are 2 ways to do this.


Option 1

You can use pygame.gfxdraw.textured_polygon(), which draws a textured polygon. Import the pygame.gfxdraw module:

import pygame.gfxdraw

Finally draw the shape with pygame.gfxdraw.textured_polygon():

pygame.gfxdraw.textured_polygon(window, star_points, stripes, 0, 0)

Option 2

If you don't want to use the gfxdraw module you have to clip the image. Create a mask from the star points in the size of the image:

mask_image = pygame.Surface(stripes.get_size(), pygame.SRCALPHA)
pygame.draw.polygon(mask_image, (255, 255, 255), star_points)
mask = pygame.mask.from_surface(mask_image)

Create a clipped striped image using the mask of the striped image. This is also explained in on one of my earlier answers: How to free transform image in pygame?.

star_stripes = mask.to_surface(setsurface = stripes.convert_alpha(), unsetcolor = (0, 0, 0, 0))

Minimal examples of both solutions:

如何修改此代码以向星形添加条纹图案?

Option 1

import pygame
import pygame.gfxdraw

# Window size
WINDOW_WIDTH    = 400
WINDOW_HEIGHT   = 400

DARK_BLUE  = (   3,   5,  54)
STARRY     = ( 230, 255, 80 )

### initialisation
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
pygame.display.set_caption("Star")

# Define a star
centre_coord = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
star_points  = [ (165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
                 (306, 346), (200, 260), (94, 346), (143, 219), (29, 144)   ]

stripes = pygame.Surface(window.get_size())
stripes.fill(STARRY)
offset_x = 400
for x in range(-offset_x, 400, 20):
    if (x//20) % 2 == 0:
        pts = [(x, 0), (x+20, 0), (x+20+offset_x, 400), (x+offset_x, 400)]
        pygame.draw.polygon(stripes, "red", pts)

# Main Loop
clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    # Re-draw the window
    window.fill( DARK_BLUE )                             # background fill
    pygame.gfxdraw.textured_polygon(window, star_points, stripes, 0, 0)
    pygame.display.flip()                                # Update the display

    # Clamp FPS
    clock.tick_busy_loop(60)

pygame.quit()

Option 2

import pygame

# Window size
WINDOW_WIDTH    = 400
WINDOW_HEIGHT   = 400

DARK_BLUE  = (   3,   5,  54)
STARRY     = ( 230, 255, 80 )

### initialisation
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
pygame.display.set_caption("Star")

# Define a star
centre_coord = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
star_points  = [ (165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
                 (306, 346), (200, 260), (94, 346), (143, 219), (29, 144)   ]

stripes = pygame.Surface(window.get_size())
stripes.fill(STARRY)
offset_x = 400
for x in range(-offset_x, 400, 20):
    if (x//20) % 2 == 0:
        pts = [(x, 0), (x+20, 0), (x+20+offset_x, 400), (x+offset_x, 400)]
        pygame.draw.polygon(stripes, "red", pts)

mask_image = pygame.Surface(stripes.get_size(), pygame.SRCALPHA)
pygame.draw.polygon(mask_image, (255, 255, 255), star_points)
mask = pygame.mask.from_surface(mask_image)
star_stripes = mask.to_surface(setsurface = stripes.convert_alpha(), unsetcolor = (0, 0, 0, 0))

# Main Loop
clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    # Re-draw the window
    window.fill( DARK_BLUE )                             # background fill
    window.blit(star_stripes, (0, 0))
    pygame.display.flip()                                # Update the display

    # Clamp FPS
    clock.tick_busy_loop(60)

pygame.quit()

huangapple
  • 本文由 发表于 2023年7月7日 08:08:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76633186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定