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

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

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

问题

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

  1. import pygame
  2. # 窗口大小
  3. 窗口宽度 = 400
  4. 窗口高度 = 400
  5. 深蓝色 = (3, 5, 54)
  6. 星星颜色 = (230, 255, 80)
  7. ### 初始化
  8. pygame.init()
  9. 窗口 = pygame.display.set_mode((窗口宽度, 窗口高度))
  10. pygame.display.set_caption("星星")
  11. # 定义一个星星
  12. 中心坐标 = (窗口宽度 // 2, 窗口高度 // 2)
  13. 星星点 = [(165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
  14. (306, 346), (200, 260), (94, 346), (143, 219), (29, 144)]
  15. # 主循环
  16. 时钟 = pygame.time.Clock()
  17. 完成 = False
  18. while not 完成:
  19. # 处理用户输入
  20. for 事件 in pygame.event.get():
  21. if 事件.type == pygame.QUIT:
  22. 完成 = True
  23. # 重新绘制窗口
  24. 窗口.fill(深蓝色) # 填充背景
  25. pygame.draw.polygon(窗口, 星星颜色, 星星点) # 画星星
  26. pygame.display.flip() # 更新显示
  27. # 限制FPS
  28. 时钟.tick_busy_loop(60)
  29. pygame.quit()

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

英文:

I have this script that creates a star in Pygame.

  1. import pygame
  2. # Window size
  3. WINDOW_WIDTH = 400
  4. WINDOW_HEIGHT = 400
  5. DARK_BLUE = ( 3, 5, 54)
  6. STARRY = ( 230, 255, 80 )
  7. ### initialisation
  8. pygame.init()
  9. window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
  10. pygame.display.set_caption("Star")
  11. # Define a star
  12. centre_coord = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
  13. star_points = [ (165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
  14. (306, 346), (200, 260), (94, 346), (143, 219), (29, 144) ]
  15. # Main Loop
  16. clock = pygame.time.Clock()
  17. done = False
  18. while not done:
  19. # Handle user-input
  20. for event in pygame.event.get():
  21. if ( event.type == pygame.QUIT ):
  22. done = True
  23. # Re-draw the window
  24. window.fill( DARK_BLUE ) # background fill
  25. pygame.draw.polygon( window, STARRY, star_points ) # Draw the star
  26. pygame.display.flip() # Update the display
  27. # Clamp FPS
  28. clock.tick_busy_loop(60)
  29. 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

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

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

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


选项1

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

  1. import pygame.gfxdraw

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

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

选项2

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

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

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

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

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

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

选项1

  1. import pygame
  2. import pygame.gfxdraw
  3. # 窗口大小
  4. WINDOW_WIDTH = 400
  5. WINDOW_HEIGHT = 400
  6. DARK_BLUE = ( 3, 5, 54)
  7. STARRY = ( 230, 255, 80 )
  8. ### 初始化
  9. pygame.init()
  10. window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
  11. pygame.display.set_caption("Star")
  12. # 定义一个星星
  13. centre_coord = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
  14. star_points = [ (165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
  15. (306, 346), (200, 260), (94, 346), (143, 219), (29, 144) ]
  16. stripes = pygame.Surface(window.get_size())
  17. stripes.fill(STARRY)
  18. offset_x = 400
  19. for x in range(-offset_x, 400, 20):
  20. if (x//20) % 2 == 0:
  21. pts = [(x, 0), (x+20, 0), (x+20+offset_x, 400), (x+offset_x, 400)]
  22. pygame.draw.polygon(stripes, "red", pts)
  23. # 主循环
  24. clock = pygame.time.Clock()
  25. done = False
  26. while not done:
  27. # 处理用户输入
  28. for event in pygame.event.get():
  29. if ( event.type == pygame.QUIT ):
  30. done = True
  31. # 重新绘制窗口
  32. window.fill( DARK_BLUE ) # 背景填充
  33. pygame.gfxdraw.textured_polygon(window, star_points, stripes, 0, 0)
  34. pygame.display.flip() # 更新显示
  35. # 限制帧率
  36. clock.tick_busy_loop(60)
  37. pygame.quit()

选项2

  1. import pygame
  2. # 窗口大小
  3. WINDOW_WIDTH = 400
  4. WINDOW_HEIGHT = 400
  5. DARK_BLUE = ( 3, 5, 54)
  6. STARRY = ( 230, 255, 80 )
  7. ### 初始化
  8. pygame.init()
  9. window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
  10. pygame.display.set_caption("Star")
  11. # 定义一个星星
  12. centre_coord = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
  13. star_points = [ (165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
  14. (306, 346), (200, 260), (94, 346), (143, 219), (29, 144) ]
  15. stripes = pygame.Surface(window.get_size())
  16. stripes.fill(STARRY)
  17. offset_x = 400
  18. for x in range(-offset_x, 400, 20):
  19. if (x//20) % 2 == 0:
  20. pts = [(x, 0), (x+20, 0), (x+20+offset_x, 400), (x+offset_x, 400)]
  21. pygame.draw.polygon(stripes, "red", pts)
  22. mask_image = pygame.Surface(stripes.get_size(), pygame.SRCALPHA)
  23. pygame.draw.polygon(mask_image, (255, 255, 255), star_points)
  24. mask = pygame.mask.from_surface(mask_image)
  25. star_stripes = mask.to_surface(setsurface = stripes.convert_alpha(), unsetcolor = (0, 0, 0, 0))
  26. # 主循环
  27. clock = pygame.time.Clock()
  28. done = False
  29. while not done:
  30. # 处理用户输入
  31. for event in pygame.event.get():
  32. if ( event.type == pygame.QUIT ):
  33. done = True
  34. # 重新绘制窗口
  35. window.fill( DARK_BLUE ) # 背景填充
  36. window.blit(star_stripes, (0, 0))
  37. pygame.display.flip() # 更新显示
  38. # 限制帧率
  39. clock.tick_busy_loop(60)
  40. pygame.quit()
英文:

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

  1. stripes = pygame.Surface(window.get_size())
  2. stripes.fill(STARRY)
  3. offset_x = 400
  4. for x in range(-offset_x, 400, 20):
  5. if (x//20) % 2 == 0:
  6. pts = [(x, 0), (x+20, 0), (x+20+offset_x, 400), (x+offset_x, 400)]
  7. 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:

  1. import pygame.gfxdraw

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

  1. 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:

  1. mask_image = pygame.Surface(stripes.get_size(), pygame.SRCALPHA)
  2. pygame.draw.polygon(mask_image, (255, 255, 255), star_points)
  3. 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?.

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

Minimal examples of both solutions:

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

Option 1

  1. import pygame
  2. import pygame.gfxdraw
  3. # Window size
  4. WINDOW_WIDTH = 400
  5. WINDOW_HEIGHT = 400
  6. DARK_BLUE = ( 3, 5, 54)
  7. STARRY = ( 230, 255, 80 )
  8. ### initialisation
  9. pygame.init()
  10. window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
  11. pygame.display.set_caption("Star")
  12. # Define a star
  13. centre_coord = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
  14. star_points = [ (165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
  15. (306, 346), (200, 260), (94, 346), (143, 219), (29, 144) ]
  16. stripes = pygame.Surface(window.get_size())
  17. stripes.fill(STARRY)
  18. offset_x = 400
  19. for x in range(-offset_x, 400, 20):
  20. if (x//20) % 2 == 0:
  21. pts = [(x, 0), (x+20, 0), (x+20+offset_x, 400), (x+offset_x, 400)]
  22. pygame.draw.polygon(stripes, "red", pts)
  23. # Main Loop
  24. clock = pygame.time.Clock()
  25. done = False
  26. while not done:
  27. # Handle user-input
  28. for event in pygame.event.get():
  29. if ( event.type == pygame.QUIT ):
  30. done = True
  31. # Re-draw the window
  32. window.fill( DARK_BLUE ) # background fill
  33. pygame.gfxdraw.textured_polygon(window, star_points, stripes, 0, 0)
  34. pygame.display.flip() # Update the display
  35. # Clamp FPS
  36. clock.tick_busy_loop(60)
  37. pygame.quit()

Option 2

  1. import pygame
  2. # Window size
  3. WINDOW_WIDTH = 400
  4. WINDOW_HEIGHT = 400
  5. DARK_BLUE = ( 3, 5, 54)
  6. STARRY = ( 230, 255, 80 )
  7. ### initialisation
  8. pygame.init()
  9. window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
  10. pygame.display.set_caption("Star")
  11. # Define a star
  12. centre_coord = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
  13. star_points = [ (165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
  14. (306, 346), (200, 260), (94, 346), (143, 219), (29, 144) ]
  15. stripes = pygame.Surface(window.get_size())
  16. stripes.fill(STARRY)
  17. offset_x = 400
  18. for x in range(-offset_x, 400, 20):
  19. if (x//20) % 2 == 0:
  20. pts = [(x, 0), (x+20, 0), (x+20+offset_x, 400), (x+offset_x, 400)]
  21. pygame.draw.polygon(stripes, "red", pts)
  22. mask_image = pygame.Surface(stripes.get_size(), pygame.SRCALPHA)
  23. pygame.draw.polygon(mask_image, (255, 255, 255), star_points)
  24. mask = pygame.mask.from_surface(mask_image)
  25. star_stripes = mask.to_surface(setsurface = stripes.convert_alpha(), unsetcolor = (0, 0, 0, 0))
  26. # Main Loop
  27. clock = pygame.time.Clock()
  28. done = False
  29. while not done:
  30. # Handle user-input
  31. for event in pygame.event.get():
  32. if ( event.type == pygame.QUIT ):
  33. done = True
  34. # Re-draw the window
  35. window.fill( DARK_BLUE ) # background fill
  36. window.blit(star_stripes, (0, 0))
  37. pygame.display.flip() # Update the display
  38. # Clamp FPS
  39. clock.tick_busy_loop(60)
  40. 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:

确定