Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python3中pygame的显示问题_Python_Macos_Pygame_Atom Editor - Fatal编程技术网

python3中pygame的显示问题

python3中pygame的显示问题,python,macos,pygame,atom-editor,Python,Macos,Pygame,Atom Editor,这段代码在使用Pygame创建的窗口中显示奇怪的行。 我正在学习如何在Pygame中使用箭头键,而不能摆脱窗口中的这个小问题。 我更改的代码是关于极客的极客: 这很好,但当我使用不同大小的窗口和不同的形状来绘制时,它会显示窗口中奇怪的线条 import pygame import tkinter pygame.init() # create the display surfce object of specific dimensions (1000x1000). win = pygame.d

这段代码在使用Pygame创建的窗口中显示奇怪的行。 我正在学习如何在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
进口tkinter
pygame.init()
#创建特定尺寸(1000x1000)的显示表面对象。
win=pygame.display.set_模式((500500))
#设置窗口名
pygame.display.set_标题(“Dungen World”)
#当前坐标
x=250
y=250
#玩家的尺寸
宽度=10
高度=10
#运动速度
水平=2.5
#pygame正在运行的指示器
运行=真
#主游戏循环
运行时:
#时间延迟(毫秒)
pygame。时间。延迟(10)
#迭代事件对象列表
#这是pygame.event.get()返回的
对于pygame.event.get()中的事件:
#如果事件对象类型为退出
#然后退出游戏
#并对两者进行编程
如果event.type==pygame.QUIT:
#这将退出while循环
运行=错误
#存储按下的键
keys=pygame.key.get_pressed()
#如果按下左箭头键
如果键[pygame.K_LEFT]和x>5:
#x坐标中的减量
x-=水平
#如果按下左箭头键
如果键[pygame.K_RIGHT]和x<500-宽度:
#x坐标中的减量
x+=vel
#如果按下左箭头键
如果键[pygame.K_UP]和y>5:
#x坐标中的减量
y-=水平
#如果按下左箭头键
如果键[pygame.K_DOWN]和y<500-高度:
#x坐标中的减量
y+=水平
#在屏幕上画正方形
pygame.draw.circle(赢,(255,255,255),(x,y),宽度/2)
#刷新窗口
pygame.display.update()
#关闭pygame窗口
pygame.quit()
在游戏循环开始时添加
win.fill((0,0,0))
(或任何您想要的背景色)。出于某种原因,如果背景没有颜色,macOS会抓狂

您的代码应该如下所示:

导入pygame
进口tkinter
pygame.init()
#创建特定尺寸(1000x1000)的显示表面对象。
win=pygame.display.set_模式((500500))
#设置窗口名
pygame.display.set_标题(“Dungen World”)
#当前坐标
x=250
y=250
#玩家的尺寸
宽度=10
高度=10
#运动速度
水平=2.5
#pygame正在运行的指示器
运行=真
#主游戏循环
运行时:
#时间延迟(毫秒)
pygame。时间。延迟(10)
#用黑色填充整个窗口
赢。填充((0,0,0))
#迭代事件对象列表
#这是pygame.event.get()返回的
对于pygame.event.get()中的事件:
#如果事件对象类型为退出
#然后退出游戏
#并对两者进行编程
如果event.type==pygame.QUIT:
#这将退出while循环
运行=错误
#存储按下的键
keys=pygame.key.get_pressed()
#如果按下左箭头键
如果键[pygame.K_LEFT]和x>5:
#x坐标中的减量
x-=水平
#如果按下左箭头键
如果键[pygame.K_RIGHT]和x<500-宽度:
#x坐标中的减量
x+=vel
#如果按下左箭头键
如果键[pygame.K_UP]和y>5:
#x坐标中的减量
y-=水平
#如果按下左箭头键
如果键[pygame.K_DOWN]和y<500-高度:
#x坐标中的减量
y+=水平
#在屏幕上画正方形
pygame.draw.circle(赢,(255,255,255),(x,y),宽度/2)
#刷新窗口
pygame.display.update()
#关闭pygame窗口
pygame.quit()
编辑: 如果没有这段代码,玩家实际上不会“移动”,其行为更像一个绘图应用程序,而不是一个游戏。

在游戏循环开始时添加
win.fill((0,0,0))
(或任何你想要的背景色)。出于某种原因,如果背景没有颜色,macOS会抓狂

您的代码应该如下所示:

导入pygame
进口tkinter
pygame.init()
#创建特定尺寸(1000x1000)的显示表面对象。
win=pygame.display.set_模式((500500))
#设置窗口名
pygame.display.set_标题(“Dungen World”)
#当前坐标
x=250
y=250
#玩家的尺寸
宽度=10
高度=10
#运动速度
水平=2.5
#pygame正在运行的指示器
运行=真
#主游戏循环
运行时:
#时间延迟(毫秒)
pygame。时间。延迟(10)
#用黑色填充整个窗口
赢。填充((0,0,0))
#迭代事件对象列表
#这是pygame.event.get()返回的
对于pygame.event.get()中的事件:
#如果事件对象类型为退出
#然后退出游戏
#并对两者进行编程
如果event.type==pygame.QUIT:
#这将退出while循环
运行=错误
#存储按下的键
keys=pygame.key.get_pressed()
#如果按下左箭头键
如果键[pygame.K_LEFT]和x>5:
#x坐标中的减量
x-=水平
#如果按下左箭头键
如果键[pygame.K_RIGHT]和x<500-宽度:
#x坐标中的减量
x+=vel
#如果按下左箭头键
如果键[pygame.K_UP]和y>5:
#x坐标中的减量
y-=水平
#如果按下左箭头键
如果键[pygame.K_DOWN]和y<500-高度:
#x坐标中的减量
y+=水平
#在屏幕上画正方形
pygame.draw.circle(赢,(255,255,255),(x,y),宽度/2)
#刷新窗口
pygame.display.update()
#关闭pygame窗口
pygame.quit()
<