乐趣区

pythonista ipad游戏代码保存

from scene import *import soundimport randomimport mathimport osA = Action
class MyScene (Scene):
def setup(self):
self.background_color=’white’

self.carlist=[]

x=100
y=300
#self.a=self.spawn_car(x,y-199)
#self.b=self.spawn_car(x+199,y-199)
#self.spawn_car(x+398,y-199)
#self.c=self.spawn_car(x,y)
#self.spawn_car(x+199,y)
#self.spawn_car(x+398,y)
#self.spawn_car(x,y+199)
#self.spawn_car(x+199,y+199)
#self.spawn_car(x+398,y+199)

self.a=self.spawn_car(x,y-200,0)
self.b=self.spawn_car(x+200,y-200,0)
self.spawn_car(x+400,y-200,0)
self.c=self.spawn_car(x,y,0)
self.spawn_car(x+200,y,4)
self.spawn_car(x+400,y,0)
self.spawn_car(x,y+200,0)
self.spawn_car(x+200,y+200,0)
self.d=self.spawn_car(x+400,y+200,0)

self.atcaryes=True
self.phyes=False
self.pyyes=False

self.p=self.a.position

self.p1=self.a.position
self.p2=self.a.position

self.rot=0
self.radian=0
self.movable=False

self.rottotalnum=0

#card5Img=’5.png’
#card1Img=’1.png’
cardlistred=[‘1.png’,’2.png’,’3.png’,’4.png’,’5.png’,’6.png’,’7.png’,’8.png’,’9.png’,’10.png’,’11.png’,’12.png’,’13.png’]
#cardlistred.append(card5Img)
#cardlistred.append(card1Img)

#card103Img=’103.png’
#card107Img=’107.png’
cardlistblack=[‘101.png’,’102.png’,’103.png’,’104.png’,’105.png’,’106.png’,’107.png’,’108.png’,’109.png’,’110.png’,’111.png’,’112.png’,’113.png’]
#cardlistblack.append(card103Img)

self.card=self.spawn_card(cardlistred,self.a)
self.card2=self.spawn_card(cardlistblack,self.d)

#set car.plusnum
for car in self.carlist:
car.plusnum=random.randint(-5,+5)

#self.run_action(A.sequence(A.call(self.movecard),A.wait(1)))
#self.run_action(A.sequence(A.call(self.movecard),A.wait(1)))
#self.run_action(A.sequence(A.call(self.movecard),A.wait(1)))
def rotate(self):
#self.collidewithcar()
acar=self.atcar()
if self.atcaryes and acar is not None:

ax=acar.position.x
ay=acar.position.y
#self.rot=(self.rot+90)%360
#self.radian=self.rot*math.pi/180
”’for carname in self.carlist:
if not carname.rotatable:
carname.remove_all_actions() ”’

if acar.rotatable:
acar.run_action(A.rotate_by((math.pi/2),0))
acar.rotnum=acar.rotnum+1
self.rottotalnum=(self.rottotalnum+1)%6
acar.calnum=(acar.calnum+1)%4
acar.carnum=(acar.calnum+acar.num)%4

if acar.num==9:
self.new_hitboxh=Rect(ax+acar.hitboxv.y-ay,ay+acar.hitboxv.x-ax,acar.hitboxv.h,acar.hitboxv.w)
self.new_hitboxv=Rect(ax+acar.hitboxh.y-ay,ay+acar.hitboxh.x-ax,acar.hitboxh.h,acar.hitboxh.w)
acar.hitboxh=self.new_hitboxh
acar.hitboxv=self.new_hitboxv
if acar.num==1 or acar.num==2 or acar.num==3 or acar.num==4:
if acar.carnum==1:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,202,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y,10,101)

if acar.carnum==2:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,5,202)

if acar.carnum==3:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,202,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,101)

