用python3.7中的忍者Pygame进行跳跃攻击冻结

2024-06-26 14:15:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的忍者游戏,有一个滚动的背景。你可以用向下箭头滑动。用向上箭头跳跃,如果你在跳跃的开始,你可以按向右箭头跳跃攻击。问题是你可以拿着右箭头,而在箭头和忍者只是冻结

我不知道如何解决这一点,所以帮助将不胜感激

抱歉,代码太长,但这是代码:

import pygame
import time
x = 60
y = 400
pygame.init()
Counter  = 0
jumpCount = 10
imagerun = 0
imageslide = 0
imagejump = 0
imageattack = 0
slow = 0
jumppower =6.5
isslide = False
isjump = False
isattack = False
ninjaheight = 192
ninjawidth = 192
ninjaheight2 = ninjaheight + 50
ninjawidth2 = ninjawidth + 50
winwidth = 1200
winheight = 600
isrunning = True
repeat = 0
score = 0
def redrawgame():
    global imageattack
    global score
    global isrunning
    win.blit(bg, (bgX, 0))  # draws our first bg image
    win.blit(bg, (bgX2, 0))
    if isrunning == True:
        win.blit(Run[imagerun],(x, y))
    if isslide == True:
        isrunning = False
        win.blit(Slide[imageslide],(x, y))
    if isjump == True and isattack == False:
        win.blit(Jump[imagejump],(x, y))
    if isattack == True:
        if imageattack == 9:
            imageattack = 0
        win.blit(Attack[imageattack],(x, y))
    score += 1
    largeFont = pygame.font.SysFont('comicsans', 65) # Font object
    text = largeFont.render('Score: ' + str(score), 1, (0 ,0 ,255)) # create our text
    win.blit(text, (840, 25)) # draw the text to the screen
    pygame.display.update()


bg = pygame.transform.scale(pygame.image.load('bg.png'),(winwidth, winheight))
bgX = 0
bgX2 = bg.get_width()

win = pygame.display.set_mode((winwidth, winheight))
pygame.display.set_caption("First Game")
Run = [pygame.transform.scale(pygame.image.load('Run__0.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__1.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__2.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__3.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__4.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__5.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__6.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__7.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__8.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__9.png'), (ninjaheight, ninjawidth))]
Slide = [pygame.transform.scale(pygame.image.load('Slide__0.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__1.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__2.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__3.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__4.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__5.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__6.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__7.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__8.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__9.png'), (ninjaheight, ninjawidth))]  
Jump = [pygame.transform.scale(pygame.image.load('Jump.png'), (ninjaheight, ninjawidth))]
Attack = [pygame.transform.scale(pygame.image.load('Jump_Attack__0.png'), (ninjaheight2, ninjawidth2)), pygame.transform.scale(pygame.image.load('Jump_Attack__1.png'), (ninjaheight2, ninjawidth2)), pygame.transform.scale(pygame.image.load('Jump_Attack__2.png'), (ninjaheight2, ninjawidth2)), pygame.transform.scale(pygame.image.load('Jump_Attack__3.png'), (ninjaheight2, ninjawidth2)), pygame.transform.scale(pygame.image.load('Jump_Attack__4.png'), (ninjaheight2, ninjawidth2)), pygame.transform.scale(pygame.image.load('Jump_Attack__5.png'), (ninjaheight2, ninjawidth2)), pygame.transform.scale(pygame.image.load('Jump_Attack__6.png'), (ninjaheight2, ninjawidth2)), pygame.transform.scale(pygame.image.load('Jump_Attack__7.png'), (ninjaheight2, ninjawidth2)), pygame.transform.scale(pygame.image.load('Jump_Attack__8.png'), (ninjaheight2, ninjawidth2)), pygame.transform.scale(pygame.image.load('Jump_Attack__9.png'), (ninjaheight2, ninjawidth2))]
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    time.sleep(0.020)
    Counter += 1
    if slow == 0:
        slow +=1
    elif slow == 1:
        slow -= 1
    keys = pygame.key.get_pressed()   
    if keys[pygame.K_DOWN]:
        if not isslide == True:
                if Counter > 39:
                    if jumpCount == 10:
                        isrunning = False
                        isjump = False
                        isslide = True
                        Counter = 0
                        imageslide = 0

    if isrunning == True:
        if imagerun == 9:
            imagerun = 0
        else:
            imagerun +=1

    if isslide == True:
        if imageslide == 9:
            if repeat == 1:
                isslide = False
                isrunning = True
                imageslide = 0
                repeat = 0
            else:
                repeat +=1
                imageslide = 0
        else:
            imageslide += 1

    if not (isjump):
        if Counter > 1:
            if keys[pygame.K_UP]:
                isjump = True
                isrunning = False
                isslide = False
                Counter = 30
    else:
        if jumpCount < 0: #if negative
            jumpCount -=1
            y -=jumpCount *jumppower
            if slow == 1:
                imageattack += 1
            if jumpCount == -9:
                isjump = False
                jumpCount = 10
                isattack = False
                imageattack = 0
        else:

            if keys[pygame.K_RIGHT]:
                if jumpCount > 6:
                    if not (isattack):
                        isattack = True
                        imageattack = 0
                        Counter = 30
                        jumpCount -= 1
                        y -= jumpCount *jumppower
            else:
                if jumpCount < 0: #if negative
                    jumpCount -=1
                    y -=jumpCount *jumppower
                    if slow == 1:
                        imageattack += 1
                    if jumpCount == -9:
                        jump = False
                        jumpCount = 10
                        isattack = False
                        imageattack = 0
                else:  
                    jumpCount -= 1
                    y -= jumpCount *jumppower
                    if slow == 1:
                        imageattack += 1

    bgX -= 15.4  # Move both background images back
    bgX2 -= 15.4
    if bgX < bg.get_width() * -1:  # If our bg is at the -width then reset its position
        bgX = bg.get_width() 
    if bgX2 < bg.get_width() * -1:
        bgX2 = bg.get_width()
    redrawgame()
    if isslide == False and isjump ==  False and isattack == False:
        isrunning = True       
