阻止对象在pygame中的故障

发布于 2025-02-09 03:22:30 字数 2441 浏览 1 评论 0 原文

我正在尝试使用Pygame这样的游戏,我目前有两个矩形,但是一个故障,我不知道为什么,我评论了 win.fill(((0,0,0,0)),它停止了一个在毛刺中的矩形中,但另一个不是我所拥有的:

# import pygame module in this program
import pygame

# activate the pygame library .
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()

# create the display surface object
# of specific dimension..e(500, 500).
win = pygame.display.set_mode((1280, 720))

# set the pygame window name
pygame.display.set_caption("Pong")

# object1 current co-ordinates
x = 15
y = 280
# object2 current co-ordinates
x1 = 1245
y1 = 280
# dimensions of the object1
width = 20
height = 130
# dimensions of the object2
width1 = 20
height1 = 130
# velocity / speed of movement
vel = 10

# Indicates pygame is running
run = True

# infinite loop
while run:
    # creates time delay of 10ms
    pygame.time.delay(0)

    # iterate over the list of Event objects
    # that was returned by pygame.event.get() method.
    for event in pygame.event.get():

        # if event object type is QUIT
        # then quitting the pygame
        # and program both.
        if event.type == pygame.QUIT:

            # it will make exit the while loop
            run = False
    # stores keys pressed
    keys = pygame.key.get_pressed()

    # if left arrow key is pressed
    if keys[pygame.K_w] and y > 0:

        # decrement in y co-ordinate
        y -= vel

    # if left arrow key is pressed
    if keys[pygame.K_s] and y < 720-height:
        # increment in y co-ordinate
        y += vel

    # completely fill the surface object
    # with black colour
    win.fill((0, 0, 0))
    # drawing object on screen which is rectangle here
    pygame.draw.rect(win, (0, 0, 255), (x, y, width, height))

    # it refreshes the window
    pygame.display.update()

    keys2 = pygame.key.get_pressed()

    # if left arrow key is pressed
    if keys2[pygame.K_UP] and y1 > 0:

        # decrement in y co-ordinate
        y1 -= vel

    # if left arrow key is pressed
    if keys2[pygame.K_DOWN] and y1 < 720-height:
        # increment in y co-ordinate
        y1 += vel

    # completely fill the surface object
    # with black colour
    win.fill((0, 0, 0))

    # drawing object on screen which is rectangle here
    pygame.draw.rect(win, (255, 255, 255), (x1, y1, width1, height1))

    # it refreshes the window
    pygame.display.update()

# closes the pygame window
pygame.quit()

I am trying to make a game like pong using pygame I currently have the two rectangles however one glitches I dont know why, I commented win.fill((0, 0, 0)) and it stops one of the rectangles from glitching but the other one does not this is what I have:

# import pygame module in this program
import pygame

# activate the pygame library .
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()

# create the display surface object
# of specific dimension..e(500, 500).
win = pygame.display.set_mode((1280, 720))

# set the pygame window name
pygame.display.set_caption("Pong")

# object1 current co-ordinates
x = 15
y = 280
# object2 current co-ordinates
x1 = 1245
y1 = 280
# dimensions of the object1
width = 20
height = 130
# dimensions of the object2
width1 = 20
height1 = 130
# velocity / speed of movement
vel = 10

# Indicates pygame is running
run = True

# infinite loop
while run:
    # creates time delay of 10ms
    pygame.time.delay(0)

    # iterate over the list of Event objects
    # that was returned by pygame.event.get() method.
    for event in pygame.event.get():

        # if event object type is QUIT
        # then quitting the pygame
        # and program both.
        if event.type == pygame.QUIT:

            # it will make exit the while loop
            run = False
    # stores keys pressed
    keys = pygame.key.get_pressed()

    # if left arrow key is pressed
    if keys[pygame.K_w] and y > 0:

        # decrement in y co-ordinate
        y -= vel

    # if left arrow key is pressed
    if keys[pygame.K_s] and y < 720-height:
        # increment in y co-ordinate
        y += vel

    # completely fill the surface object
    # with black colour
    win.fill((0, 0, 0))
    # drawing object on screen which is rectangle here
    pygame.draw.rect(win, (0, 0, 255), (x, y, width, height))

    # it refreshes the window
    pygame.display.update()

    keys2 = pygame.key.get_pressed()

    # if left arrow key is pressed
    if keys2[pygame.K_UP] and y1 > 0:

        # decrement in y co-ordinate
        y1 -= vel

    # if left arrow key is pressed
    if keys2[pygame.K_DOWN] and y1 < 720-height:
        # increment in y co-ordinate
        y1 += vel

    # completely fill the surface object
    # with black colour
    win.fill((0, 0, 0))

    # drawing object on screen which is rectangle here
    pygame.draw.rect(win, (255, 255, 255), (x1, y1, width1, height1))

    # it refreshes the window
    pygame.display.update()

# closes the pygame window
pygame.quit()

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

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

发布评论

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

评论(1

佞臣 2025-02-16 03:22:30

调用 pygame.display.update()在应用程序循环结束时仅一次。多个调用 pygame.display.update() pygame.display.flip()引起闪烁。参见为什么pygame动画是闪烁的。也只清除显示一次。
实际上,您实际上是在 对象。如果您借鉴了与PyGame显示器关联的 surface ,则在显示屏中不立即可见。更改将可见,当显示显示使用 pygame.display.update() pygame.display.flip()
如果清除显示屏,则所有先前绘制的元素都变得不可见。因此,在框架开始时清除显示器一次,然后在框架末端进行一次更新。

典型的pygame应用程序循环必须:

clock = pygame.time.Clock()
run = True

# application loop
while run:
    # limit the frames per second to limit CPU usage
    clock.tick(100)

    # handle the events    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # update the game states and positions of objects
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w] and y > 0:
        y -= vel
    if keys[pygame.K_s] and y < 720-height:
        y += vel
    if keys[pygame.K_UP] and y1 > 0:
        y1 -= vel
    if keys[pygame.K_DOWN] and y1 < 720-height:
        y1 += vel

    # clear the display
    win.fill((0, 0, 0))

    # draw the entire scene
    pygame.draw.rect(win, (0, 0, 255), (x, y, width, height))
    pygame.draw.rect(win, (255, 255, 255), (x1, y1, width1, height1))

    # update the display
    pygame.display.update()

Call pygame.display.update() only once at the end of the application loop. Multiple calls to pygame.display.update() or pygame.display.flip() cause flickering. See Why is the PyGame animation is flickering. Also clear the display only once per frame.
You are actually drawing on a Surface object. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visible, when the display is updated with either pygame.display.update() or pygame.display.flip().
If you clear the display, all previously drawn elements become invisible. Therefore, clear the display once at the beginning of the frame and update it once at the end of the frame.

The typical PyGame application loop has to:

clock = pygame.time.Clock()
run = True

# application loop
while run:
    # limit the frames per second to limit CPU usage
    clock.tick(100)

    # handle the events    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # update the game states and positions of objects
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w] and y > 0:
        y -= vel
    if keys[pygame.K_s] and y < 720-height:
        y += vel
    if keys[pygame.K_UP] and y1 > 0:
        y1 -= vel
    if keys[pygame.K_DOWN] and y1 < 720-height:
        y1 += vel

    # clear the display
    win.fill((0, 0, 0))

    # draw the entire scene
    pygame.draw.rect(win, (0, 0, 255), (x, y, width, height))
    pygame.draw.rect(win, (255, 255, 255), (x1, y1, width1, height1))

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