if acar.carnum==0:
acar.hitboxh=Rect(acar.position.x,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,202)
if acar.num==5 or acar.num==6 or acar.num==7 or acar.num==8:
if acar.carnum==1:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y,5,101)

if acar.carnum==2:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,101)

if acar.carnum==3:
acar.hitboxh=Rect(acar.position.x,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,101)

if acar.carnum==0:
acar.hitboxh=Rect(acar.position.x,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y,5,101)
#rotate unrotated
if self.rottotalnum==0:
for acar in self.carlist:
if acar.rotnum==0:
acar.run_action(A.rotate_by((math.pi/2),0))
ax=acar.position.x
ay=acar.position.y

acar.calnum=(acar.calnum+1)%4
acar.carnum=(acar.calnum+acar.num)%4

if acar.num==9:
self.new_hitboxh=Rect(ax+acar.hitboxv.y-ay,ay+acar.hitboxv.x-ax,acar.hitboxv.h,acar.hitboxv.w)
self.new_hitboxv=Rect(ax+acar.hitboxh.y-ay,ay+acar.hitboxh.x-ax,acar.hitboxh.h,acar.hitboxh.w)
acar.hitboxh=self.new_hitboxh
acar.hitboxv=self.new_hitboxv
if acar.num==1 or acar.num==2 or acar.num==3 or acar.num==4:
if acar.carnum==1:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,202,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y,10,101)

if acar.carnum==2:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,5,202)

if acar.carnum==3:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,202,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,101)

if acar.carnum==0:
acar.hitboxh=Rect(acar.position.x,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,202)
if acar.num==5 or acar.num==6 or acar.num==7 or acar.num==8:
if acar.carnum==1:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y,5,101)

if acar.carnum==2:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,101)

if acar.carnum==3:
acar.hitboxh=Rect(acar.position.x,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,101)

if acar.carnum==0:
acar.hitboxh=Rect(acar.position.x,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y,5,101)

def movecollidewithcar(self):

acar=self.atcar()
if self.atcaryes and acar is not None:

#print(acar.position)
carmlist=[]

self.carlist.remove(acar)
for car in self.carlist:
if car is not None and acar is not None and car.hitboxh.intersects(acar.hitboxh):
carmlist.append(car)

#self.phyes=True
self.movable=True
#print(‘hh’)

elif car is not None and acar is not None and car.hitboxv.intersects(acar.hitboxv):
carmlist.append(car)

#self.pyyes=True
self.movable=True
#print(‘vv’)

if self.movable:
carm=random.choice(carmlist)
self.p=(carm.position.x-3,carm.position.y)

#resultintfilename=self.card.intfilename+carm.plusnum
self.card.intfilename=self.card.intfilename+carm.plusnum
#resultintfilename=self.card.intfilename
if self.card.intfilename>=13:
self.card.intfilename=13
if self.card.intfilename<=1:
self.card.intfilename=1
resultstrfilename=str(self.card.intfilename)
self.card.remove_from_parent()
self.card=self.change_card(self.card,(resultstrfilename+’.png’))

#self.phyes=False
#self.pyyes=False
self.carlist.append(acar)

if self.movable:
self.card.position=self.p
#print(self.card.position)

acar.rotatable=False
self.movable=False

def atcar(self):
for car in self.carlist:
if car is not None and car.bbox.intersects(self.card.bbox):
self.atcaryes=True
car.rotatable=True
#print(‘at’)
#print(car.position)
return car

#card2operation
def rotate2(self):
#self.collidewithcar()
acar=self.atcar2()
if self.atcaryes and acar is not None:

ax=acar.position.x
ay=acar.position.y
#self.rot=(self.rot+90)%360
#self.radian=self.rot*math.pi/180
”’for carname in self.carlist:
if not carname.rotatable:
carname.remove_all_actions() ”’

if acar.rotatable:
acar.run_action(A.rotate_by((math.pi/2),0))
acar.rotnum=acar.rotnum+1
self.rottotalnum=(self.rottotalnum+1)%6
acar.calnum=(acar.calnum+1)%4
acar.carnum=(acar.calnum+acar.num)%4

