当我运行此代码时,我没有错误,但屏幕上没有任何内容

时间:2014-03-09 23:05:18

标签: python pygame screen draw pong

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

我不知道为什么会这样?!我想它与循环的范围有关? 对于有类似问题的人,请留在这里。

0 个答案:

没有答案