我正在尝试使用带有类的pygame创建自己的游戏,但我的问题是我无法理解为什么我的程序无法正常运行

import pygame
import time
import random
import sys

pygame.init()
screen = pygame.display.set_mode((1280, 720))
pygame.display.set_caption("this game")


class Background:

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

    picture = pygame.image.load("C:/images/dunes.jpg")
    picture = pygame.transform.scale(picture, (1280, 720))

    def draw(self):
        pygame.surface.Surface.blit(picture, (self.xpos, self.ypos))



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

    def move_left(self):
        self.xpos =- 5

    def move_right(self):
        self.xpos =+ 5

    def jump(self):
        for x in range(1, 10):
            self.ypos =-1
            pygame.display.show()

        for x in range(1, 10):
            self.ypos =+1
            pygame.display.show()

    picture = pygame.image.load("C:/pics/hammerhood.png")
    picture = pygame.transform.scale(picture, (200, 200))

    def draw(self):
        pygame.surface.Surface.blit(picture, (self.xpos, self.ypos))




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

    picture = pygame.image.load("C:/pics/dangler_fish.png")
    picture = pygame.transform.scale(picture, (200, 200))


    def teleport(self):
        self.xpos = random.randint(1, 1280)
        self.pos= random.randint(1, 720)

    def draw(self):
        pygame.surface.Surface.blit(picture, (self.xpos, self.ypos))






while True:
    ice = Background(720, 360)
    hammerhood = Monster(200, 500)
    fish = enemy(0, 0)
    fish.teleport()


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if pygame.event == pygame.K_d:
            hammerhood.move_right()

        if pygame.event == pygame.K_a:
            hammerhood.move_left()

        if pygame.event == pygame.K_w:
            hammerhood.jump()

    hammerhood.draw()
    ice.draw()
    fish.draw()


对我说的是,在抽奖中的第49行

pygame.surface.Surface.blit(picture, (self.xpos, self.ypos))
NameError: name 'picture' is not defined


我已经尝试了所有方法,还有另一个项目是我从互联网上复制的,这与在课堂上画图的方式完全相同,但是在此项目中,它不起作用

最佳答案

您可以在班级顶层定义图片,从而使其成为班级属性。要通过某种方法访问它,您必须使用self. picture

关于python - 为什么Pygame Blit不能在类里面工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53238706/

10-12 17:01
查看更多