if acar.num==9:
self.new_hitboxh=Rect(ax+acar.hitboxv.y-ay,ay+acar.hitboxv.x-ax,acar.hitboxv.h,acar.hitboxv.w)
self.new_hitboxv=Rect(ax+acar.hitboxh.y-ay,ay+acar.hitboxh.x-ax,acar.hitboxh.h,acar.hitboxh.w)
acar.hitboxh=self.new_hitboxh
acar.hitboxv=self.new_hitboxv
if acar.num==1 or acar.num==2 or acar.num==3 or acar.num==4:
if acar.carnum==1:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,202,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y,10,101)

if acar.carnum==2:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,5,202)

if acar.carnum==3:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,202,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,101)

if acar.carnum==0:
acar.hitboxh=Rect(acar.position.x,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,202)
if acar.num==5 or acar.num==6 or acar.num==7 or acar.num==8:
if acar.carnum==1:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y,5,101)

if acar.carnum==2:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,101)

if acar.carnum==3:
acar.hitboxh=Rect(acar.position.x,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,101)

if acar.carnum==0:
acar.hitboxh=Rect(acar.position.x,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y,5,101)
#rotate unrotated
if self.rottotalnum==0:
for acar in self.carlist:
if acar.rotnum==0:
acar.run_action(A.rotate_by((math.pi/2),0))
ax=acar.position.x
ay=acar.position.y

acar.calnum=(acar.calnum+1)%4
acar.carnum=(acar.calnum+acar.num)%4

if acar.num==9:
self.new_hitboxh=Rect(ax+acar.hitboxv.y-ay,ay+acar.hitboxv.x-ax,acar.hitboxv.h,acar.hitboxv.w)
self.new_hitboxv=Rect(ax+acar.hitboxh.y-ay,ay+acar.hitboxh.x-ax,acar.hitboxh.h,acar.hitboxh.w)
acar.hitboxh=self.new_hitboxh
acar.hitboxv=self.new_hitboxv
if acar.num==1 or acar.num==2 or acar.num==3 or acar.num==4:
if acar.carnum==1:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,202,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y,10,101)

if acar.carnum==2:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,5,202)

if acar.carnum==3:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,202,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,101)

if acar.carnum==0:
acar.hitboxh=Rect(acar.position.x,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,202)
if acar.num==5 or acar.num==6 or acar.num==7 or acar.num==8:
if acar.carnum==1:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y,5,101)

if acar.carnum==2:
acar.hitboxh=Rect(acar.position.x-101,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,101)

if acar.carnum==3:
acar.hitboxh=Rect(acar.position.x,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y-101,10,101)

if acar.carnum==0:
acar.hitboxh=Rect(acar.position.x,acar.position.y-5,101,10)
acar.hitboxv=Rect(acar.position.x-5,acar.position.y,5,101)

def movecollidewithcar2(self):

acar=self.atcar2()
if self.atcaryes and acar is not None:

#print(acar.position)
carmlist=[]

self.carlist.remove(acar)
for car in self.carlist:
if car is not None and acar is not None and car.hitboxh.intersects(acar.hitboxh):
carmlist.append(car)

#self.phyes=True
self.movable=True
#print(‘hh’)

elif car is not None and acar is not None and car.hitboxv.intersects(acar.hitboxv):
carmlist.append(car)

#self.pyyes=True
self.movable=True
#print(‘vv’)

if self.movable:
carm=random.choice(carmlist)
self.p=(carm.position.x+3,carm.position.y)

#resultintfilename=self.card2.intfilename+carm.plusnum
self.card2.intfilename=self.card2.intfilename+carm.plusnum
#resultintfilename=self.card2.intfilename
if self.card2.intfilename>=113:
self.card2.intfilename=113
if self.card2.intfilename<=101:
self.card2.intfilename=101
resultstrfilename=str(self.card2.intfilename)
self.card2.remove_from_parent()
self.card2=self.change_card(self.card2,(resultstrfilename+’.png’))

