Pygame 总是在 python 中崩溃
Pygame always crashing in python
在我 运行 我的 pygame 编码后 运行s 直到水平线框出现在屏幕上,但之后它停止了,当我们试图穿过它时它显示错误Python 没有回应。
这是我的代码:
import pygame
import sys
import random
from time import sleep
pygame.init()
pygame.display.init()
root = pygame.display.set_mode((600, 500))
pygame.display.set_caption('Game attempt')
clock = pygame.time.Clock()
body_horizontel = pygame.image.load(r"snake\body_horizontal.png")
# variables
x_coordinate = random.randint(0, 40)
y_coordinate = random.randint(0, 40)
bg = pygame.image.load(r"bg.jpg")
apple = pygame.image.load(r"apple.png")
body_horizontel_location_x = 260
body_horizontel_location_y = 230
root.blit(bg, (0, 0))
root.blit(apple, (x_coordinate * 15, y_coordinate * 12.5))
while True:
pygame.event.pump()
root.blit(body_horizontel, (body_horizontel_location_x, body_horizontel_location_y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
run = 0
while run < 4:
if body_horizontel_location_y >= 40:
root.blit(bg, (0, 0))
root.blit(apple, (x_coordinate * 15, y_coordinate * 12.5))
body_horizontel_location_y -= 40
root.blit(body_horizontel, (body_horizontel_location_x, body_horizontel_location_y))
sleep(0.01)
pygame.display.update()
run += 1
root.blit(apple, (x_coordinate * 15, y_coordinate * 12.5))
pygame.display.update()
root.blit(bg, (0, 0))
clock.tick(100)
我有 if 子句,因此如果框没有超出屏幕,这会导致问题,因为当我尝试通过单击向上箭头按钮使其超出屏幕时屏幕崩溃。我什至尝试用 pygame.time.delay() 或 clock.tick() 改变睡眠,但其中 none 有效。
嗨,在你的 while 循环中,它继续 运行,直到 运行 大于或等于 4。如果你的 if 语句为真,运行 变量只会加一,所以如果它是假的,你的程序将永远停留在那个 while 循环中,因为 运行 永远不会大于 4
改用它作为您的 if 语句
if body_horizontel_location_y >= 40:
root.blit(bg, (0, 0))
root.blit(apple, (x_coordinate * 15, y_coordinate * 12.5))
body_horizontel_location_y -= 40
root.blit(body_horizontel, (body_horizontel_location_x, body_horizontel_location_y))
sleep(0.01)
pygame.display.update()
run += 1
如您所见,我从 if 语句
中取出了 运行 += 1
在我 运行 我的 pygame 编码后 运行s 直到水平线框出现在屏幕上,但之后它停止了,当我们试图穿过它时它显示错误Python 没有回应。 这是我的代码:
import pygame
import sys
import random
from time import sleep
pygame.init()
pygame.display.init()
root = pygame.display.set_mode((600, 500))
pygame.display.set_caption('Game attempt')
clock = pygame.time.Clock()
body_horizontel = pygame.image.load(r"snake\body_horizontal.png")
# variables
x_coordinate = random.randint(0, 40)
y_coordinate = random.randint(0, 40)
bg = pygame.image.load(r"bg.jpg")
apple = pygame.image.load(r"apple.png")
body_horizontel_location_x = 260
body_horizontel_location_y = 230
root.blit(bg, (0, 0))
root.blit(apple, (x_coordinate * 15, y_coordinate * 12.5))
while True:
pygame.event.pump()
root.blit(body_horizontel, (body_horizontel_location_x, body_horizontel_location_y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
run = 0
while run < 4:
if body_horizontel_location_y >= 40:
root.blit(bg, (0, 0))
root.blit(apple, (x_coordinate * 15, y_coordinate * 12.5))
body_horizontel_location_y -= 40
root.blit(body_horizontel, (body_horizontel_location_x, body_horizontel_location_y))
sleep(0.01)
pygame.display.update()
run += 1
root.blit(apple, (x_coordinate * 15, y_coordinate * 12.5))
pygame.display.update()
root.blit(bg, (0, 0))
clock.tick(100)
我有 if 子句,因此如果框没有超出屏幕,这会导致问题,因为当我尝试通过单击向上箭头按钮使其超出屏幕时屏幕崩溃。我什至尝试用 pygame.time.delay() 或 clock.tick() 改变睡眠,但其中 none 有效。
嗨,在你的 while 循环中,它继续 运行,直到 运行 大于或等于 4。如果你的 if 语句为真,运行 变量只会加一,所以如果它是假的,你的程序将永远停留在那个 while 循环中,因为 运行 永远不会大于 4
改用它作为您的 if 语句
if body_horizontel_location_y >= 40:
root.blit(bg, (0, 0))
root.blit(apple, (x_coordinate * 15, y_coordinate * 12.5))
body_horizontel_location_y -= 40
root.blit(body_horizontel, (body_horizontel_location_x, body_horizontel_location_y))
sleep(0.01)
pygame.display.update()
run += 1
如您所见,我从 if 语句
中取出了 运行 += 1