【发布时间】:2021-04-02 02:36:35
【问题描述】:
This is the output of my code. I can't figure out how to fix this issue. 此代码在使用 Pygame 创建的窗口中显示奇怪的线条。 我正在学习如何在 Pygame 中使用箭头键并且不能摆脱窗口中的这个故障。 我更改的代码在极客的极客上:https://www.geeksforgeeks.org/python-drawing-design-using-arrow-keys-in-pygame/ 这工作得很好,但是当我使用不同大小的窗口和不同的形状来绘制它时,它会在窗口中显示奇怪的线条。
import pygame
import tkinter
pygame.init()
# create the display surfce object of specific dimensions (1000x1000).
win = pygame.display.set_mode((500, 500))
# Setting the window name
pygame.display.set_caption("Dungen World")
# Current coordinates
x = 250
y = 250
# dimensions of the player
width = 10
height = 10
# Speed of movement
vel = 2.5
# Indicator that pygame is running
running = True
# Main game loop
while running:
# Time delay(milliseconds)
pygame.time.delay(10)
# Iterate over the list of event objects
# that was returned by the pygame.event.get()
for event in pygame.event.get():
# if event object type is QUIT
# then quitting the pygame
# and program both
if event.type == pygame.QUIT:
# This will exit the while loop
running = False
# Stores the key pressed
keys = pygame.key.get_pressed()
# If left arrow key is pressed
if keys[pygame.K_LEFT] and x > 5:
# Decrement in x coordinates
x -= vel
# If left arrow key is pressed
if keys[pygame.K_RIGHT] and x < 500 - width:
# Decrement in x coordinates
x += vel
# If left arrow key is pressed
if keys[pygame.K_UP] and y > 5:
# Decrement in x coordinates
y -= vel
# If left arrow key is pressed
if keys[pygame.K_DOWN] and y < 500 - height:
# Decrement in x coordinates
y += vel
# drawing the square on screen
pygame.draw.circle(win, (255, 255, 255), (x, y), width/2)
# To refresh the window
pygame.display.update()
# Closes the pygame window
pygame.quit()
【问题讨论】:
-
代码对我来说很好用。如果您调用
pygame.display.flip()而不是pygame.display.update(),会有什么变化吗? -
在我的 Linux 机器上运行良好。可能是 macOS 问题?
-
似乎是 macOS 图形问题。如果您使用较新版本的python + pygame,问题仍然存在吗?在 Windows 上对我来说,python2.7/3.6/3.8 这工作正常
-
pygame.display.flip() 仍然有同样的问题
-
已更新所有内容,但仍然无法正常工作,可能是 macOS 图形问题!
标签: python macos pygame atom-editor