#self.phyes=False
#self.pyyes=False
self.carlist.append(acar)

if self.movable:
self.card2.position=self.p

#######################################################
#twocardoverlapped
#if self.card2.position==self.card.position:
#print(‘overlapp’)
#print(self.card2.position)
#self.card2.position=(self.card2.position.x+10,self.card2.position.y)
#print(self.card2.position)
#self.card.position=(self.card.position.x-40,self.card.position.y)
#if not(self.card2.position==self.card.position):
#???##############################################################

acar.rotatable=False
self.movable=False

def atcar2(self):
for car in self.carlist:
if car is not None and car.bbox.intersects(self.card2.bbox):
self.atcaryes=True
car.rotatable=True
#print(‘at’)
#print(car.position)
return car

else:self.atcaryes=False
def spawn_card(self,listname,positioncar):
file=random.choice(listname)

card1=SpriteNode(file)
card1.blend_mode=BLEND_MULTIPLY
card1.position=positioncar.position
filename=os.path.splitext(file)[0]
card1.intfilename=int(filename)
self.add_child(card1)
return card1

def change_card(self,lastcardname,file):
card1=SpriteNode(file)
card1.blend_mode=BLEND_MULTIPLY
card1.position=lastcardname.position
card1.intfilename=lastcardname.intfilename
self.add_child(card1)
return card1

def spawn_car(self,x,y,pnum):
randomcarlist=[]
car1=SpriteNode(‘img1.png’)
car1.blend_mode=BLEND_MULTIPLY
car1.position=(x,y)
car1.rotnum=0
car1.num=1
car1.calnum=0
car1.carnum=0
car1.hitboxh=Rect(car1.position.x-101,car1.position.y-5,202,10)
car1.hitboxv=Rect(car1.position.x-5,car1.position.y,10,101)
randomcarlist.append(car1)

car2=SpriteNode(‘img2.png’)
car2.blend_mode=BLEND_MULTIPLY
car2.position=(x,y)
car2.rotnum=0
car2.num=2
car2.calnum=0
car2.carnum=0
car2.hitboxh=Rect(car2.position.x-101,car2.position.y-5,101,10)
car2.hitboxv=Rect(car2.position.x-5,car2.position.y-101,5,202)
randomcarlist.append(car2)

car3=SpriteNode(‘img3.png’)
car3.blend_mode=BLEND_MULTIPLY
car3.position=(x,y)
car3.rotnum=0
car3.num=3
car3.calnum=0
car3.carnum=0
car3.hitboxh=Rect(car3.position.x-101,car3.position.y-5,202,10)
car3.hitboxv=Rect(car3.position.x-5,car3.position.y-101,10,101)
randomcarlist.append(car3)

car4=SpriteNode(‘img4.png’)
car4.blend_mode=BLEND_MULTIPLY
car4.position=(x,y)
car4.rotnum=0
car4.num=4
car4.calnum=0
car4.carnum=0
car4.hitboxh=Rect(car4.position.x,car4.position.y-5,101,10)
car4.hitboxv=Rect(car4.position.x-5,car4.position.y-101,10,202)
randomcarlist.append(car4)

car5=SpriteNode(‘img5.png’)
car5.blend_mode=BLEND_MULTIPLY
car5.position=(x,y)
car5.rotnum=0
car5.num=5
car5.calnum=0
car5.carnum=0
car5.hitboxh=Rect(car5.position.x-101,car5.position.y-5,101,10)
car5.hitboxv=Rect(car5.position.x-5,car5.position.y,5,101)
randomcarlist.append(car5)

car6=SpriteNode(‘img6.png’)
car6.blend_mode=BLEND_MULTIPLY
car6.position=(x,y)
car6.rotnum=0
car6.num=6
car6.calnum=0
car6.carnum=0
car6.hitboxh=Rect(car6.position.x-101,car6.position.y-5,101,10)
car6.hitboxv=Rect(car6.position.x-5,car6.position.y-101,10,101)
randomcarlist.append(car6)

car7=SpriteNode(‘img7.png’)
car7.blend_mode=BLEND_MULTIPLY
car7.position=(x,y)
car7.rotnum=0
car7.num=7
car7.calnum=0
car7.carnum=0
car7.hitboxh=Rect(car7.position.x,car7.position.y-5,101,10)
car7.hitboxv=Rect(car7.position.x-5,car7.position.y-101,10,101)
randomcarlist.append(car7)

car8=SpriteNode(‘img8.png’)
car8.blend_mode=BLEND_MULTIPLY
car8.position=(x,y)
car8.rotnum=0
car8.num=8
car8.calnum=0
car8.carnum=0
car8.hitboxh=Rect(car8.position.x,car8.position.y-5,101,10)
car8.hitboxv=Rect(car8.position.x-5,car8.position.y,5,101)
randomcarlist.append(car8)

car9=SpriteNode(‘img9.png’)
car9.blend_mode=BLEND_MULTIPLY
car9.position=(x,y)
car9.rotnum=0
car9.num=9
car9.calnum=0
car9.carnum=0
car9.hitboxh=Rect(car9.position.x-101,car9.position.y-5,202,10)
car9.hitboxv=Rect(car9.position.x-0.5,car9.position.y-0.5,1,1)
randomcarlist.append(car9)

car10=SpriteNode(‘img10.png’)
car10.blend_mode=BLEND_MULTIPLY
car10.position=(x,y)
car10.rotnum=0
car10.num=9
car10.calnum=0
car10.carnum=0
car10.hitboxh=Rect(car10.position.x-0.5,car10.position.y-0.5,1,1)
car10.hitboxv=Rect(car10.position.x-5,car10.position.y-101,10,202)
randomcarlist.append(car10)

car11=SpriteNode(‘img11.png’)
car11.blend_mode=BLEND_MULTIPLY
car11.position=(x,y)
car11.rotnum=0
car11.num=9
car11.calnum=0
car11.carnum=0
car11.hitboxh=Rect(car11.position.x-101,car11.position.y-5,202,10)
car11.hitboxv=Rect(car11.position.x-5,car11.position.y-101,10,202)
randomcarlist.append(car11)

car=random.choice(randomcarlist)
self.add_child(car)
car.rotatable=False
car.plusnum=pnum
self.carlist.append(car)

return car

def did_change_size(self):
pass

def update(self):
#self.rot=(self.rot+90)%360
#if self.a.hitboxh.intersects(self.b.hitboxh):
#print(1)
#print(self.a.hitboxh.intersection(self.b.hitboxh))
#if self.a.hitboxv.intersects(self.c.hitboxv):
#print(1)
#print(self.a.hitboxv.intersection(self.c.hitboxv))
#for car in self.carlist:
#act=A.repeat(A.rotate_by(math.pi/2,1),-1)
#car.run_action(act)
#self.run_action(A.sequence(A.call(self.movecard),A.wait(10)))
#self.run_action(A.sequence(A.call(self.movecard),A.wait(10)))
#self.run_action(A.repeat(A.sequence(A.call(self.rotate),A.call(self.movecollidewithcar)),-1))
#self.movecard()
#self.movecard()
#self.movecard()
#self.rotate()
#self.movecard()
#self.rotate()
if self.t>=1:
self.rotate()
self.movecollidewithcar()
self.rotate2()
self.movecollidewithcar2()
self.t=0

def touch_began(self, touch):
pass

def touch_moved(self, touch):
pass

def touch_ended(self, touch):
pass

if name == ‘__main__’:
run(MyScene(), show_fps=True)

退出移动版