如果这是一个简单的修复,但我找不到任何内容,请提前致歉。我对 pygame 比较陌生,但我不明白为什么当我运行这个时,绘制的第一个条总是被一半切断。无论如何,对我来说,我应该从 0,400 开始,从 0 开始画到 40,然后向上。如果不是这种情况,请启发好奇的头脑
from pygame import *
import pygame, sys, random
pygame.init()
screen = pygame.display.set_mode((1000,400))
colour = (0, 255, 0)
array = []
x, y, z, b = -80, 0, 0, 0
flag = True
for c in range(5):
array.append(random.randint(100, 400));
for c in array:
print c
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if len(array) == z:
flag = False
if flag == True:
b = array[z]
x += 80
z += 1
pygame.draw.line(screen, colour, (x, 400), (x, (400-b)), 40)
pygame.display.update()
最佳答案
pygame 正在从 (0,400) 到 (0, 400-b) 绘制一条线,线宽为 40。
这是一种移动线条的方法,使每条线条都完全可见:
for i, b in enumerate(array):
x = i*80 + 20 # <-- add 20 to shift by half the linewidth
pygame.draw.line(screen, colour, (x, 400), (x, (400-b)), 40)
<小时/>
import sys
import random
import pygame
pygame.init()
screen = pygame.display.set_mode((1000,400))
colour = (0, 255, 0)
array = [random.randint(100, 400) for c in range(5)]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
linewidth = 40
for i, b in enumerate(array):
x = i*linewidth*2 + linewidth//2
pygame.draw.line(screen, colour, (x, 400), (x, (400-b)), linewidth)
pygame.display.update()
关于python - pygame绘制函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18933963/