需要帮助来“画”线在Python中(pygame)

发布于 2024-12-10 14:31:02 字数 491 浏览 0 评论 0原文

我一直在遵循教程来学习 pygame。下面的代码是创建一个绿色的窗口(640 x 400)。该程序还公开了在屏幕上画一条红线的方法。到目前为止,我还没有成功地让这条线出现。有什么建议吗?

#! /usr/bin/env python

import pygame

screen = pygame.display.set_mode((640, 400))
running = 1
green = 0, 255, 0
red = 255, 0, 0
point1 = 639, 479
point2 = 0, 0


while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running = 0

    screen.fill(green)
    pygame.display.flip()
    pygame.draw.line(screen, red, point1, point2)

ive been following a tutorial to learn pygame. the code below is to make a window (640 by 400) that s green. The program is also exposed to draw a red line across the screen. so far i have not been sucessfull in having the line appear. any suggestions?

#! /usr/bin/env python

import pygame

screen = pygame.display.set_mode((640, 400))
running = 1
green = 0, 255, 0
red = 255, 0, 0
point1 = 639, 479
point2 = 0, 0


while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running = 0

    screen.fill(green)
    pygame.display.flip()
    pygame.draw.line(screen, red, point1, point2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无畏 2024-12-17 14:31:02

您需要在display.flip()之前调用draw.line,因为现在您在绘制线条之前将数据从缓冲区复制到显示器。

You need to call draw.line before the display.flip(), as it is now you are copying the data from the buffer to the display before the lines is drawn.

可爱暴击 2024-12-17 14:31:02

为了使某些功能正常工作,您必须在一开始就将代码更改为:

import pygame
from pygame.locals import *
pygame.init()

这可确保您拥有所有必需品并“初始化” pygame。
没有 pygame.init() 它不会“打开”大多数功能

in order for some functions to work you have to change your code at the beginning to:

import pygame
from pygame.locals import *
pygame.init()

this makes sure you have all of the essentials and that you "initialize" pygame.
without pygame.init() it wouldn't "turn on" most of the functions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文