2016-10-13 44 views
0

私は自分のコードを動作させるのに苦労してきました。繰り返しキーをタップするのではなく、W、A、S、またはDを押しながら画像を連続して移動したいのですが、コードに問題があります。今、ビデオシステムは初期化されていないと言われています。私はなぜこれがポップアップしているのかわかりません - 私はすでにコード内にpygame.init()を持っています。pygameで繰り返しコマンドを実行するには?

import pygame 

#set up the initial pygame window 
pygame.init() 
screen = pygame.display.set_mode([900,600]) 

#set background color 
background = pygame.Surface(screen.get_size()) 
background.fill([204,255,229]) 
screen.blit(background, (0,0)) 

#Pull in the image to the program 
my_image = pygame.image.load("google_logo.png") 

#copy the image pixels to the screen 
left_side = 50 
height = 50 
screen.blit(my_image, [left_side, height]) 

#Display changes 
pygame.display.flip() 

moveLeft = False 
moveRight = False 
moveUp = False 
moveDown = False 

#set up pygame event loop 

while True: 
for event in pygame.event.get(): 
    print event 
if event.type == pygame.quit(): 
    running = False 
if event.type == pygame.KEYDOWN: 
     # change the keyboard variables 
    if event.key == K_LEFT or event.key == ord('a'): 
     moveRight = False 
     moveLeft = True 
    if event.key == K_RIGHT or event.key == ord('d'): 
     moveLeft = False 
     moveRight = True 
    if event.key == K_UP or event.key == ord('w'): 
     moveDown = False 
     moveUp = True 
    if event.key == K_DOWN or event.key == ord('s'): 
     moveUp = False 
     moveDown = True 
if event.type == pygame.KEYUP: 
    if event.key == K_ESCAPE: 
     pygame.quit() 
     sys.exit() 
    if event.key == K_LEFT or event.key == ord('a'): 
     moveLeft = False 
    if event.key == K_RIGHT or event.key == ord('d'): 
     moveRight = False 
    if event.key == K_UP or event.key == ord('w'): 
     moveUp = False 
    if event.key == K_DOWN or event.key == ord('s'): 
     moveDown = False 

pygame.quit() 

ありがとう!

+0

あなたの本を考えてオーバー_way_、あなたはいくつかの深刻な設計上の欠陥を持っています。私はこの[質問](http://stackoverflow.com/questions/16626143/how-to-make-a-character-move-while-key-is-held-down)を読むことをお勧めします。 –

答えて

0

私はあなたのコードを少し悩ましていて、少しの間違いを修正しました。うまくいけば、私がいくつかの理由を変えた理由の私のコメントを読んでほしいのですが。また、あなたのコードは、私が信じるいくつかの理由のためにビデオエラーを起こしていました:1.キー「エスケープ」が押されていないかどうかをチェックしていたので、常にpygame.quit()と呼んでいました。(pygame.KEYUP)2. 'pygame.display.flip() 'をゲームループ自体に組み込むことで、一度だけ画面を更新することができます。それだけです。

コールを使用するには、import sysも必要でした。

ビデオのエラーは修正されていますが、画像は移動しません。うまくいけば、なぜあなたは.. ..ハッピーコーディング!

-Travis

import pygame 

# Initialize pygame and create the window! 
pygame.init() 
screen = pygame.display.set_mode((800,600)) 

#Creating background Surface and setting the colour. 
background = pygame.Surface(screen.get_size()) 
background.fill((255,255,0)) 

# Loading a few needed variables. 
my_image = pygame.image.load("dogtest.png") 

left_side = 50 
height = 50 

moveLeft = False 
moveRight = False 
moveUp = False 
moveDown = False 


# These varibales are standard when making a game of some sort! 
total_frames = 0 #How many frames have passed. 
clock = pygame.time.Clock() # Use this in the main loop to set game running 
          # at a certain speed! 
FPS = 60 
running = True #The game condition (whether the loop runs or not!) 

while running: 

    # This is constantly repeating if the user presses the 'x' to close the 
    # program so the program closes safely and efficiently! 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      running = False 
      pygame.quit() 

     if event.type == pygame.KEYDOWN: # This handles Key Presses! 
      if event.key == pygame.K_ESCAPE: 
       running = False 
       pygame.quit() 
       sys.exit() 
      if event.key == pygame.K_LEFT or event.key == ord('a'): 
       moveRight = False 
       moveLeft = True 
      if event.key == pygame.K_RIGHT or event.key == ord('d'): 
       moveLeft = False 
       moveRight = True 
      if event.key == pygame.K_UP or event.key == ord('w'): 
       moveDown = False 
       moveUp = True 
      if event.key == pygame.K_DOWN or event.key == ord('s'): 
       moveUp = False 
       moveDown = True 

     if event.type == pygame.KEYUP: # This is checking if the key ISN'T being pressed!    
      if event.key == pygame.K_LEFT or event.key == ord('a'): 
       moveLeft = False 
      if event.key == pygame.K_RIGHT or event.key == ord('d'): 
       moveRight = False 
      if event.key == pygame.K_UP or event.key == ord('w'): 
       moveUp = False 
      if event.key == pygame.K_DOWN or event.key == ord('s'): 
       moveDown = False 

    # Blitting the Background surface and image! 
    screen.blit(background, (0, 0)) 
    screen.blit(my_image, (left_side, height)) 


    clock.tick(FPS) # Sets the speed in which the code runs, (and the game!) 
    total_frames += 1 # Keeps a variable storing the number of frames have passed 
    pygame.display.update() # Constanly updates the screen! You can also use: pygame.display.flip() 


quit() 
関連する問題