2016-09-27 42 views
0

我已經被教過如何在pygame中左右,上下移動圖像。我們的下一個任務是使圖像沿對角線移動,但我不明白。這是我到目前爲止的代碼:(抱歉奇怪的名字) 哦,還有,我的代碼中有兩個圖像。我想知道是否有一種方法可以移動兩個圖像,而不會在屏幕上消失?例如,我可以使用箭頭鍵移動一個圖像,但其他圖像將消失。我也可以使用WASD移動其他圖像,但第一個圖像會消失。非常感謝!如何在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") 
person = pygame.image.load("google_logo2.png") 

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

#Display changes 
pygame.display.flip() 

#set up pygame event loop 
running = True 
while running: 
for event in pygame.event.get(): 
    print event 
    if event.type == pygame.QUIT: 
      running = False 
    elif event.type == pygame.KEYDOWN: 
     if event.key == pygame.K_q: 
      print "QUITTING NOW..." 
      pygame.time.delay(2000) 
      running = False 
     if event.key == pygame.K_h: 
      print "HELLO!" 
      pygame.time.delay(2500) 
      running = False 
     if event.key == pygame.K_c: 
      print "To move the original Google logo, use the arrow keys. To move the second logo, use the WASD keys." 
     elif event.key == pygame.K_RIGHT: 
      screen.blit(background, (0,0)) 
      left_side = left_side + 10 
      screen.blit(my_image, [left_side, height]) 
      pygame.display.flip() 
     elif event.key == pygame.K_LEFT: 
      screen.blit(background, (0,0)) 
      left_side = left_side - 10 
      screen.blit(my_image, [left_side, height]) 
      pygame.display.flip() 
     elif event.key == pygame.K_UP: 
      screen.blit(background, (0,0)) 
      height = height - 10 
      screen.blit(my_image, [left_side, height]) 
      pygame.display.flip() 
     elif event.key == pygame.K_DOWN: 
      screen.blit(background, (0,0)) 
      height = height + 10 
      screen.blit(my_image, [left_side, height]) 
      pygame.display.flip() 
     elif event.key == pygame.K_w: 
      screen.blit(background, (0,0)) 
      down_diagonal = down_diagonal - 10 
      screen.blit(person, [diagonal, down_diagonal]) 
      pygame.display.flip() 
     elif event.key == pygame.K_a: 
      screen.blit(background, (0,0)) 
      diagonal = diagonal - 10 
      screen.blit(person, [diagonal, down_diagonal]) 
      pygame.display.flip() 
     elif event.key == pygame.K_s: 
      screen.blit(background, (0,0)) 
      down_diagonal = down_diagonal + 10 
      screen.blit(person, [diagonal, down_diagonal]) 
      pygame.display.flip() 
     elif event.key == pygame.K_d: 
      screen.blit(background, (0,0)) 
      diagonal = diagonal + 10 
      screen.blit(person, [diagonal, down_diagonal]) 
      pygame.display.flip() 

pygame.quit() 

編輯︰我修改了我的代碼,就像你說的,但它仍然不是很適合我。 (我對這些問題再次道歉,因爲我對Python非常陌生),我會永遠感謝您的幫助。

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 
screen.blit(my_image, [x, y]) 

#Display changes 
pygame.display.flip() 

keys = {'right':False, 'up':False, 'left':False, 'down':False} 

