为什么我的 Pygame 每次尝试让精灵跳跃时都会崩溃?

Why does my Pygame crash everytime I try to make my sprite jump?

我是第一次在 pygame 中制作游戏,每次单击 W、向上或 Space(我设置为使角色跳跃)都会关闭游戏。我不知道是我不小心让它关闭了游戏还是我对 Python 和 Pygame 不熟悉。我遵循了一些教程,所以教程可能是错误的或已过时。有人知道怎么回事吗?

import pygame
import random
import time

GRAY = (192, 192, 192)
RED  = (255,   0,   0)

class Player(pygame.sprite.Sprite):
    
    def __init__(self, color, x, y, width, weight):
        super().__init__()
        self.color = color
        
        self.image = pygame.Surface([width, weight])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def update(self):
        self.rect.x += 5
    
        if self.rect.x >= 950:
            self.rect.x = 0

#main

def jump(self):
    x = 363
    self.rect.x =+ 50
    x = 413


color_line = (0,0,0)

pygame.init()
screen = pygame.display.set_mode([1000, 500])

all_sprites_list = pygame.sprite.Group()

player_width  = 50
player_weight = 50

player = Player(RED, 50, 413, 50, 50)
all_sprites_list.add(player)

clock = pygame.time.Clock()

running = True
while running:

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

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        jump()
    if keys[pygame.K_w]:
        jump()
    if keys[pygame.K_SPACE]:
        jump()

    
    #updates
    
    all_sprites_list.update()

    #draws
    
    screen.fill((255, 255, 255))

    pygame.draw.line(screen, color_line, (0, 500), (1000, 500), 75)
    
    all_sprites_list.draw(screen)

    pygame.display.flip()

    clock.tick(30)
    
pygame.quit()

好吧,您的 jump( self ) 函数应该在播放器 class 中缩进。这是最主要的事情。初始化值放在 __init__ 内;在那里更有意义。看了这个供参考。
https://github.com/AfoninZ/pybird/blob/6c92b68f196a32e23d77fda17c603c6e1200b1bb/flappybird.py

#! /usr/bin/env python3
##  pip3 install pygame
##  python3 -m pip install pygame

import pygame
import random
import time

pygame .init()
screensize = Width, Height = 1000, 500
screen = pygame .display .set_mode( screensize )

all_sprites_list = pygame .sprite .Group()
clock = pygame .time .Clock()

WHITE = 255, 255, 255
GRAY = 192, 192, 192
RED  = 255, 0, 0
BLACK = 0, 0, 0
GROUND = 463

class Player( pygame .sprite .Sprite ):
    def __init__( self, group ):
        super() .__init__( group )
        self .color = RED
        self .width = 50
        self .height = 50
        self .image = pygame .Surface( [self .width,  self .height] )
        self .image .fill( self .color )
        self .rect = self .image .get_rect()
        self .rect .x = 50
        self .rect .y = GROUND
        self .jumping = False

    def update( self ):
        self .rect .x += 5  ##  right

        if self .rect .x >= 950:  ##  carriage return
            self .rect .x = 0

        if self .jumping:
            self .rect .y -= 8  ##  up
            if self .rect .y <= GROUND -self .height *3:
                self .jumping = False  ##  reached apogee, begin descent
        else:
            self .rect .y += 8  ##  down
            if self .rect .y >= GROUND -self .height:
                self .rect .y = GROUND -self .height

    def jump( self ):  ##  only jump when touching, or nearly touching ground
        if not self .jumping and self .rect .y >= GROUND -self .height *1.1:
            self .jumping = True


player = Player( all_sprites_list )

##  main

running = True
while running:

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

    keys = pygame .key .get_pressed()
    if keys[ pygame .K_UP ]:  player .jump()
    if keys[ pygame .K_w ]:  player .jump()
    if keys[ pygame .K_SPACE ]:  player .jump()

    #updates
    all_sprites_list .update()

    #draws
    screen .fill( WHITE )
    pygame .draw .line( screen, BLACK, ( 0, Height ), ( Width, Height ), 75 )
    
    all_sprites_list .draw( screen )
    pygame .display .flip()

    clock .tick( 30 )
    
pygame .quit()