当我运行这个代码时,我没有得到任何错误,但是屏幕上什么也没有画

2024-10-03 00:31:22 发布

您现在位置:Python中文网/ 问答频道 /正文

import sys, time, decimal, random, pygame
from pygame.locals import *
pygame.init()
pbif = "pongbackground.jpg"                #my image of the backdrop 1024x576
ppif = "pongpaddle.png"                    #my image of the paddle 33x103

screen = pygame.display.set_mode((1024, 576), 0, 32)        #(16:9)
backdrop = pygame.image.load(pbif).convert() #sets variable 'backdrop' as my image
paddle = pygame.image.load(ppif).convert()   #sets variable 'paddle' as my image

py = 288                                 #initial y co-ordinate for paddle
pmovey = 0                               #SOLVED this was the missing line.

while True:
    for event in pygame.event.get():        #should enable quitting through red x
        if event.type == QUIT:              #this doesn't work either, except when I
            pygame.quit()                   #temporarily remove the drawing block
            sys.exit()                      #from the bottom
        if event.type == KEYDOWN:
            if event.key == K_UP:
                pmovey = -1
            elif event.key == K_DOWN:
                pmovey = 1
        if event.type == KEYUP:
            if event.key == K_UP:
                pmovey = 0
            elif event.key == K_DOWN:
                pmovey = 0

    py += pmovey
    screen.blit(backdrop, (0, 0))           #SHOULD draw background but doesnt
    screen.blit(paddle, (100, py))          #same problem
    pygame.display.update()

啊,我自己解决的。需要线路:

pmovey = 0

在下面

py = 288

我不知道为什么?!我想这和循环的范围有关吧? 任何有类似问题的人都会离开这里。你知道吗


Tags: thekeypyimageimporteventifmy