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

时间:2013-11-18 15:56:45

标签: python image keyboard pygame

我正在尝试创建一个游戏,其中“player_one.png”由箭头键控制移动并躲避一堆随机放置的“player_two.png”,它可以在足球上以随机方向自行移动领域。

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

我也无法根据箭头键移动“player_one.png”图像。我找到了一个使用Sprites的程序,它以我想让我的玩家移动的方式移动一个红色块,但不能用我的图像切换精灵。

这是我的初始设置,显示字段和彼此面对的两个图像。

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()

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

class Player(pygame.sprite.Sprite):
    # Constructor function
    def __init__(self, color, x, y):
        # Call the parent's constructor
        pygame.sprite.Sprite.__init__(self)

        # Set height, width
        self.image = pygame.Surface([35, 35])
        self.image.fill(color)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.x = 930
        self.rect.y = 250

    def reset_player(self):
        self.rect.x = 930
        self.rect.y = 250

    # Find a new position for the player
    def update(self):
        self.rect.x += self.change_y
        self.rect.y += self.change_x

while done == False:

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

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.rect.x -= player.rect.width
            elif event.key == pygame.K_RIGHT:
                player.rect.x += player.rect.width
            elif event.key == pygame.K_UP:
                player.rect.y -= player.rect.height
            elif event.key == pygame.K_DOWN:
                player.rect.y += player.rect.height


    # -- Draw everything
    # Clear screen
    screen.fill(green)

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


    # Draw sprites  
    all_sprites_list.draw(screen)

    text = font.render("Score: "+str(score), True, black)
    screen.blit(text, [10, 10])


    # Flip screen   
    pygame.display.flip()

    # Pause
    clock.tick(20)  

pygame.quit()

有人可以帮我弄清楚如何让我的图像像精灵那样移动。或者帮我在整个场地上显示“player_two.png”并随机移动。如果“player_two.png”每按一次箭头键每3次移动一次是最好的。如果这很模糊,请给我发电子邮件,我可以发送我的代码。我需要在2周内完成这个游戏!

1 个答案:

答案 0 :(得分:3)

对游戏的多条评论 -

  1. 我没有看到定义self.change_y的位置。此外,更新功能不应该像 - self.rect.x += self.change_x而不是self.rect.x += self.change_y吗?

  2. 此外,主循环应该是这样的

    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
    
  3. 我建议您查看this question