【发布时间】: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