python - 闪烁 2 个圆圈
问题描述
我必须交替使用 pygame 闪烁(打开和关闭)2 个圆圈。如何使用 pygame 让它闪烁。
for event in pygame.event.get():
blueball = pygame.draw.circle(screen, b, (175,100),20,3)
redball = pygame.draw.circle(screen, r, (675,350),20,3)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
bluebally += 5
redballx +=5
if event.key == pygame.K_DOWN:
bluebally += 5
redballx +=5
if event.key == pygame.K_RIGHT:
blueballx += 5
redbally +=5
if event.key == pygame.K_RIGHT:
blueballx += 5
redbally +=5
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(blueball,(blueballx,bluebally))
screen.blit(redball,(redballx,redbally))
我希望蓝球和红球交替闪烁
解决方案
我不知道您的完整代码,但如果这对您有帮助,请告诉我:
绘图功能之前的某处
color_cycle_index = 0
然后在你的绘图功能中
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
bluebally += 5
redballx +=5
if event.key == pygame.K_DOWN:
bluebally += 5
redballx +=5
if event.key == pygame.K_RIGHT:
blueballx += 5
redbally +=5
if event.key == pygame.K_RIGHT:
blueballx += 5
redbally +=5
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if color_cycle_index % 2 == 0:
# draw red
pygame.draw.circle(screen, r, (redballx, redbally), 20, 3)
else:
# draw blue
pygame.draw.circle(screen, b, (blueballx, bluebally), 20, 3)
color_cycle_index += 1 # <-- it's better to increase this every n seconds instead of every frame
推荐阅读
- eclipse - 如何在rcptt中获取当前工作目录
- angular - 带有 ng-template 和 async 的 ngIf 在 Angular 中短暂显示错误值
- python - 如何通过正则表达式拆分python列表
- angular - Angular 材质主题定制
- haskell - 如何为使用 Stack 构建的 Haskell 项目设置可执行输出位置?
- python - Scrapy:如何抓取从下拉列表中选择的多个网页
- c# - asp.net core 2.1 ef core 2.1 日期比较在服务器上不起作用
- jquery - 我可以在 jQuery 中获取一个项目所在的所有 div id 的数组吗?
- c# - 这个自相关公式是否正确编程?
- java - SaxonApiException:提供的节点已经过模式验证,但 XPath 表达式是在没有模式感知的情况下编译的