gpt4 book ai didi

python - 为什么pygame blit不能在类里面工作

转载 作者:太空宇宙 更新时间:2023-11-04 04:27:35 25 4
gpt4 key购买 nike

我正在尝试使用带有类的 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。图片

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com