我试着用pygame用python构建一个简单的游戏。一开始,我的问题是使移动更加平稳,因为矩形的移动几乎每秒钟都会停滞几毫秒。然后我找到了一个解决办法操作系统环境['SDL\u VIDEODRIVER']='directx'”进入“我的代码”,并将显示模式更改为“全屏”和“双缓冲”。现在的移动更加流畅,但每当我在全屏游戏中使用Alt+Tab时,就会出现以下错误:
Traceback (most recent call last):
File "C:\Users\L-Tramp-GAMING\Documents\Python\Game\Main_Game.py", line 64, in <module>
screen.fill(BG_COLOR)
pygame.error: IDirectDrawSurface3::Blt: Surface was lost
我不知道如何绕过这个问题。我也在想,我是否可以运行在窗口模式下的directx线在正常速度增加游戏。目前,在窗口模式下,游戏的运行速度要快得多。我希望你们能帮我。谢谢你,保罗
import pygame
import random
import os
#Variables
WIDTH = 1280
HEIGHT = 720
GAME_OVER = False
BG_COLOR = (0, 0, 20)
playerWidth = 50
playerHeight = 50
playerPosX = WIDTH / 2 - playerWidth / 2
playerPosY = HEIGHT - (playerHeight + 75)
playerSpeed = 10
enemieWidth = 75
enemieHeight = 75
enemiePosX = random.randint(0, WIDTH - enemieWidth)
enemiePosY = 0
enemieSpeed = 5
enemieCounter = 1
####################################################################################################
os.environ['SDL_VIDEODRIVER'] = 'directx'
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN | pygame.DOUBLEBUF)
pygame.display.set_caption("Game")
pygame.key.set_repeat(1, 10)
clock = pygame.time.Clock()
#GameLoop
while not GAME_OVER:
for e in pygame.event.get():
if e.type == pygame.QUIT:
GAME_OVER = True
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_a:
playerPosX -= playerSpeed
print(hex(screen.get_flags() & 0xFFFFFFFF))
if e.key == pygame.K_d:
playerPosX += playerSpeed
#Graphics
screen.fill(BG_COLOR)
player = pygame.draw.rect(screen, (0, 255, 0), (playerPosX, playerPosY, playerWidth, playerHeight))
if enemiePosY < HEIGHT:
enemie = pygame.draw.rect(screen, (255, 0, 0), (enemiePosX, enemiePosY, enemieWidth, enemieHeight))
enemiePosY += enemieSpeed
else:
enemieCounter += 1
enemiePosY = 0
enemiePosX = random.randint(0, WIDTH - enemieWidth)
if (enemieCounter + 1) % 2 == 0:
pass
#End Graphics
pygame.display.flip()
代码是否可以处理错误,然后尝试重新创建screen对象?
这与从全屏切换到窗口的过程是相同的。你知道吗
编辑:从pygamewiki:https://www.pygame.org/wiki/toggle_fullscreen添加了一些代码,希望能够解决OP评论中的进一步问题。你知道吗
你的行动迟缓是由pygame.key.set\u重复. 为了让玩家按住a和d移动,你可以更新玩家在游戏循环中的位置,而不是通过跟踪速度变量来使用set\ U repeat。如果你想用操作系统环境另一个原因除了修复滞后,然后这将不起作用,但否则这应该是好的。你知道吗
相关问题 更多 >
编程相关推荐