2016-09-27 4 views
0

파이 게임을 왼쪽, 오른쪽, 위, 아래로 움직이는 방법을 배웠습니다. 우리의 다음 임무는 이미지를 대각선으로 움직이는 것이지만 어떻게 이해할 수없는 것입니다. 이것은 지금까지의 코드입니다 : (이상한 이름은 유감스럽게 생각합니다) 아, 그리고 코드에 두 개의 이미지가 있습니다. 화면에 사라지지 않고 두 이미지를 모두 이동할 수있는 방법이 있는지 궁금합니다. 예를 들어 화살표 키를 사용하여 하나의 이미지를 이동할 수 있지만 다른 이미지는 사라집니다. WASD를 사용하여 다른 이미지를 이동할 수도 있지만 첫 번째 이미지는 사라집니다. 정말 고맙습니다!이미지를 파이 게임에서 대각선으로 이동하는 방법

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

나는 아래로 키를 모니터링하고 키를 누른 다음 수학을 수행해야한다고 생각합니다.

먼저 설정이 :

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

그리고 이벤트 유형 KEYUP에 같은 일을하지만 Falsekeys[key]을 설정

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

그런 다음 이벤트 KEYDOWNdict[key] True로 설정합니다. 이벤트 루프에서 다음

:

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"를 정의하지 않았다고 말합니다 - 어떻게하면 좋을까요? 죄송합니다. 간단한 질문 인 것 같습니다. 파이썬을 처음 접했지만 열심히 배워야합니다. 감사! –

+0

@SummerNguyen 나는 내 대답을 편집했다. 먼저 x와 y를 0으로 설정해야한다. –

+0

방금 ​​개정 된 코드를 게시 했으므로 검토해 주시면 고맙겠습니다. 도움을 주셔서 다시 한 번 감사드립니다. 제게 많은 의미가 있습니다. –

관련 문제