简体   繁体   English

当我启动这段代码时,我在 pygame 模块的代码中发现了错误 Traceback (last last call last) 的问题

[英]When I start this code, i found a problem with error Traceback (most recent call last) in my code with pygame module

I have problem with function.我对 function 有疑问。 I starting learn pygame with video course, and this is my first pygame code.我开始通过视频课程学习 pygame,这是我的第一个 pygame 代码。 My.py code:我的.py代码:

import pygame

pygame.init()

win = pygame.display.set_mode((1280, 720))

pygame.display.set_caption("text.")

walkaniml = pygame.image.load('left1.png')
walkanimr = pygame.image.load('right1.png')
stickmanStand = pygame.image.load('stickman.png')

clock = pygame.time.Clock()

x = 250
y = 400
widht = 271
height = 293
speed = 5

jump = False
jumplov = 10

left = False
right = False
animcount = 0

def drawcube():

    global animcount

    win.fill((255, 218, 185))

    if animcount + 1 >= 24:
        animcount = 0

    if left:
        win.blit(walkaniml(animcount // 1, (x, y)))

    elif right:
        win.blit(walkanimr(animcount // 1, (x, y)))

    else:
        win.blit(stickmanStand, (x, y))

    pygame.display.update()

run = True

while run:
    clock.tick(24)

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

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and x > 1:
        x -= speed
        left = True
        right = False

    elif keys[pygame.K_RIGHT] and x < 1280 - widht - 1:
        x += speed
        left = False
        right = True

    else:
        left = False
        right = False
        animcount = 0

    if not(jump):

        if keys[pygame.K_DOWN] and y < 720 - height - 1:
            y += speed

        if keys[pygame.K_SPACE]:
            jump = True
    else:

        if jumplov >= -10:
            if jumplov < 0:
                y += (jumplov ** 2) / 3

            else:
                y -= (jumplov ** 2) / 3
            jumplov -= 1

        else:

            jump = False
            jumplov = 10

enter image description here drawcube()在此处输入图像描述drawcube()

I wanna do a mini-game with stickman, and i found a very big problem, when I start game in cmd and I'm going to web to found decision, but.. I don't find him.我想用火柴人做一个迷你游戏,我发现了一个非常大的问题,当我在 cmd 开始游戏时,我要去 web 找到决定,但是..我找不到他。 Please guys, I realy need help((拜托各位,我真的需要帮助((

walkaniml and walkanimr are pygame.Surface objects. walkanimlwalkanimrpygame.Surface对象。 You can't call an object:您不能调用 object:

win.blit(walkaniml(animcount // 1, (x, y)))

win.blit(walkaniml, (x, y))

If walkaniml and walkanimr are a list of Surfaces , you can get an element from the lists by it's index with subscription (even if the list contains only 1 element):如果walkanimlwalkanimrSurfaces的列表,您可以通过订阅的索引从列表中获取一个元素(即使列表仅包含 1 个元素):

walkaniml = [ pygame.image.load('left1.png') ]
walkanimr = [ pygame.image.load('right1.png') ]
if left:
    if animcount >= len(walkaniml):
        animcount = 0
    win.blit(walkaniml[animcount], (x, y))

elif right:
    if animcount >= len(walkanimr):
        animcount = 0
    win.blit(walkanimr[animcount], (x, y))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次运行此代码时,我都会得到 Traceback(最近一次调用),如何解决这个问题? - I get Traceback (most recent call last) each time I run this code, how to fix this? 为什么我会收到“回溯(最近一次呼叫最后一次):”错误? - Why am I getting a “ Traceback (most recent call last):” error? 我正在获取 Traceback Traceback(最近一次通话最后一次): - i am getting Traceback Traceback (most recent call last): 获取错误消息 Traceback(最近一次调用最后一次):文件“input/code.py”,第 1 行,在<module> a= int(输入()) - Getting error message Traceback (most recent call last): File "input/code.py", line 1, in <module> a= int(input()) 我如何摆脱这个错误:Traceback(最近一次调用最后一次):文件“<string> ",第 1 行,在<module>文件“C:\程序?</module></string> - How Do I get rid of this error:Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\Program? 我收到此错误:Traceback(最近调用最后一次):文件“./prog.py”,第 4 行,在<module>关键错误:“价格”</module> - I'm getting this error : Traceback (most recent call last): File "./prog.py", line 4, in <module> KeyError: 'price' 为什么“Traceback(最近一次调用最后一次):”即使您使用 python 和 selenium 编写正确的代码来自动化应用程序,也会出现错误 - Why "Traceback (most recent call last):" error comes even when you write correct code to automate application using python with selenium 我可以逃避此错误回溯(最近一次通话是最近一次):“文件” <stdin> ”,第1行,在 <module> 档案“ <stdin> ”,第2行,在data_entry中 - can i escape this error Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “<stdin>”, line 2, in data_entry 我的 discord 机器人出现错误“回溯(最近一次通话最后一次):” - My discord bot have a error “Traceback (most recent call last):” 当我尝试运行此Python程序时,出现回溯错误(最近一次调用最近)。 我想知道解决方案 - I get an error of traceback (most recent call last) when I try to run this Python program. I want to know the solution to this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM