【发布时间】:2019-04-02 06:12:01
【问题描述】:
我被教导如何在 pygame 中向左、向右、向上和向下移动图像。我们的下一个任务是让图像沿对角线移动,但我不明白怎么做。到目前为止,这是我的代码:(对不起,奇怪的名字) 哦,还有,我的代码中有两张图片。我想知道是否有一种方法可以移动两个图像而不会在屏幕上消失?例如,我可以使用箭头键移动一个图像,但另一个图像会消失。我也可以使用 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()
【问题讨论】: