Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 没有按键时如何添加精灵?_Python_Pygame_Sprite - Fatal编程技术网

Python 没有按键时如何添加精灵?

Python 没有按键时如何添加精灵?,python,pygame,sprite,Python,Pygame,Sprite,没有按键时如何添加精灵?我有每四个方向涵盖当箭头键被按下,但当他们被释放的精灵去明显,但不想如何添加它。我试着添加其他声明和其他东西,我得到的最好的东西是站在前面的精灵在其他的下面,但似乎不能让它去时,一个箭头键被按下,并返回时,他们放松 谢谢你的帮助。提前谢谢。 我的代码: 导入pygame pygame.init() 黑色=(0,0,0) 白色=(255,255,255) 红色=(200,0,0) 绿色=(0,200,0) 蓝色=(0,0200) #背景 background=pygame.

没有按键时如何添加精灵?我有每四个方向涵盖当箭头键被按下,但当他们被释放的精灵去明显,但不想如何添加它。我试着添加其他声明和其他东西,我得到的最好的东西是站在前面的精灵在其他的下面,但似乎不能让它去时,一个箭头键被按下,并返回时,他们放松

谢谢你的帮助。提前谢谢。 我的代码:

导入pygame
pygame.init()
黑色=(0,0,0)
白色=(255,255,255)
红色=(200,0,0)
绿色=(0,200,0)
蓝色=(0,0200)
#背景
background=pygame.image.load('playinggame state2.png')
#性格
standingforward=pygame.image.load('standingforward.png'))
StandingDowndown=pygame.image.load('StandingDowndown.png'))
standingleft=pygame.image.load('standingleft.png')
standingright=pygame.image.load('standingright.png')
#玩家变量
x=375
y=525
w=50
h=50
水平=0.5
屏幕宽度=800
屏幕高度=575
screen=pygame.display.set_模式((屏幕宽度、屏幕高度))
pygame.display.set_标题(“青蛙”)
sprite=pygame.draw.rect
运行=真
运行时:
#精灵
屏幕光点(背景,(0,0))
keys=pygame.key.get_pressed()
如果键[pygame.K_左]:
屏幕blit(站在左侧,(x,y))
如果键[pygame.K_RIGHT]:
屏幕blit(站在右侧,(x,y))
如果键[pygame.K_DOWN]:
屏幕光点(垂直向下,(x,y))
如果键[pygame.K_UP]:
屏幕光点(向前站立,(x,y))
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
pygame.display.flip()
#控制
keys=pygame.key.get_pressed()
如果键[pygame.K_左]:
x-=水平
如果x<0:
x=0
如果键[pygame.K_RIGHT]:
x+=vel
如果x>屏幕宽度-w:
x=屏幕宽度-w
如果键[pygame.K_UP]:
y-=水平
如果y<0:
y=0
如果键[pygame.K_DOWN]:
y+=水平
如果y>屏幕高度-h:
y=屏幕高度-h
pygame.quit()

检查按键行程后,您应该
screen.blit(currentSprite,(x,y))
。在此之前,您应该声明一个变量来保存应该显示的精灵,当您按键时,您应该为该变量分配正确的精灵,例如
currentSprite=standingright
。祝你好运@Bolovsky感谢您的评论,但这是我的第一个项目,因此不确定这意味着什么。在检查按键后,您应该
screen.blit(currentSprite,(x,y))
。在此之前,您应该声明一个变量来保存应该显示的精灵,当您按键时,您应该为该变量分配正确的精灵,例如
currentSprite=standingright
。祝你好运@博洛夫斯基感谢您的评论,不过这是我的第一个项目,所以不确定这意味着什么
import pygame
pygame.init()

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (200, 0, 0)
GREEN = (0, 200, 0)
BLUE = (0, 0, 200)

# background
background = pygame.image.load('Playing Game state 2.png')

# character
standingforward = pygame.image.load('standingforward.png')
standingdownward = pygame.image.load('standingdownwards.png')
standingleft = pygame.image.load('standingleft.png')
standingright = pygame.image.load('standingright.png')

# player variables
x = 375
y = 525
w = 50
h = 50
vel = 0.5
screenWidth = 800
screenHeight = 575

screen = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("FROGGER")
sprite = pygame.draw.rect

running = True
while running:

    # sprites
    screen.blit(background, (0, 0))
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        screen.blit(standingleft, (x, y))
    if keys[pygame.K_RIGHT]:
        screen.blit(standingright, (x, y))
    if keys[pygame.K_DOWN]:
        screen.blit(standingdownward, (x, y))
    if keys[pygame.K_UP]:
        screen.blit(standingforward, (x, y))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.display.flip()

    # controls
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= vel
        if x < 0:
            x = 0
    if keys[pygame.K_RIGHT]:
        x += vel
        if x > screenWidth-w:
            x = screenWidth-w
    if keys[pygame.K_UP]:
        y -= vel
        if y < 0:
            y = 0

    if keys[pygame.K_DOWN]:
        y += vel
        if y > screenHeight-h:
            y = screenHeight-h


pygame.quit()