【问题标题】:pygame draw.rect not working with surface.fillpygame draw.rect 不适用于surface.fill
【发布时间】:2020-03-07 16:38:53
【问题描述】:

您好,我查看了多个帖子,但没有看到我的答案。我正在构建一个简单的蛇游戏,当我遍历draw.rect 时,它不能与surface.fill 一起使用。

看来我把flip()/update()命令放在哪里都没关系,请帮忙!

import pygame
from pygame import Color

surface = pygame.display.set_mode((400, 400))
gs = 20
tail = 5
trail = []
pygame.time.set_timer(pygame.USEREVENT, 250)
sx = sy = 15
vx = vy = 0
block = pygame.Rect(100, 100, 20, 20)

while True:
    for e in pygame.event.get():
        if e.type == pygame.USEREVENT:
            trail.append({"x": sx, "y": sy})
            print(trail)
            while len(trail) > tail:
                trail.pop(0)
            surface.fill(Color("black"))
            for t in trail:
                print(t)
                pygame.draw.rect(
                    surface, Color("green"), (sx * gs, sy * gs, gs - 1, gs - 1)
                )
                pygame.display.update()

            sx += vx
            sy += vy

        if sx > gs - 1:
            sx = 0
        if sx < 0:
            sx = gs - 1
        if sy > gs - 1:
            sy = 0
        if sy < 0:
            sy = gs - 1

        if e.type == pygame.KEYDOWN:
            if e.key == 273 and vy != 1:
                vx, vy = 0, -1
            if e.key == 274 and vy != -1:
                vx, vy = 0, 1
            if e.key == 275 and vx != -1:
                vx, vy = 1, 0
            if e.key == 276 and vx != 1:
                vx, vy = -1, 0

【问题讨论】:

    标签: python pygame pygame-surface


    【解决方案1】:

    您一直在 (sx * gs, sy * gs, gs - 1, gs - 1) 处绘制相同的东西,而不是在轨迹内使用 x 和 y 值。绘制矩形输入应该是:

    surface, Color("green"), (t["x"] * gs, t["y"] * gs, gs - 1, gs - 1)
    

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多