#set up pygame event loop 
running = True 
while running: 
for event in pygame.event.get(): 
    print event 
    if event.type == pygame.QUIT: 
      running = False 
    elif event.type == pygame.KEYDOWN: 
     if event.key == pygame.K_q: 
      print "QUITTING NOW..." 
      pygame.time.delay(2000) 
      running = False 
     if event.key == pygame.K_h: 
      print "HELLO!" 
      pygame.time.delay(2500) 
      running = False 
     if event.key == pygame.K_c: 
      print "To move the original Google logo, use the arrow keys. To move the second logo, use the WASD keys." 
     if event.key == pygame.K_RIGHT: 
      keys['right'] = True 
     if event.key == pygame.K_UP: 
      keys['up'] = True 
     if event.key == pygame.K_DOWN: 
      keys['down'] = True 
     if event.key == pygame.K_RIGHT: 
      keys['right'] = True 
     if event.key == pygame.K_LEFT: 
      keys['left'] = True 
    elif event.type == pygame.KEYUP: 
     if event.key == pygame.K_RIGHT: 
      keys['right'] = False 
     if event.key == pygame.K_UP: 
      keys['up'] = False 
     if event.key == pygame.K_DOWN: 
      keys['down'] = False 
     if event.key == pygame.K_LEFT: 
      keys['left'] = False 

    x = 0 
    y = 0 

    if keys['right']: 
     x += 10 
    if keys['up']: 
     y += 10 
    if keys['down']: 
     y -=10 
    if keys['left']: 
     x -=10 

pygame.quit() 

回答

0

我認爲你應該監視鍵和鍵,然後做數學。

第一套這樣的:

keys = {'right':False, 'up':False, 'left':False, 'down':False} 

然後在事件KEYDOWN設置你的dict[key]True

if event.key == pygame.K_RIGHT: 
    keys['right'] = True 
if event.key == pygame.K_UP: 
    keys['up'] = True 
... 

而且在事件類型KEYUP做同樣的事情,但設置keys[key]False

然後在你的事件循環:

x = 0 
y = 0 

if keys['right']: 
    x += 10 

if keys['up']: 
    y += 10 

.... 

,然後使用xy移動你的對象。

screen.blit(my_image, [x, y]) 

現在你可以保持鍵按下,你的圖像將被移動,當你釋放你的鑰匙,它會停止(無需反覆自來水鍵移動)

編輯:

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 
screen.blit(my_image, [x, y]) 

#Display changes 
pygame.display.flip() 

keys = {'right':False, 'up':False, 'left':False, 'down':False} 
x = 0 
y = 0 
#set up pygame event loop 
running = True 
while running: 
    screen.blit(my_image, [x, y]) 
    pygame.display.flip() 
    for event in pygame.event.get(): 
     print event 
     if event.type == pygame.QUIT: 
       running = False 
     elif event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_q: 
       print "QUITTING NOW..." 
       pygame.time.delay(2000) 
       running = False 
      if event.key == pygame.K_h: 
       print "HELLO!" 
       pygame.time.delay(2500) 
       running = False 
      if event.key == pygame.K_c: 
       print "To move the original Google logo, use the arrow keys. To move the second logo, use the WASD keys." 
      if event.key == pygame.K_RIGHT: 
       keys['right'] = True 
      if event.key == pygame.K_UP: 
       keys['up'] = True 
      if event.key == pygame.K_DOWN: 
       keys['down'] = True 
      if event.key == pygame.K_LEFT: 
       keys['left'] = True 
     elif event.type == pygame.KEYUP: 
      if event.key == pygame.K_RIGHT: 
       keys['right'] = False 
      if event.key == pygame.K_UP: 
       keys['up'] = False 
      if event.key == pygame.K_DOWN: 
       keys['down'] = False 
      if event.key == pygame.K_LEFT: 
       keys['left'] = False 

     x = 0 
     y = 0 

     if keys['right']: 
      x += 10 
     if keys['up']: 
      y += 10 
     if keys['down']: 
      y -=10 
     if keys['left']: 
      x -=10 

pygame.quit() 
+0

非常感謝您的意見,因爲這是我一直在努力的工作。但是,當我按照你的說法做時,它說我沒有定義「x和y」 - 我將如何做到這一點?對不起,如果這看起來像一個簡單的問題,我是非常新的Python,但很渴望學習。謝謝! –

+0

@SummerNguyen我已經編輯了我的答案,首先你必須將x和y設置爲0. –

+0

我剛剛發佈了我的修改後的代碼,如果能夠查看它,我將非常感激。再次感謝您的幫助,這對我來說意義重大。 –