14
2024
11

python编程学习-where-my-ice-cream

学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。

可以在线运行,效果如下(在新页面中打开链接):

代码如下(用到平台自定义的xdf库):

from xdf import *
import math
x0 = 0
y0 = 0
x = 0
y = 0
W = 768
H = 1024
G = 1.6
JUMPF = -20
MAX_SPEED = 10
MAX_SPEED_Y = 20
ACC = 6
levels = [
  ["I",866,630,"P",150,625,"C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",475,675,"C",525,675,"C",575,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",875,675,"C",925,675,"C",975,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,725,"C",375,725,"C",425,725,"C",475,725,"C",525,725,"C",575,725,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",325,775,"C",375,775,"C",425,775,"C",475,775,"C",525,775,"C",575,775,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,675,"C",1025,725,"C",1025,775],
  ["I",866,630,"P",150,625,"C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",475,675,"C",525,675,"C",575,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",875,675,"C",925,675,"C",975,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,725,"C",375,725,"C",425,725,"C",475,725,"C",525,725,"C",575,725,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",325,775,"C",375,775,"C",425,775,"C",475,775,"C",525,775,"C",575,775,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,675,"C",1025,725,"C",1025,775,"C",525,525,"C",525,575,"C",525,625,"C",525,475],
  ["I",434,430,"P",150,625,"C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",875,675,"C",925,675,"C",975,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,725,"C",375,725,"C",425,725,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",325,775,"C",375,775,"C",425,775,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,675,"C",1025,725,"C",1025,775,"C",375,475,"C",425,475,"C",475,475,"C",525,475,"C",575,475,"C",625,475],
  ["I",226,430,"P",150,625,"C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",875,675,"C",925,675,"C",975,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,725,"C",375,725,"C",425,725,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",325,775,"C",375,775,"C",425,775,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,675,"C",1025,725,"C",1025,775,"C",75,475,"C",125,475,"C",175,475,"C",225,475,"C",475,675,"C",475,725,"C",475,775,"C",625,475,"C",675,475,"C",725,475,"C",775,475,"C",825,475,"C",875,475,"C",925,475,"C",975,475],
  ["I",325,425,"P",150,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,675,"C",375,675,"C",425,675,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",525,675,"C",575,675,"C",625,675,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,725,"C",1025,775,"C",75,475,"C",125,475,"C",175,475,"C",225,475,"C",475,675,"C",675,675,"C",725,475,"C",775,475,"C",825,475,"C",875,475,"C",925,475,"C",975,475,"C",425,575,"C",525,575,"C",575,575,"C",625,575,"C",475,575,"C",375,625,"C",375,575,"C",375,525,"C",375,475,"C",375,425,"C",375,375,"C",375,325,"C",275,475,"C",325,475,"C",575,375,"C",625,375,"C",1025,475,"C",325,325,"C",275,375,"C",75,425,"C",75,375,"C",75,325,"C",75,275,"H",725,475,0],
  ["I",925,425,"P",150,625,"C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",475,675,"C",525,675,"C",575,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,725,"C",375,725,"C",425,725,"C",475,725,"C",525,725,"C",575,725,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",325,775,"C",375,775,"C",425,775,"C",475,775,"C",525,775,"C",575,775,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",1025,675,"C",1025,725,"C",1025,775,"C",25,625,"C",25,575,"C",25,525,"C",25,475,"C",25,425,"C",25,375,"C",25,325,"C",25,275,"C",25,25,"C",25,75,"C",25,125,"C",25,175,"C",25,225,"C",875,675,"C",875,725,"C",875,775,"C",1025,625,"C",1025,575,"C",1025,525,"C",1025,475,"C",1025,425,"C",1025,375,"C",1025,325,"C",1025,275,"C",1025,25,"C",1025,75,"C",1025,125,"C",1025,175,"C",1025,225,"C",825,425,"C",825,375,"C",825,325,"C",825,275,"C",825,25,"C",825,75,"C",825,125,"C",825,175,"C",825,225,"C",875,425,"C",975,325,"C",875,475,"C",925,475,"C",975,475,"C",675,625,"C",675,575,"C",675,525,"C",675,475,"C",775,625,"C",825,625,"C",475,575,"C",525,575,"C",475,625,"C",525,625,"C",175,475,"C",225,475,"C",275,475,"C",325,475,"C",175,425,"C",475,375,"C",525,375,"C",575,375,"H",575,375,0],
  ["Z",15,753,"Z",75,753,"Z",135,753,"Z",195,753,"Z",255,753,"Z",315,753,"Z",375,753,"Z",435,753,"Z",495,753,"Z",555,753,"Z",615,753,"Z",675,753,"Z",735,753,"Z",795,753,"Z",855,753,"Z",915,753,"Z",975,753,"Z",1035,753,"P",175,625,"C",775,525,"C",825,525,"C",325,625,"C",375,625,"C",625,625,"C",675,625,"C",75,475,"C",125,475,"C",475,625,"C",525,625,"C",125,675,"C",175,675,"C",225,675,"I",100,425,"C",898,425,"C",948,425,"C",875,425,"C",925,425,"H",525,625,0,"H",825,525,0],
  ["Z",15,753,"Z",75,753,"Z",135,753,"Z",195,753,"Z",255,753,"Z",315,753,"Z",375,753,"Z",435,753,"Z",495,753,"Z",555,753,"Z",615,753,"Z",675,753,"Z",735,753,"Z",795,753,"Z",855,753,"Z",915,753,"Z",975,753,"Z",1035,753,"P",500,625,"C",775,475,"C",825,475,"I",775,325,"C",525,675,"C",575,675,"C",625,675,"C",375,675,"C",425,675,"C",475,675,"C",925,625,"C",975,625,"C",1025,625,"C",775,625,"C",825,625,"C",875,625,"C",175,525,"C",225,525,"C",275,525,"C",25,525,"C",75,525,"C",125,525,"C",325,425,"C",375,425,"C",175,425,"C",225,425,"C",275,425,"C",675,375,"C",725,375,"C",775,375,"C",525,375,"C",575,375,"C",625,375,"C",825,375,"H",775,625,180,"H",525,375,0,"H",175,425,0],
  ["Z",15,753,"Z",75,753,"Z",135,753,"Z",195,753,"Z",255,753,"Z",315,753,"Z",375,753,"Z",435,753,"Z",495,753,"Z",555,753,"Z",615,753,"Z",675,753,"P",500,625,"I",75,325,"C",525,675,"C",575,675,"C",625,675,"C",375,675,"C",425,675,"C",475,675,"C",875,475,"C",925,475,"C",975,475,"C",725,475,"C",775,475,"C",825,475,"C",1025,475,"C",475,425,"C",525,425,"C",575,425,"C",325,425,"C",375,425,"C",425,425,"C",175,375,"C",225,375,"C",25,375,"C",75,375,"C",125,375,"C",225,475,"C",75,475,"C",125,475,"C",175,475,"C",25,325,"C",25,275,"C",25,225,"C",25,175,"C",25,125,"C",25,75,"C",25,25,"H",725,475,89,"H",325,425,180,"H",225,375,0],
  ["Z",550,753,"P",325,625,"I",75,625,"Z",910,753,"Z",970,753,"C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,725,"C",375,725,"C",425,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",325,775,"C",375,775,"C",425,775,"C",475,675,"C",475,725,"C",475,775,"C",625,675,"C",675,675,"C",625,725,"C",675,725,"C",625,775,"C",675,775,"C",725,675,"C",725,725,"C",725,775,"C",25,625,"C",25,575,"C",25,525,"C",25,475,"C",25,425,"C",25,375,"C",25,325,"C",25,275,"C",25,25,"C",25,75,"C",25,125,"C",25,175,"C",25,225,"C",1025,675,"C",1025,725,"C",1025,775,"C",1025,625,"C",1025,575,"C",1025,525,"C",1025,475,"C",1025,425,"C",1025,375,"C",1025,325,"C",1025,275,"C",1025,25,"C",1025,75,"C",1025,125,"C",1025,175,"C",1025,225,"C",775,475,"C",825,475,"C",875,475,"C",925,475,"C",975,475,"C",375,475,"C",425,475,"C",475,475,"C",525,475,"C",575,475,"C",225,625,"C",225,575,"C",225,525,"C",225,475,"H",225,475,0,"H",225,475,180,"H",775,478,89,"H",775,478,-91],
  ["C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",475,675,"C",525,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",875,675,"C",925,675,"C",975,675,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,675,"C",1025,725,"C",1025,775,"P",675,425,"I",475,425,"C",325,475,"C",375,475,"C",425,475,"C",475,475,"C",525,475,"C",575,475,"C",625,475,"C",675,475,"C",725,475,"C",775,475,"C",825,475,"C",875,475,"C",925,475,"C",975,475,"C",1025,475,"C",325,325,"C",325,375,"C",325,425,"C",325,275,"C",375,275,"C",425,275,"C",475,275,"C",525,275,"C",625,275,"C",675,275,"C",725,275,"C",775,275,"C",825,275,"C",875,275,"C",625,325,"C",625,375,"C",625,425,"C",525,325,"C",525,375,"H",475,675,0,"H",125,675,0,"H",275,700,180,"H",875,475,0],
  ["P",500,250,"I",200,475,"Z",15,753,"Z",75,753,"Z",135,753,"Z",195,753,"Z",255,753,"Z",315,753,"Z",375,753,"Z",615,753,"Z",675,753,"Z",735,753,"Z",795,753,"Z",855,753,"Z",915,753,"Z",975,753,"Z",1035,753,"C",625,525,"C",675,525,"C",725,525,"C",175,525,"C",225,525,"C",75,475,"C",125,475,"C",275,475,"C",325,475,"C",775,575,"C",825,575,"C",875,525,"C",925,525,"H",825,575,0,"H",125,475,0,"H",325,475,180,"H",325,475,0],
  ["I",725,625,"P",425,425,"C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",475,675,"C",525,675,"C",575,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",875,675,"C",925,675,"C",975,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,725,"C",375,725,"C",425,725,"C",475,725,"C",525,725,"C",575,725,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",325,775,"C",375,775,"C",425,775,"C",475,775,"C",525,775,"C",575,775,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,675,"C",1025,725,"C",1025,775,"C",375,475,"C",425,475,"C",475,475,"C",525,475,"C",575,475,"C",625,475,"C",675,475,"C",725,475,"C",775,475,"C",825,475,"C",875,425,"C",925,425,"C",975,425,"C",1025,425,"C",475,525,"C",475,575,"C",475,625,"C",25,475,"C",75,475,"C",125,475,"C",175,475,"C",225,475,"C",375,425,"C",375,375,"C",375,325,"C",375,275,"C",375,225,"H",775,475,-61,"H",775,475,180,"H",175,475,0,"H",875,425,0],
  ["C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",475,675,"C",525,675,"C",575,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",875,675,"C",925,675,"C",975,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,725,"C",375,725,"C",425,725,"C",475,725,"C",525,725,"C",575,725,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",325,775,"C",375,775,"C",425,775,"C",475,775,"C",525,775,"C",575,775,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,675,"C",1025,725,"C",1025,775,"I",75,525,"P",100,325,"C",25,625,"C",25,575,"C",25,525,"C",25,475,"C",25,425,"C",25,375,"C",25,325,"C",25,275,"C",25,25,"C",25,75,"C",25,125,"C",25,175,"C",25,225,"C",1025,625,"C",1025,575,"C",1025,525,"C",1025,475,"C",1025,425,"C",1025,375,"C",1025,325,"C",1025,275,"C",1025,25,"C",1025,75,"C",1025,125,"C",1025,175,"C",1025,225,"C",375,575,"C",425,575,"C",475,575,"C",525,575,"C",575,575,"C",625,575,"C",675,575,"C",725,575,"C",775,575,"C",825,575,"C",875,575,"C",925,575,"C",975,575,"C",75,575,"C",125,575,"C",175,575,"C",75,375,"C",125,375,"C",175,375,"C",225,375,"C",275,375,"C",325,375,"C",375,375,"C",425,375,"C",475,375,"C",525,375,"C",575,375,"C",625,375,"C",675,375,"C",725,375,"C",775,375,"C",825,375,"C",975,375,"H",776,378,180,"H",776,378,60,"H",975,375,-90,"H",625,575,0,"H",175,575,0,"H",275,375,180,"H",375,375,0],
  ["C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",425,675,"C",475,675,"C",525,675,"C",575,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",875,675,"C",925,675,"C",975,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",425,725,"C",475,725,"C",525,725,"C",575,725,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",425,775,"C",475,775,"C",525,775,"C",575,775,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,675,"C",1025,725,"C",1025,775,"I",575,325,"P",625,625,"C",25,525,"C",75,525,"C",125,525,"C",175,525,"C",225,525,"C",275,525,"C",325,525,"C",375,525,"C",425,525,"C",425,575,"C",425,625,"C",425,375,"C",425,425,"C",425,475,"C",575,425,"C",625,425,"C",675,425,"C",725,425,"C",775,425,"C",775,525,"C",825,525,"C",875,525,"C",925,525,"C",975,525,"C",1025,525,"C",575,375,"C",625,375,"H",125,525,0,"H",425,375,0,"H",775,425,89],
  ["I",625,625,"P",75,625,"C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",475,675,"C",525,675,"C",575,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",875,675,"C",925,675,"C",975,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,725,"C",375,725,"C",425,725,"C",475,725,"C",525,725,"C",575,725,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",325,775,"C",375,775,"C",425,775,"C",475,775,"C",525,775,"C",575,775,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,675,"C",1025,725,"C",1025,775,"C",225,575,"C",275,575,"C",175,625,"C",225,625,"C",275,625,"C",325,625,"C",425,425,"C",475,425,"C",525,425,"C",575,425,"C",625,425,"C",825,375,"C",875,375,"C",925,375,"C",975,375,"C",1025,375,"C",75,375,"C",125,375,"C",175,375,"C",325,475,"C",375,475,"C",825,525,"C",825,575,"C",825,625,"C",825,425,"C",825,475,"C",475,525,"C",475,575,"C",475,625,"C",475,475,"H",125,375,0,"H",375,475,0,"H",475,425,0,"H",625,425,180,"H",825,475,180],
  ["C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",475,675,"C",525,675,"C",575,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",875,675,"C",925,675,"C",975,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,725,"C",375,725,"C",425,725,"C",475,725,"C",525,725,"C",575,725,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",325,775,"C",375,775,"C",425,775,"C",475,775,"C",525,775,"C",575,775,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,675,"C",1025,725,"C",1025,775,"I",150,625,"P",125,325,"C",25,575,"C",75,575,"C",25,625,"C",75,625,"C",825,575,"C",875,575,"C",925,575,"C",975,575,"C",825,625,"C",875,625,"C",925,625,"C",975,625,"C",1025,575,"C",1025,625,"C",775,375,"C",825,375,"C",875,375,"C",925,375,"C",975,375,"C",1025,375,"C",175,475,"C",225,475,"C",275,475,"C",325,475,"C",375,475,"C",425,475,"C",475,475,"C",25,375,"C",75,375,"C",125,375,"C",175,375,"C",425,425,"C",475,425,"C",525,425,"C",575,375,"C",625,375,"C",473,525,"C",473,575,"C",473,625,"C",175,425,"C",25,225,"C",25,275,"C",25,325,"C",25,125,"C",25,175,"C",25,25,"C",25,75,"C",1025,225,"C",1025,275,"C",1025,325,"C",1025,125,"C",1025,175,"C",1025,25,"C",1025,75,"H",350,475,0,"H",500,425,0,"H",600,375,180,"H",825,375,180],
  ["C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",475,675,"C",525,675,"C",575,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",875,675,"C",925,675,"C",975,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,725,"C",375,725,"C",425,725,"C",475,725,"C",525,725,"C",575,725,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",325,775,"C",375,775,"C",425,775,"C",475,775,"C",525,775,"C",575,775,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,675,"C",1025,725,"C",1025,775,"I",675,625,"P",100,475,"C",475,575,"C",525,575,"C",475,625,"C",525,625,"C",25,525,"C",75,525,"C",125,525,"C",175,525,"C",225,525,"C",275,525,"C",325,525,"C",325,425,"C",375,425,"C",425,425,"C",475,425,"C",525,425,"C",575,425,"C",625,425,"C",675,425,"C",725,425,"C",325,475,"C",775,325,"C",825,325,"C",875,325,"C",925,325,"C",975,325,"C",1025,325,"C",875,475,"C",925,475,"C",975,475,"C",1025,475,"C",1025,375,"C",1025,425,"C",25,375,"C",25,425,"C",25,475,"C",25,225,"C",25,275,"C",25,325,"C",25,75,"C",25,125,"C",25,175,"C",25,25,"C",1020,225,"C",1020,275,"C",1020,75,"C",1020,125,"C",1020,175,"C",1020,25,"C",725,625,"C",725,475,"C",725,525,"C",725,575,"H",775,325,29,"H",575,425,0,"H",325,425,29,"H",275,525,0],
  ["C",25,675,"C",75,675,"C",125,675,"C",175,675,"C",225,675,"C",275,675,"C",325,675,"C",375,675,"C",425,675,"C",475,675,"C",525,675,"C",575,675,"C",625,675,"C",675,675,"C",725,675,"C",775,675,"C",825,675,"C",875,675,"C",925,675,"C",975,675,"C",25,725,"C",75,725,"C",125,725,"C",175,725,"C",225,725,"C",275,725,"C",325,725,"C",375,725,"C",425,725,"C",475,725,"C",525,725,"C",575,725,"C",625,725,"C",675,725,"C",725,725,"C",775,725,"C",825,725,"C",875,725,"C",925,725,"C",975,725,"C",25,775,"C",75,775,"C",125,775,"C",175,775,"C",225,775,"C",275,775,"C",325,775,"C",375,775,"C",425,775,"C",475,775,"C",525,775,"C",575,775,"C",625,775,"C",675,775,"C",725,775,"C",775,775,"C",825,775,"C",875,775,"C",925,775,"C",975,775,"C",1025,675,"C",1025,725,"C",1025,775,"C",25,525,"C",25,375,"C",25,425,"C",25,475,"C",25,225,"C",25,275,"C",25,325,"C",25,75,"C",25,125,"C",25,175,"C",25,25,"C",25,575,"C",25,625,"C",1025,525,"C",1025,375,"C",1025,425,"C",1025,475,"C",1025,225,"C",1025,275,"C",1025,325,"C",1025,75,"C",1025,125,"C",1025,175,"C",1025,25,"C",1025,575,"C",1025,625,"C",325,575,"C",375,575,"C",325,625,"C",375,625,"I",75,625,"P",350,325,"C",75,375,"C",125,375,"C",175,375,"C",225,375,"C",275,375,"C",325,375,"C",375,375,"C",425,375,"C",475,375,"C",525,375,"C",575,375,"C",625,375,"C",675,375,"C",725,375,"C",875,375,"C",925,375,"C",975,375,"C",775,525,"C",825,525,"C",875,525,"C",525,625,"C",575,625,"H",375,575,0,"H",575,375,89,"H",725,375,0,"H",825,525,0,"H",925,375,0,"H",125,375,0,"H",125,375,180,"H",125,375,-90,"H",125,375,89],
  ["C",175,325,"C",225,325,"C",175,375,"C",225,375,"I",175,625,"P",500,525,"C",725,575,"C",775,575,"C",725,625,"C",775,625,"C",175,675,"C",225,675,"C",275,675,"H",203,353,0,"H",203,353,180,"H",203,353,-90,"H",203,353,89,"H",753,603,0,"H",753,603,180,"H",753,603,-90,"H",753,603,89]
]


class HPoint:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def setXy(self, x, y):
        self.x = x
        self.y = y

    def add(self, p):
        return HPoint(self.x + p.x, self.y + p.y)

    def subtract(self, p):
        return HPoint(self.x - p.x, self.y - p.y)

    def middle(self, p):
        return HPoint((self.x + p.x) / 2, (self.y + p.y) / 2)

    def length(self):
        return math.sqrt(self.x * self.x + self.y * self.y)

    def normalize(self, n):
        a = n / self.length()
        return HPoint(self.x * a, self.y * a)

    def dot(self, p):
        return self.x * p.x + self.y * p.y

# 分离轴 碰撞检测
class Sat:
    def __init__(self):
        pass
    def test(self, points0, points1):
        return self.halfTest(points0, points1) and self.halfTest(points1, points0)
    def halfTest(self, points0, points1):
        n = len(points0)
        for i in range(n):
            axis = self.getAxis(points0[i], points0[(i + 1) % n])
            range1 = self.getRange(axis[0], axis[1], points0)
            range2 = self.getRange(axis[0], axis[1], points1)
            if not self.intersection(range1[0], range1[1], range2[0], range2[1]):
                return False
        return True
    def getAxis(self, p0, p1):
        p2 = HPoint((p0.x + p1.x) / 2, (p0.y + p1.y) / 2)
        p3 = HPoint(-(p0.y - p1.y), p0.x - p1.x)
        p3 = p3.normalize(1)
        return [p2, p2.add(p3)]
    def getRange(self, p0, p1, points):
        max = None
        min = None
        v = p1.subtract(p0)
        for p in points:
            n = v.dot(p.subtract(p0))
            if max == None or n > max:
                max = n
            if min == None or n < min:
                min = n
        return [min, max]
    def intersection(self, n0, n1, n2, n3):
        if n1 < n2 or n3 < n0:
            return False
        return True
    pass
Sat = Sat()
class Sprite:
    def __init__(self, src, x = 0, y = 0, name = ''):
        self.stamp = stamp(src, x, y)
        self.src = src
        self.name = name
        self.x = x
        self.y = y
        self.speedY = 0
        self.speedX = 0
        self.visible = True
        self.removed = False
        self.isStand = False
        self.flipped = False
        self.lastFlipped = False
        self.rotation = 0
        pass
    def change(self, src):
        if src != self.src:
            self.src = src
            self.stamp.change(src)
    def reset(self):
        pass
    def front(self):
        self.stamp.front()
        pass
    def update(self):
        pass
    def refresh(self, parent):
        if self.flipped != self.lastFlipped:
            self.lastFlipped = self.flipped
            self.stamp.flip()
        if self.removed:
            if not self.stamp.hidden:
                self.stamp.hide()
            return
        if self.visible and parent.visible:
            if self.stamp.hidden:
                self.stamp.show()
        else:
            if not self.stamp.hidden:
                self.stamp.hide()
        if not self.stamp.hidden:
            self.stamp.rotate(self.rotation + 90)
            self.stamp.move(768 - (parent.y + self.y), parent.x + self.x)
        pass
    def tap(self):
        if self.visible and self.stamp.hits(x, y):
            return True
        return False
    pass

class Layer:
    def __init__(self, x = 0, y = 0):
        self.x = x
        self.y = y
        self.children = []
        self.pool = []
        self.visible = True
        pass
    def addSp(self, src, x = 0, y = 0, name = ''):
        for sp in self.pool:
            if sp.src == src:
                self.pool.remove(sp)
                self.children.append(sp)
                sp.x = x
                sp.y = y
                sp.name = name
                if sp.src != src:
                    sp.stamp.change(src)
                sp.removed = False
                sp.visible = True
                sp.speedY = 0
                sp.speedX = 0
                sp.front()
                return sp
        sp = Sprite(src, x, y, name)
        self.children.append(sp)
        return sp
    def clear(self):
        self.pool.extend(self.children)
        self.children.clear()
        pass
    def removeSp(self, sp):
        self.pool.append(sp)
        self.children.remove(sp)
        sp.removed = True
    def update(self):
        pass
    def refresh(self):
        for sp in self.children:
            sp.refresh(self)
        for sp in self.pool:
            sp.removed = True
            sp.refresh(self)
        pass
    def reset(self):
        pass
    def tap(self):
        if self.visible:
            for sp in reversed(self.children):
                if sp.visible and sp.tap():
                    return True
        return False
    pass

class Home(Layer):
    def __init__(self):
        Layer.__init__(self, 0, 0)
        pass
    def reset(self):
        fill("ice-c-bg1@004l")
        pass
    def tap(self):
        if abs(x0 - 505) < 125 and abs(y0 - 505) < 35:
            game.showMenu()
            return True
        return False
    pass
class LevelSelectMenu(Layer):
    def __init__(self):
        Layer.__init__(self, 0, 0)
        self.locks = []
        for j in range(4):
            for i in range(5):
                self.locks.append(self.addSp("ice-c-l@005M", 280+120*i, 320+86*j))
        pass
    def reset(self):
        fill("ce-c-bg2@004m")
        pass
    def updateRecords(self, records):
        for i in range(len(self.locks)):
            self.locks[i].visible = i > records
        pass
    def update(self):
        super().update()
        pass
    def refresh(self):
        super().refresh()
        pass
    def tap(self):
        for i in range(len(self.locks)):
            sp = self.locks[i]
            if not sp.visible:
                x1 = x0 - self.x - sp.x
                y1 = y0 - self.y - sp.y
                if abs(x1) < 35 and abs(y1) < 35:
                    game.startLevel(i)
                    return True
            else:
                break
        return False
    pass

class Gamelayer(Layer):
    def __init__(self):
        Layer.__init__(self,0,0)
        self.pool = []
        self.penguin = None
        self.menu = None
        self.count = 0
        self.state = 0
        pass
    def reset(self):
        fill("ice-c-bg@004k")
        pass
    def tap(self):
        if self.state != 0:
            return False
        for sp in self.children:
            if not sp.visible:
                continue
            dx = x0 - sp.x
            dy = y0 - sp.y
            if sp.name == 'M':
                if abs(dx) < 30 and abs(dy) < 30:
                    game.showMenu()
                    return True
        #self.jumpPenguin()
        return False
    def jumpPenguin(self):
        if not self.penguin.isStand:
            return
        self.penguin.speedY = JUMPF
        self.penguin.isStand = False
        pass
    def update(self):
        super().update()
        if not self.penguin:
            return
        if self.state == 0:
            self.updateHammer()
            self.movePenguin()
        if self.state == 0:
            self.checkHit()
        elif self.state == 1:
            self.count += 1
            if self.count == 10:
                game.complete()
        elif self.state == 2:
            self.count += 1
            if self.count == 10:
                game.gameOver()
        pass
    def updateHammer(self):
        for sp in self.children:
            if sp.name == 'H':
                sp.rotation += 2
        pass
    def movePenguin(self):
        penguin = self.penguin
        if isKeydown('39'):
            self.penguin.speedX += ACC
        if isKeydown('37'):
            self.penguin.speedX -= ACC
            pass
        if isKeydown('38') or isKeydown('32'):
            self.jumpPenguin()
        if penguin.speedX > MAX_SPEED:
            penguin.speedX = MAX_SPEED
        elif penguin.speedX < -MAX_SPEED:
            penguin.speedX = -MAX_SPEED
        # move x
        penguin.x += penguin.speedX
        self.formatPos()
        self.checkHitFloorX()
        penguin.speedX *= 0.7
        # move y
        penguin.speedY += G
        if penguin.speedY > MAX_SPEED_Y:
            penguin.speedY = MAX_SPEED_Y
        penguin.y += penguin.speedY
        penguin.isStand = False
        self.formatPos()
        self.checkHitFloorY()
        if penguin.isStand:
            if abs(penguin.speedX) < 0.1:
                penguin.speedX = 0
                penguin.change("ice-c-p1@004u")
            else:
                penguin.change("ice-c-pr@005K")
        else:
            penguin.change("ice-c-p16@0059")
        if penguin.speedX < 0:
            penguin.flipped = True
        elif penguin.speedX > 0:
            penguin.flipped = False
        pass
    def checkHitFloorX(self):
        penguin = self.penguin
        for sp in self.children:
            if sp.name == "C":
                dx = penguin.x - sp.x
                dy = penguin.y - sp.y
                if abs(dx) < 48 and abs(dy) < 48:
                    penguin.x = sp.x + 48 * abs(dx) / dx
                    self.formatPos()
                    return
        pass
    def checkHitFloorY(self):
        penguin = self.penguin
        for sp in self.children:
            if sp.name == "C":
                dy = penguin.y - sp.y
                dx = penguin.x - sp.x
                if abs(dx) < 48 and abs(dy) < 48:
                    penguin.y = sp.y + 48 * abs(dy) / dy
                    if dy < 0:
                        penguin.isStand = True
                    penguin.speedY = 0
                    self.formatPos()
    def formatPos(self):
        penguin = self.penguin
        if penguin.x < 0:
            penguin.x += 1024
        elif penguin.x > 1024:
            penguin.x -= 1024
        if penguin.y < 0:
            penguin.y += 768
        elif penguin.y > 768:
            penguin.y -= 768
        pass
    def checkHit(self):
        penguin = self.penguin
        for sp in self.children:
            if sp.name == "C" or sp.name == "P" or sp.name == "M":
                continue
            dx = sp.x - penguin.x
            dy = sp.y - penguin.y
            if sp.name == "Z":
                if abs(dx) < 55 and abs(dy) < 40:
                    return self.fail()
            elif sp.name == "I":
                if abs(dx) < 50 and abs(dy) < 50:
                    return self.complete()
            elif sp.name == "H":
                if self.hitHammer(sp):
                    return self.fail()
        pass
    def hitHammer(self, hm):
        penguin = self.penguin
        points1 = [
            HPoint(penguin.x - 25, penguin.y - 25),
            HPoint(penguin.x + 25, penguin.y - 25),
            HPoint(penguin.x + 25, penguin.y + 25),
            HPoint(penguin.x - 25, penguin.y + 25)
        ]
        ang = hm.rotation * math.pi / 180
        c = math.cos(ang)
        s = math.sin(ang)
        ps2 = [
            [-25, 65],
            [25, 65],
            [25, 100],
            [-25, 100],
            [-5, 0],
            [5, 0],
            [5, 65],
            [-5, 65]
        ]
        points2 = []
        for a in ps2:
            p = HPoint(hm.x + c * a[0] - s * a[1], 
                       hm.y + s * a[0] + c * a[1])
            points2.append(p)
        return Sat.test(points1, points2[:4]) or Sat.test(points1, points2[4:])
    def fail(self):
        self.state = 2
    def complete(self):
        self.state = 1
        pass
    def start(self, arr):
        self.initLevel(arr)
        pass
    def initLevel(self, arr):
        #fill("ice-c-bg@004k")
        self.penguin = None
        self.icecream = None
        self.menu = None
        self.count = 0
        self.state = 0
        self.clear()
        i = 0
        names = {
            "H": "ice-c-hm@004p",
            "P": "ice-c-p1@004u",
            "I": "ice-c-i@004o",
            "C": "ice-c-c@005N",
            "Z": "ice-c-zhu@004s"
        }
        while i < len(arr):
            s = arr[i]
            x = arr[i+1]
            y = arr[i+2]
            i+=3
            sp = self.addSp(names[s], x, y, s)
            if s == 'H':
                sp.rotation = arr[i]
                i+=1
            elif s == 'P':
                self.penguin = sp
            elif s == 'I':
                self.icecream = sp
        self.menu = self.addSp("ice-c-home@004q", 1024-50, 50, "M")
        self.icecream.front()
        self.penguin.front()
        self.menu.front()
        
        pass
    pass

class Game:
    def __init__(self):
        self.homeLayer = Home()
        self.menuLayer = LevelSelectMenu()
        self.gameLayer = Gamelayer()
        self.layers = [self.homeLayer, self.menuLayer, self.gameLayer]
        self.maxLevel = 0
        self.level = 0
        self.goHome()
        pass
    def hideAll(self):
        for layer in self.layers:
            layer.visible = False
        pass
    def update(self):
        for layer in self.layers:
            layer.update()
        pass
    def refresh(self):
        for layer in self.layers:
            layer.refresh()
        pass
    def tap(self):
        for layer in self.layers:
            if layer.visible and layer.tap():
                break
        pass
    def complete(self):
        self.nextLevel()
        pass
    def showMenu(self):
        self.hideAll()
        self.menuLayer.reset()
        self.menuLayer.updateRecords(self.maxLevel)
        self.menuLayer.visible = True
        pass
    def gameOver(self):
        self.restart()
        pass
    def startLevel(self, level):
        self.level = level
        self.start()
        pass
    def goHome(self):
        self.hideAll()
        self.homeLayer.reset()
        self.homeLayer.visible = True
        pass
    def nextLevel(self):
        if self.level < 20 - 1:
            self.level += 1
        self.maxLevel = max(self.maxLevel, self.level)
        self.start()
        pass
    def start(self):
        self.hideAll()
        self.gameLayer.reset()
        self.gameLayer.visible = True
        self.gameLayer.start(levels[self.level])
        pass
    def restart(self):
        self.start()
        pass
    pass

game = Game()
keys = {"37": False, "38": False, "39": False, "32": False}

def tap():
    updateMouse()
    game.tap()
    pass

def loop():
    updateMouse()
    game.update()
    game.refresh()
    pass

def updateMouse():
    global x0,y0,x,y
    x0 = y
    y0 = 768 - x

def isKeydown(code):
    return keys[code] or yaogan.isKeyDown(code)
def keydown(code, key):
    keys[str(code)] = True
def keyup(code, key):
    keys[str(code)] = False

class Yaogan:
    def __init__(self, w = 768):
        self.w = w
        self.touches = []
        self.touch0 = [0, 0, 0, 0]
        self.touch1 = [0, 0, 0, 0]
        self.keys = {"37": False, "38": False, "39": False, "40": False, "32": False}
        self.loop()
    def touch(self, x, y):
        if x < self.w / 2:
            self.touch0 = [x, y, x, y]
        else:
            self.touch1 = [x, y, x, y]
    def touching(self, x, y):
        if x < self.w / 2:
            self.touch0[2] = x
            self.touch0[3] = y
        else:
            self.touch1[2] = x
            self.touch1[3] = y
    def untouch(self, x, y):
        if x < self.w / 2:
            self.touch0 = [0, 0, 0, 0]
        else:
            self.touch1 = [0, 0, 0, 0]
    def loop(self):
        dx = self.touch0[2] - self.touch0[0]
        dy = self.touch0[3] - self.touch0[1]
        self.keys["37"] = dx < -20
        self.keys["39"] = dx > 20
        self.keys["38"] = dy < -20
        self.keys["40"] = dy > 20
        self.keys["32"] = self.touch1[0] != 0
        delay(self.loop, 50)
    def isKeyDown(self, key):
        return self.keys[key]
    pass

yaogan = Yaogan(1024)
def touch():
    yaogan.touch(y, 768-x)
def untouch():
    yaogan.untouch(y, 768-x)
def touching():
    yaogan.touching(y, 768-x)



也可以下载代码,本地python环境运行(用pygame封装了xdf库)。



« 上一篇下一篇 »

相关文章:

python编程学习-华容道  (2024-11-14 16:43:25)

python编程学习-动画  (2024-11-14 16:39:37)

python编程学习-别踩白块  (2024-11-14 16:38:59)

python编程学习-俄罗斯方块  (2024-11-14 16:38:19)

python编程学习-井字棋  (2024-11-14 16:34:9)

python编程学习-五子棋  (2024-11-14 16:33:23)

python编程学习-乒乓  (2024-11-14 16:32:46)

python编程学习-一笔画关卡生成  (2024-11-14 16:32:6)

python编程学习-一笔画  (2024-11-14 16:31:27)

python编程学习-woblox  (2024-11-14 16:30:47)

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。