【问题标题】:pygame making the sprite go upwards and downwardspygame 使精灵向上和向下移动
【发布时间】:2018-11-25 04:37:40
【问题描述】:

我试图通过使用箭头键使精灵上下移动,但它计划只是稍微向上和稍微向下移动:x 和 y 轴有一个速度,还有一个位置。还有两个函数是 draw 和 update(获取新的 xpos 和新的 ypos)。这是我的代码

import pygame
import random
import sys

pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()  # A clock to limit the frame rate.
pygame.display.set_caption("this game")


class Background:
    picture = pygame.image.load("C:/images/cliff.jpg").convert()
    picture = pygame.transform.scale(picture, (1280, 720))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))


class player_first:
    picture = pygame.image.load("C:/aliens/ezgif.com-crop.gif")
    picture = pygame.transform.scale(picture, (200, 200))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y
        self.speed_x = 0
        self.speed_y = 0
        self.rect = self.picture.get_rect()


    def update(self):
        self.xpos =+ self.speed_x
        self.ypos =+ self.speed_y

    def draw(self):      #left right
        #screen.blit(pygame.transform.flip(self.picture, True, False), self.rect)
        screen.blit(self.picture, (self.xpos, self.ypos))







class Bullet_player_1(pygame.sprite.Sprite):
    picture = pygame.image.load("C:/aliens/giphy.gif").convert_alpha()
    picture = pygame.transform.scale(picture, (100, 100))


    def __init__(self):
        self.xpos = 360
        self.ypos = 360
        self.speed_x = 0
        super().__init__()
        self.rect = self.picture.get_rect()


    def update(self):
        self.xpos += self.speed_x

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))
        #self.screen.blit(pygame.transform.flip(self.picture, False, True), self.rect)

    def is_collided_with(self, sprite):
        return self.rect.colliderect(sprite.rect)



player_one_bullet_list = pygame.sprite.Group()
player_one = player_first(0, 0)
cliff = Background(0, 0)

while True:
    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    player_one.speed_y = -5
                    print("up")

                elif event.key == pygame.K_s:
                    player_one.speed_y = 5
                    print("down")



                elif event.key == pygame.K_SPACE:
                    bullet_player_one = Bullet_player_1

                    bullet_player_one.ypos = player_one.ypos
                    bullet_player_one.xpos = player_one.xpos

                    bullet_player_one.speed_x = 14

                    player_one_bullet_list.add(bullet_player_one)




            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_d and player_one.speed_x > 0:
                    player_one.speed_x = 0
                elif event.key == pygame.K_a and player_one.speed_x < 0:
                    player_one.speed_x = 0



    player_one.update()
    cliff.draw()
    player_one.draw()

    player_one_bullet_list.update()

    for bullet_player_one in player_one_bullet_list:
            bullet_player_one.draw()



    pygame.display.flip()
    clock.tick(60)

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    您写的是self.xpos =+ self.speed_x(解释为self.xpos = +self.speed_x)而不是self.xpos += self.speed_x。因此,您不是在将速度添加到位置,而是在覆盖它。

    【讨论】:

    • 你能帮我解决一下什么时候释放 KEY 来阻止角色移动
    • 如果这回答了您的问题,请随时将其标记为已接受。否则,说如果它没有回答它。如果您还有其他问题,请在 Stackoverflow 上搜索,如果不存在,请创建一个新问题。为了帮助您,this answer 提供了该问题的解决方案。
    • 谢谢,但没有帮助,请您尝试在释放钥匙时帮助我以使船停止移动
    • @Will.2004 我不太明白。这是一个新问题还是与帖子相同的问题?我不知道这艘船是什么;是播放器吗?在这种情况下,您有一个 if 语句来停止 x 轴;它不起作用还是您希望它也停止在 y 轴上? Here 是另一个答案,它展示了另一种处理输入的技术,这将使您的玩家能够正确移动和停止。
    • 是的,同样的问题:“当释放 KEY 以阻止角色移动时,你能帮我解决一下吗?”而船就是玩家
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多