簡體   English   中英

pygame 不適用於 pycharm 社區版(無響應)

[英]pygame not working with pycharm community edition (not responding)

alright, so i was just coding in my pygame game called 'flappy box', the game reaches my expectations, but whenever i die ingame or i try to quit the game, the pygame window crashes, i am using pycharm community edition 2019.1, also ,這是我的代碼

import pygame
import random

pygame.init()

win = pygame.display.set_mode((650, 650))
cin = pygame.display.set_mode((650, 650))
pygame.display.set_caption("Flappy box")
colo = (255, 0, 0)
colot = (0, 255, 0)
x = 50
y = 275
width = 40
height = 40
vel = 40
tx = random.choice(range(490, 601))
tin = [0, 345]
ty = random.choice(tin)
th = random.choice(range(270, 590))
tw = 50
level = 0
run = True

while run:
    keys = pygame.key.get_pressed()
    pygame.time.delay(90)
    xi = x + 10
    pygame.time.delay(9)
    x = xi
    yi = y + 10
    pygame.time.delay(1)
    y = yi
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    mover = pygame.Rect(x, y, width, height)
    killer = pygame.Rect(tx, ty, tw, th)

    if keys[pygame.K_SPACE]:
        y -= vel
    if keys[pygame.K_1]:
        y += 15
    win.fill((0, 0, 200))  # Fills the screen with black
    pygame.draw.rect(win, (colo), mover)
    pygame.draw.rect(win, (colot), killer)
    pygame.font.init()
    myfont = pygame.font.SysFont('Comic Sans MS', 15)
    textsurfaces = myfont.render(f'level={level}', False, (255, 0, 0))
    win.blit(textsurfaces, (320, 10))

    while mover.colliderect(killer):
        run =False
    if x == 610:
        xi = 50
        x = xi
        yi = 275
        y = yi
        tx = random.choice(range(70, 601))
        tin = [0, 345]
        ty = random.choice(tin)
        th = random.choice(range(290, 590))
        lvl = level + 1
        level = lvl
    pygame.display.update()
pygame.quit()

所以,當我運行它時,它給了我退出代碼 -805306369 (0xCFFFFFFF) 並且 windows 對話框顯示,python 沒有響應,我該如何解決這個問題?

mover.colliderect(killer)時,您的代碼將進入無限循環,並且不會終止。 由於它沒有從事件隊列中讀取,Windows 將 label 你的程序沒有響應。 我想你打算寫:

if mover.colliderect(killer):
    run = False

或者,您可以使用break ,並避免使用run

if mover.colliderect(killer):
    break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM