|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
摩擦摩擦 报错 求助
import pygame
import sys
from pygame.locals import *
from random import *
#球类继承自Sprite类
class Ball(pygame.sprite.Sprite):
def __init__(self,grayball_image,greenball_image,position,speed,bg_size):
#初始化动画精灵
pygame.sprite.Sprite.__init__(self)
self.grayball_image=pygame.image.load.grayball_(image).convert_alpha()
self.greenball_image=pygame.image.load.greenball_(image).convert_alpha()
self.rect=self.grayball_image.get_rect()
#将小球放在指定位置
self.rect.left,self.rect.top=position
self.speed=speed
self.target=target
self.control=False
self.width,self.height=bg_size[0],bg_size[1]
self.radius=self.rect.width/2
def move(self):
self.rect=self.rect.move(self.speed)
#如果小球左侧出来了边界,那么小位置球将改为在右侧的边界
#实现左近右出的效果
if self.rect.right<0:
self.rect.left=self.width
elif self.rect.left>self.width:
self.rect.right=0
elif self.rect.bottom<0:
self.rect.top=self.height
elif self.rect.top>self.height:
self.rect.bottom=0
def check(self,motion):
if self.target<motion<self.target+5:
return True
else:
return False
class Glass(pygame.sprite.Sprite):
def __init__(self,glass_image,mouse_image,bg_size):
#初始化动画精灵
pygame.sprite.Sprite.__init__(self)
self.glass_image=pygame.image.load(glass_image).convert_alpha()
self.glass_rect=self.glass_image.get_rect()
self.glass_rect.left,self.glass_rect.top=\
(bg_size[0]-self.glass_rect.width)//2,\
bg_size[1]-self.glass_rect.height
self.mouse_image=pygame.image.load(mouse_image).convert_alpha()
self.mouse_rect=self.mouse_image.get_rect()
self.mouse_rect.left,self.mouse_rect.top=\
self.glass_rect.left,self.glass_rect.top
pygame.mouse.set_visible(False)
def main():
pygame.init()
grayball_image="D:/085素材摩擦摩擦/gray_ball.png"
greenball_image="D:/085素材摩擦摩擦/green_ball.png"
glass_image="D:/085素材摩擦摩擦/glass.png"
mouse_image="D:/085素材摩擦摩擦/hand.png"
bg_image="D:/085素材摩擦摩擦/background.png"
running=True
#添加魔性的背景音乐
pygame.mixer.music.load("D:/085素材摩擦摩擦/bg_music.ogg")
pygame.mixer.music.play()
#添加音效
loser_sound=pygame.mixer.Sound("D:/085素材摩擦摩擦/loser.wav")
laugh_sound=pygame.mixer.Sound("D:/085素材摩擦摩擦/laugh.wav")
winner_sound=pygame.mixer.Sound("D:/085素材摩擦摩擦/winner.wav")
hole_sound=pygame.mixer.Sound("D:/085素材摩擦摩擦/hole.wav")
#音乐播放完,游戏结束
GAMEOVER=USEREVENT
pygame.mixer.music.set_endevent(GAMEOVER)
#根据背景图片指定游戏界面尺寸
bg_size= width, height=1024,681
screen=pygame.display.set_mode(bg_size)
pygame.display.set_caption("Play the ball - FishC Demo")
background=pygame.image.load(bg_image).convert_alpha()
#用来存放小球对象的列表
balls=[]
group=pygame.sprite.Group()
#创建五个小球
for i in range(5):
#位置随机,速度随机
position=randint(0,width-100),randint(0,height-100)
speed=[randint(-10,10),randint(-10,10)]
ball=Ball(grayball_image,greenball_image,position,speed,bg_size,5*(i+1))
while pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
ball.rect.left,ball.rect.top=randint(0,width-100),randint(0,height-100)
balls.append(ball)
group.add(ball)
glass=Glass(glass_image,mouse_image,bg_size)
motion=0
MYTIMER=USEREVENT+1
pygame.time.set_timer(MYTIMER,1000)
clock=pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
elif event.type==GAMEOVER:
loser_sound.play()
pygame.time.delay(2000)
laugh_sound.play()
running=False
elif event.type==MYTIMER:
if motion:
for each in group:
if each.check(motion):
each.speed=[0,0]
each.control=True
motion=0
elif event.type==MOUSEMOTION:
motion+=1
screen.blit(background,(0,0))
screen.blit(glass.glass_image,glass.glass_rect)
glass.mouse_rect.left,glass.mouse_rect.top=pygame.mouse.get_pos()
if glass.mouse_rect.left<glass.glass_rect.left:
glass.mouse_rect.left=glass.glass_rect.left
if glass.mouse_rect.left>glass.glass_rect.right-glass.mouse_rect.width:
glass.mouse_rect.left=glass.glass_rect.right-glass.mouse_rect.width
if glass.mouse_rect.top<glass.glass_rect.top:
glass.mouse_rect.top=glass.glass_rect.top
if glass.mouse_rect.top>glass.glass_rect.bottom-glass.mouse_rect.height:
glass.mouse_rect.top=glass.glass_rect.bottom-glass.mouse_rect.height
screen.blit(glass.mouse_image,glass.mouse_rect)
for each in balls:
each.move()
if each.control:
#话绿色小球
screen.blit(each.greenball_image,each.rect)
else:
screen.blit(each.grayball_image,each.rect)
for each in group:
group.remove(each)
if pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
each.speed[0]=-each.speed[0]
each.speed[1]=-each.speed[1]
group.add(each)
pygame.display.flip()
clock.tick(30)
if __name__=="__main__":
main()
报错:Traceback (most recent call last):
File "D:/89摩擦摩擦.py", line 189, in <module>
main()
File "D:/89摩擦摩擦.py", line 110, in main
ball=Ball(grayball_image,greenball_image,position,speed,bg_size,5*(i+1))
TypeError: __init__() takes 6 positional arguments but 7 were given
|
|