パイガームのイメージを左、右、上下に移動する方法を教えています。私たちの次の仕事はイメージを斜めに動かすことですが、私はどのように理解できません。これはこれまでの私のコードです:(奇妙な名前には申し訳ありません) ああ、私のコードには2つの画像があります。私は画面上に消えずに両方の画像を動かすことができる方法があるかどうか疑問に思っていましたか?たとえば、矢印キーを使用して1つの画像を移動できますが、もう一方の画像は消えます。 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()
あなたのご意見をいただきありがとうございます。これは、私が取り組もうとしているものです。しかし、あなたが言うように、私は「xとy」を定義していないと言います - どうすればいいですか?申し訳ありませんが、これは簡単な質問のように思えますが、私は非常にPythonに新しいですが、勉強することを熱望しています。ありがとう! –
@SummerNguyen私は自分の答えを編集しました。最初にxとyを0に設定しなければなりません。 –
改訂コードを投稿しましたが、それを見ていただければ幸いです。助けてくれてありがとう、それは私にはたくさんのことを意味する。 –