在python/pygame中使用箭头键使图像移动一个空间

在python/pygame中使用箭头键使图像移动一个空间,python,image,keyboard,pygame,Python,Image,Keyboard,Pygame,我正在尝试创建一个游戏,其中“player_one.png”由箭头键控制,可以在足球场上随意移动和躲避一堆随机放置的“player_two.png” 我已经创建了一个程序来显示字段和两个图像。我很难让“player_two.png”在球场上显示多个副本,无法让它们移动 我也无法根据箭头键移动“player_one.png”图像。我发现一个程序使用精灵移动一个红色方块,就像我希望我的播放器移动一样,但不能用我的图像切换精灵 这是我的初始设置,它显示字段和两个相对的图像 import pygame

我正在尝试创建一个游戏,其中“player_one.png”由箭头键控制,可以在足球场上随意移动和躲避一堆随机放置的“player_two.png”

我已经创建了一个程序来显示字段和两个图像。我很难让“player_two.png”在球场上显示多个副本,无法让它们移动

我也无法根据箭头键移动“player_one.png”图像。我发现一个程序使用精灵移动一个红色方块,就像我希望我的播放器移动一样,但不能用我的图像切换精灵

这是我的初始设置,它显示字段和两个相对的图像

import pygame
import random
pygame.init()

#colors
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 100, 0)
red = (255, 0, 0)


size = [1000, 500]
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Tony's newest game attempt")


#boolean for while loop
done = False

#create clock 
clock = pygame.time.Clock()

#declare font
font = pygame.font.Font(None, 25)

player_one = pygame.image.load("player_one.png").convert()
player_one.set_colorkey(white)

player_two = pygame.image.load("player_two.png").convert()
player_two.set_colorkey(white)

while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True             


    screen.fill(green)

    for x in range(60,940,35):
        pygame.draw.line(screen, white, [x, 0], [x, 500], 1)


    score = 0
    text = font.render("Score:" +str(score), True, black)

    screen.blit(text, [0, 0])
    screen.blit(player_one, [940, 240])

    screen.blit(player_two, [60, 240])


    pygame.display.flip()

    clock.tick(20)

pygame.quit()
使用类(我无法处理图像)的精灵程序使用以下代码移动播放器精灵:(请注意,这不是整个程序)


有人能帮我弄清楚如何让我的图像像精灵那样移动吗。或者帮助我在整个场地上显示“player_two.png”,并在任意方向移动。如果“player_two.png”每3次移动一次(我按一个箭头键)。如果这是含糊不清的,请给我发电子邮件,我可以发送我的代码。我需要在两周内完成这场比赛

对游戏的多个评论-

  • 我不知道self.change\y是在哪里定义的。另外,更新函数是否应该像-
    self.rect.x+=self.change\ux
    而不是
    self.rect.x+=self.change\uy

  • 另外,主循环应该是这样的

    while not done:
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done=True
    
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    if player.rect.x > 0:
                        player.rect.x -= player.rect.width
                elif event.key == pygame.K_RIGHT:
                    if player.rect.x < screen_width: # the screen/game width
                        player.rect.x += player.rect.width
                # Something similar for the up & down keys
    
    未完成时:
    对于pygame.event.get()中的事件:
    如果event.type==pygame.QUIT:
    完成=正确
    elif event.type==pygame.KEYDOWN:
    如果event.key==pygame.K_左:
    如果player.rect.x>0:
    player.rect.x-=player.rect.width
    elif event.key==pygame.K_RIGHT:
    如果player.rect.x<屏幕宽度:#屏幕/游戏宽度
    player.rect.x+=player.rect.width
    #上下键也有类似的功能
    
  • 我建议您阅读。

    您是否阅读了本教程,特别是接近结尾的“移动多个图像”部分?基本上,您需要创建多个基于“player_two.png”的对象,并将它们存储在某种容器中,比如列表。如果您首先让
    Player
    子类处理图像,还可以将它们存储在
    pygame.sprite.Group
    中。在我看来,你需要决定走哪条路,集中精力让这种方法发挥作用。
    while not done:
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done=True
    
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    if player.rect.x > 0:
                        player.rect.x -= player.rect.width
                elif event.key == pygame.K_RIGHT:
                    if player.rect.x < screen_width: # the screen/game width
                        player.rect.x += player.rect.width
                # Something similar for the up & down keys