1 回答
TA贡献1820条经验 获得超2个赞
创建python对象时,实例化需要加上括号,例如:
class Colour:
def __init__( self ):
self.red = 0
self.green = 0
self.blue = 0
light_red = Colour # <-- Wrong
dark_red = Colour() # <-- Correct
这种类型的错误发生在几个不同的实例化中。该错误是因为您的all_sprites精灵组不是精灵组的“副本”,而是对对象定义的引用。时钟对象也是如此sa3at。
另外:有一个错字:fill不是dill;并且该all_sprites小组从未被吸引到屏幕上。
import pygame
import random
import sys
pygame.init()
width=600
height=400
#rangakan
spy=(255,255,255)
swr=(100,0,0)
zard=(255,255,0)
shen=(0,0,255)
rash=(0,0,0)
rashe_tox=(50,50,50)
shasha=pygame.display.set_mode((width,height))
pygame.display.set_caption("Maraka")
sa3at=pygame.time.Clock()
runnung=True
def dist(x1,y1,x2,y2):
return ((x2-x1)**2 + (y2-y1)**2)**1/2
def draw_txt(surf,text,size,x,y):
font_name = pygame.font.match_font("arial")
font = pygame.font.Font(font_name,size)
text_surface = font.render(text,True,spy)
text_rect=text_surface.get_rect()
text_rect.midtop=(x,y)
surf.blit(text_surface,text_rect)
class Snake(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((12,12))
self.image.fill(spy)
self.rect=self.image.get_rect()
self.rect.center=(100,100)
self.speedx=0
self.speedy=0
self.score=0
self.tail=[]
def update(self):
if self.score ==len(self.tail):
self.tail.append((self.rect.x,self.rect.y))
else:
self.tail.append((self.rect.x,self.rect.y))
self.tail.pop(0)
self.rect.x+=self.speedx
self.rect.y+=self.speedy
keys=pygame.key.get_pressed()
if keys[pygame.K_RIGHT] and self.speedx >=0:
self.speedx=10
self.speedy=0
if keys[pygame.K_LEFT] and self.speedx <=0:
self.speedx=10
self.speedy=0
if keys[pygame.K_UP] and self.speedx <=0:
self.speedx=10
self.speedy=0
if keys[pygame.K_DOWN] and self.speedx >=0:
self.speedx=10
self.speedy=0
if self.rect.left >=width:
self.rect.left=1
if self.rect.left >=height:
self.rect.top=0
if self.rect.bottom <=0:
self.rect.bottom=height
if self.rect.left <=0:
self.rect.left= width
def _exit(self):
for i in range (1,len(self.tail)):
if dist(self.rect.x,self.rect.y,self.tail[i][0],self.tail[i][1])<1:
sys.exit()
class Food(pygame.sprite.Sprite):
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((12,12))
self.image.fill(swr)
self.rect=self.image.get_rect()
self.rect.center=(x,y)
all_sprites=pygame.sprite.Group()
player=Snake()
food=Food(random.randrange(20,width-20),random.randrange(20,height-20))
all_sprites.add(player)
all_sprites.add(food)
while runnung:
sa3at.tick(26)
keys_pressed=pygame.event.get()
for i in keys_pressed:
if i.type==pygame.QUIT:
runnung=False
if pygame.sprite.collide_rect(player,food):
food.kill()
player.score+=1
food=Food(random.randrange(20,width-20),random.randrange(20,height-20))
all_sprites.add(food)
all_sprites.update()
shasha.fill(rashe_tox)
all_sprites.draw( shasha )
player._exit()
for i in range(1,len(player.tail)):
pygame.draw.rect(shasha,spy,(player.tail[i][0],player.tail[i][1],12,12))
draw_txt(shasha,str(player.score),18,width/2,10)
all_sprites.draw(shasha)
pygame.display.flip()
pygame.quit()
通过这些更改,代码会生成某种游戏窗口。
添加回答
举报