如何解决python3中pygame的显示问题
This is the output of my code. I can't figure out how to fix this issue. 这段代码在使用 Pygame 创建的窗口中显示了奇怪的线条。 我正在学习如何在 Pygame 中使用箭头键,但并没有摆脱窗口中的这个小故障。 我所做更改的代码在 geeks for geeks 上: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()
解决方法
在游戏循环开始时添加 win.fill((0,0))
(或您想要的任何背景颜色)。出于某种原因,如果背景没有颜色,macOS 会吓坏。
您的代码应如下所示:
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)
# Fill entire window with black
win.fill((0,0))
# 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()
编辑: 如果没有这段代码,玩家实际上并不会“移动”,而是更像是一个绘图应用而不是游戏。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。