pygame.quit()

我希望如果你按下右边的按钮,它会像只敲一次一样。谢谢你的帮助,安德鲁


Tags: runimagefalsetrueifpngtransformload
1条回答
网友
1楼 · 发布于 2024-06-26 14:15:11

我跑不动,也不能解决跳跃的问题

它只展示了如何使它更具可读性。它在函数update_xxxdraw_xxx中拆分代码,如update_attack(),draw\u attac(), 'update_bg()draw_bg()。这样mainloop就更短了

现在在代码中搜索问题应该更容易了

import pygame

#  - constants  - (UPPER_CASE_NAMES)

WIN_WIDTH = 1200
WIN_HEIGHT = 600

NINJA_WIDTH = 192
NINJA_HEIGHT = 192
NINJA_WIDTH_2 = NINJA_WIDTH + 50
NINJA_HEIGHT_2 = NINJA_HEIGHT + 50

#  - classes  - (CamelCaseNames)

# empty

#  - functions  - (lower_case_names)

def draw_bg():
    win.blit(bg, (bg_x1, 0))  # draws our first bg image
    win.blit(bg, (bg_x2, 0))

def draw_text():
    text = large_ont.render('Score: ' + str(score), 1, (0 ,0 ,255)) # create our text
    win.blit(text, (840, 25)) # draw the text to the screen

def draw_running():
    win.blit(run_images[run_index], (x, y))

def draw_slide():
    win.blit(slide_images[slide_index], (x, y))

def draw_attack():
    win.blit(attack_images[attack_index], (x, y))

def update_bg():
    global bg_x1
    global bg_x2

    bg_x1 -= 15.4  # Move both background images back
    bg_x2 -= 15.4

    if bg_x1 < -bg.get_width():  # If our bg is at the -width then reset its position
        bg_x1 = bg.get_width() 
    if bg_x2 < -bg.get_width():
        bg_x2 = bg.get_width()

def update_running():
    global run_indes

    if run_index == 9:
        run_index = 0
    else:
        run_index +=1

