python - pygame绘制函数

标签 python pygame

如果这是一个简单的修复,但我找不到任何内容,请提前致歉。我对 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/

相关文章:

Python Pickle 在 Windows 中加载时崩溃

python - 事件后 pygame

python - Python多线程时queue.Queue的正确实现

python - Pygame - 两名玩家的射击功能

python - 如何更改 PyOpenGL 中位图字符的字体大小?

python - 广告扰乱了我的文章爬行

python - 在 pygame 中显示的缓冲区在 tkinter 中不显示

java - 从 jython 调用具有可变长度参数(varags)的 Java 方法

python - 将 Python 与 Tkinter 结合使用,如何根据选项菜单中选择的选项使按钮按下执行不同的操作?

**kwargs 中的 Python 可调用参数