不久前我开始学习python,遇到类问题。我将属性放入类中,但是python找不到。我检查了数百次代码,但找不到任何错误。 这是代码:
import pygame
pygame.init()
class igralec():
def _init_(self, x, y, v, h, desno, levo, skok):
self.x = 600
self.y = 250
self.v = 9
self.h = 20
self.a = 0
self.o = 0
self.levo = False
self.desno = False
self.skok = False
def naris(self, screen):
if self.a > 30:
self.a = 0
if self.a%2 == 0:
self.o = 0
elif self.a%2 == 1:
self.o = 1
if self.is_levo:
screen.blit(levahoja[self.o], (self.x, self.y))
self.a += 1
elif self.is_desno:
screen.blit(desnahoja[self.o], (self.x, self.y))
self.a += 1
else:
screen.blit(stanje, (self.x, self.y))
gravitacija = 1
levo = False
desno = False
run = True
skok = False
desnahoja = [pygame.image.load('slavR.png'), pygame.image.load('slavRk.png')]
levahoja = [pygame.image.load('slavL.png'), pygame.image.load('slavLk.png')]
ozadje = pygame.image.load('slav.png')
stanje = pygame.image.load('slavR.png')
screen= pygame.display.set_mode((889, 390))
pygame.display.set_caption("Grafika 101")
slav = igralec()
def grafika():
global a, o
screen.blit(ozadje, (0,0))
slav.naris(self,screen)
pygame.display.flip()
while run:
clock=pygame.time.Clock()
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT and slav.x > slav.v or (skok and event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT and slav.x > slav.v):
slav.x -= slav.v
slav.levo = True
slav.desno = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT and slav.x < 800 - 30 - slav._init_.v or (skok and event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT and slav.x < 800 - 30 - slav.v):
slav.x += slav.v
slav.desno = True
slav.levo = False
else:
slav._desno = False
slav.levo = True
slav.a = 0
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
slav.skok = True
slav.levo = False
slav.desno = False
if slav.skok:
slav.y -= slav.h
slav.h -= gravitacija
if slav.y == 250 and slav.h < 0:
slav.skok = False
slav.h = 20
grafika()
pygame.quit()
这是我得到的错误:
Traceback (most recent call last):
File "C:\Users\Moji podatki\Documents\Python programi\Igrca\grafika 101.py", line 68, in <module>
if slav.skok:
AttributeError: 'igralec' object has no attribute 'skok'
如果您能尽快回答我的问题,我将不胜感激。