def update_slide():
    global slide_index
    global is_slide
    global is_running
    global repeat

    is_running = False

    if slide_index == 9:
        if repeat == 1:
            is_slide = False
            is_running = True
            slide_index = 0
            repeat = 0
        else:
            repeat += 1
            slide_index = 0
    else:
        slide_index += 1

def update_attack()
    global image_index

    if image_index == 9:
        image_index = 0

def update_jump():
    global jump_count
    global x, y
    global attack_index
    global is_jump
    global is_attack
    global counter

    if jump_count < 0: #if negative
        jump_count -=1
        y -= jump_count *jump_power
        if slow == 1:
            attack_index += 1
        if jump_count == -9:
            is_jump = False
            jump_count = 10
            is_attack = False
            attack_index = 0
    else:
        if keys[pygame.K_RIGHT]:
            if jump_count > 6:
                if not is_attack:
                    is_attack = True
                    attack_index = 0
                    counter = 30
                    jump_count -= 1
                    y -= jump_count * jump_power
        else:
            if jump_count < 0: #if negative
                jump_count -=1
                y -= jump_count *jump_power
                if slow == 1:
                    attack_index += 1
                if jump_count == -9:
                    jump = False
                    jump_count = 10
                    is_attack = False
                    attack_index = 0
            else:  
                jump_count -= 1
                y -= jump_count *jump_power
                if slow == 1:
                    attack_index += 1

#  - main  - (lower_case_names)

# - init -
pygame.init()

win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
win_rect = win.get_rect()

pygame.display.set_caption("First Game")

# - objects -
x = 60
y = 400

counter  = 0
jump_count = 10

slow = 0
jump_power =6.5
is_slide = False
is_jump = False
is_attack = False
is_running = True
repeat = 0
score = 0

run_index = 0
slide_index = 0
jump_index = 0
attack_index = 0

#

large_font = pygame.font.SysFont('comicsans', 65)

bg = pygame.transform.scale(pygame.image.load('bg.png'), (WIN_WIDTH, WIN_HEIGHT))
bg_x1 = 0
bg_x2 = bg.get_width()


run_images = []
for i in range(10):
    img = pygame.image.load('Run__{}.png'.format(i))
    img = pygame.transform.scale(img, (NINJA_WIDTH, NINJA_HEIGHT)),
    run_images.append(img)

slide_images = []
for i in range(10):
    img = pygame.image.load('Slide__{}.png'.format(i))
    img = pygame.transform.scale(img, (NINJA_WIDTH, NINJA_HEIGHT)),
    slide_images.append(img)

jump_images = [
    pygame.transform.scale(pygame.image.load('Jump.png'), (NINJA_WIDTH, NINJA_HEIGHT))
]

attack_images = []
for i in range(10):
    img = pygame.image.load('Jump_Attack__{}.png'.format(i))
    img = pygame.transform.scale(img, (NINJA_WIDTH_2, NINJA_HEIGHT_2)),
    slide_images.append(img)

# - mainloop -

clock = pygame.time.Clock()
run = True

while run:

    # - events -

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

    # - updates (without draws) -

    counter += 1

    if slow == 0:
        slow +=1
    elif slow == 1:
        slow -= 1

    keys = pygame.key.get_pressed()

    if keys[pygame.K_DOWN]:
        if not is_slide:
            if counter > 39:
                if jump_count == 10:
                    is_running = False
                    is_jump = False
                    is_slide = True
                    counter = 0
                    slide_index = 0

    if is_running:
        update_running()

    if is_slide:
        update_slide()

    if isjump:
        update_jump()
    else:
        if counter > 1:
            if keys[pygame.K_UP]:
                is_jump = True
                is_running = False
                is_slide = False
                counter = 30

    if is_attack:
        update_attack()

    update_bg()

    score += 1

    # - draws (without updates) -

    draw_bg()

    if is_running:
        draw_running()

    if is_slide:
        draw_slide()

    if is_jump and is_attack:
        win.blit(jump_images[jump_index], (x, y))

    if is_attack:
        draw_attack()

    draw_text()

    pygame.display.update()

    # - others -

    if not is_slide and not is_jump and not is_attack:
        is_running = True

    # - speed -

    clock.ticks(30) # speed 30 FPS

# - end -

pygame.quit()

相关问题 更多 >