gpt4 book ai didi

python - 我在哪里插入设置标题?

转载 作者:行者123 更新时间:2023-12-04 03:30:11 25 4
gpt4 key购买 nike

所以,我一直在为运动和重力做一个 python 程序,一切正常,但我没有设置标题,我真的不知道为什么。我试图在最后插入它,但都不起作用。是我需要写在另一个地方,还是其他类型的问题?

import pygame, sys

WIDTH, HEIGHT = 500, 350

pygame.init()
pygame.display.set_caption('gravity movement')
window = pygame.display.set_mode((WIDTH, HEIGHT), 32, 0)
clock = pygame.time.Clock()

player = pygame.Rect(30, 30, 32, 32)
player_speed = 5


#movement
def move(rect, x, y):
rect.x += x
rect.y += y

#Add gravity
def gravity(rect, g_force= 6):
rect.y += g_force
if rect.y + rect.h >= HEIGHT:
rect.y = HEIGHT - rect.h


x, y = 0,0

while True:
clock.tick(60)#means that for every second (at most) 60 frames should pass

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x = -player_speed
if event.key == pygame.K_RIGHT:
x = player_speed
if event.key == pygame.K_UP:
y = -20
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x = 0
if event.key == pygame.K_RIGHT:
x = 0
if event.key == pygame.K_UP:
y = 0


#Draw
window.fill((0, 0, 20))
pygame.draw.rect(window, (255, 24, 10), player)

#definitive movement
move(player, x= x, y=y)
gravity(player)


pygame.display.flip()

最佳答案

您遇到的问题是因为在此处将标志设置为 32:

window = pygame.display.set_mode((WIDTH, HEIGHT), 32, 0) #Flags = 32 , depth = 0

只要把flags改成0就可以了

应该是这样

window = pygame.display.set_mode((WIDTH, HEIGHT))

您可以阅读更多关于标志和深度的信息 here

所以最终的代码应该是这样的:

import pygame, sys

WIDTH, HEIGHT = 500, 350

pygame.init()
pygame.display.set_caption('gravity movement')
window = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

player = pygame.Rect(30, 30, 32, 32)
player_speed = 5


#movement
def move(rect, x, y):
rect.x += x
rect.y += y

#Add gravity
def gravity(rect, g_force= 6):
rect.y += g_force
if rect.y + rect.h >= HEIGHT:
rect.y = HEIGHT - rect.h


x, y = 0,0

while True:
clock.tick(60)#means that for every second (at most) 60 frames should pass

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x = -player_speed
if event.key == pygame.K_RIGHT:
x = player_speed
if event.key == pygame.K_UP:
y = -20
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x = 0
if event.key == pygame.K_RIGHT:
x = 0
if event.key == pygame.K_UP:
y = 0


#Draw
window.fill((0, 0, 20))
pygame.draw.rect(window, (255, 24, 10), player)

#definitive movement
move(player, x= x, y=y)
gravity(player)


pygame.display.flip()

关于python - 我在哪里插入设置标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67045092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com