2016-10-11 1 views
1

これはStackOverflowの私の最初の投稿です。皆さんが初心者プログラマーを助けてくれることを願っています。それはPygame Pythonの簡単な質問です。Pygame、画面上で正方形を動かすが、前の動きを消去することはできない

私は画面上の四角形を移動しようとしていますが、前の動きを消去することはできません。

import pygame 

pygame.init() 
screen = pygame.display.set_mode((400,300)) 
pygame.display.set_caption("shield hacking") 
JogoAtivo = True 
GAME_BEGIN = False 
# Speed in pixels per frame 
x_speed = 0 
y_speed = 0 
cordX = 10 
cordY = 100 


def desenha(): 
    quadrado = pygame.Rect(cordX, cordY ,50, 52) 

    pygame.draw.rect(screen, (255, 0, 0), quadrado) 
    pygame.display.flip() 



while JogoAtivo: 
    for evento in pygame.event.get(): 
     print(evento); 
    #verifica se o evento que veio eh para fechar a janela 
     if evento.type == pygame.QUIT: 
       JogoAtivo = False 
       pygame.quit(); 
     if evento.type == pygame.KEYDOWN:  
      if evento.key == pygame.K_SPACE: 
        print('GAME BEGIN') 
        desenha() 
        GAME_BEGIN = True; 
      if evento.key == pygame.K_LEFT and GAME_BEGIN: 
        speedX=-3 
        cordX+=speedX 
        desenha() 


      if evento.key == pygame.K_RIGHT and GAME_BEGIN: 
        speedX=3 
        cordX+=speedX 
        desenha() 

答えて

4

前の画像を上書きする必要があります。最も簡単にはそうのように、ゲームループの最初に背景色で画面を埋めるためにある:

screen.fill((0, 0, 0)) 
0

広場が所定の位置にあった前に、あなたはまた、広場を上描き、その後、前の表面からすべてを描くことができますそれはいつも別の場所にあります。 次に、新しいサーフェスを毎回画面に描画します。

しかし、それほど良い戦略ではありませんが、遅くなりちらつきを引き起こす可能性があります。例えば

# Draw anything to the display surface named screen, then do: 
# By display surface I mean the surface returned by pygame.display.set_mode() 
orig = screen.copy() 
# Sometimes it will not be filled with a image from screen that you have drawn before, so you draw 
# all to orig surface 
# Then you make a surface that will be shown on the screen and do: 
s = pygame.surface.Surface() 
s.blit(orig, 0, 0) 
# Then you draw the rectangle or whatever on s, and then: 
screen.blit(s, 0, 0) 
pygame.display.flip() 
# and you keep orig as a starting point for every move 

は、あなたが画面上に多くの可動オブジェクトを持っている場合は、同時に、これは良いですが、またはあなただけの、他の制限された領域を更新している場合は、他の回答で提案されているように、最後の画像の上に再描画します。

ディスプレイ面に直接描画すると、フリッカーが大きくなり、特にディスプレイがハードウェアアクセラレーションを使用すると問題が発生することがあります。特に携帯電話などのデスクトップ以外のデバイスでは描画して新しいものを追加することは、通常、表示面に直接問題なく機能し、問題への正しいアプローチになります。

私は矛盾していると思っていますが、あなたは単にあなた自身のために見なければならないものがあります。実験によって経験を得る。

+0

を役に立てば幸い、あなたがORIGブリット前の表面に)(記入するか、それぞれの時間を持っている、またはようになります毎回新しいサーフェス。 – Dalen

0

私も初心者ですと私は似た何かをやっていると私は、これはところで

import pygame 

clock=pygame.time.Clock() #the frames per second BIF in pygame 
pygame.init() 
FPS=30 
display_width=800 
display_height=600 
white=(255,255,255) 
black=(0,0,0) 
block_size=10 
gameExit=False 
lead_x = display_width/2 
lead_y = display_height/2 

lead_x_change = 0 # 0 beacuse we will not be changing the position in the beginning 
lead_y_change = 0 

gameDisplay=pygame.display.set_mode((display_width,display_height)) 

while not gameExit: #game loop 

    for event in pygame.event.get(): #event handling BIF in pygame EVENT LOOP 
     #print(event) # prints out the position of the mouse and the buttons pressed when in game window 
     if event.type== pygame.QUIT: #if the user presses the [x] in game window, it quits the window 
      gameExit=True  

    if event.key == pygame.K_LEFT: 
       lead_x_change = -block_size #block size is number of pixels moved in one loop 
       lead_y_change=0 

      elif event.key==pygame.K_RIGHT: 
       lead_x_change= block_size 
       lead_y_change=0 

      elif event.key==pygame.K_UP: 
       lead_y_change= -block_size 
       lead_x_change=0 

      elif event.key==pygame.K_DOWN: 
       lead_y_change= block_size 
       lead_x_change=0 
    lead_y+=lead_y_change 
    lead_x+=lead_x_change 

    gameDisplay.fill(white) #fills the display surface object, backgroud color is the parameter filled in   

    pygame.draw.rect(gameDisplay,black,[lead_x,lead_y,block_size,block_size]) 

    pygame.display.update() #after done with all the action,update the surface 

    clock.tick(FPS) #runs the game at 30FPS 

pygame.quit() 
quit() 
関連する問題