做过前端的兄弟应该都知道css sprite(也称css精灵),这是一种常用的减少http请求次数的优化手段。把很多小图拼成一张大图,只加载1次,然后用css定位到不区的区域,从而展示不同的图片。游戏中也是类似的道理,如下图:

pygame-KidsCanCode系列jumpy-part9-使用spritesheet

每一个小图片,都有自己的x,y,width,height信息,通常会放在一个xml中,类似:

  1 <TextureAtlas imagePath="sprites.png">
  2     <SubTexture name="bronze_1.png" x="707" y="296" width="84" height="84"/>
  3     <SubTexture name="bronze_2.png" x="826" y="206" width="66" height="84"/>
  4     <SubTexture name="bronze_3.png" x="899" y="116" width="50" height="84"/>
  5     <SubTexture name="bronze_4.png" x="670" y="406" width="14" height="84"/>
  6     <SubTexture name="bubble.png" x="0" y="1662" width="211" height="215"/>
  7     <SubTexture name="bunny1_hurt.png" x="382" y="946" width="150" height="174"/>
  8     <SubTexture name="bunny1_jump.png" x="382" y="763" width="150" height="181"/>
  9     <SubTexture name="bunny1_ready.png" x="614" y="1063" width="120" height="191"/>
 10     <SubTexture name="bunny1_stand.png" x="690" y="406" width="120" height="201"/>
 11     <SubTexture name="bunny1_walk1.png" x="678" y="860" width="120" height="201"/>
 12     <SubTexture name="bunny1_walk2.png" x="692" y="1458" width="120" height="207"/>
 13     <SubTexture name="bunny2_hurt.png" x="411" y="1866" width="150" height="174"/>
 14     <SubTexture name="bunny2_jump.png" x="416" y="1660" width="150" height="181"/>
 15     <SubTexture name="bunny2_ready.png" x="581" y="1265" width="121" height="191"/>
 16     <SubTexture name="bunny2_stand.png" x="584" y="0" width="121" height="201"/>
 17     <SubTexture name="bunny2_walk1.png" x="584" y="203" width="121" height="201"/>
 18     <SubTexture name="bunny2_walk2.png" x="678" y="651" width="121" height="207"/>
 19     <SubTexture name="cactus.png" x="707" y="134" width="117" height="160"/>
 20     <SubTexture name="carrot.png" x="820" y="1733" width="78" height="70"/>
 21     <SubTexture name="carrot_gold.png" x="814" y="1661" width="78" height="70"/>
 22     <SubTexture name="carrots.png" x="812" y="554" width="54" height="49"/>
 23     <SubTexture name="cloud.png" x="0" y="1152" width="260" height="134"/>
 24     <SubTexture name="coin_bronze.png" x="329" y="1390" width="60" height="61"/>
 25     <SubTexture name="coin_gold.png" x="244" y="1981" width="61" height="61"/>
 26     <SubTexture name="coin_silver.png" x="307" y="1981" width="61" height="61"/>
 27     <SubTexture name="flame.png" x="900" y="1733" width="41" height="80"/>
 28     <SubTexture name="flyMan_fly.png" x="566" y="510" width="122" height="139"/>
 29     <SubTexture name="flyMan_jump.png" x="568" y="1534" width="122" height="135"/>
 30     <SubTexture name="flyMan_stand.png" x="568" y="1671" width="122" height="139"/>
 31     <SubTexture name="flyMan_still_fly.png" x="692" y="1667" width="120" height="132"/>
 32     <SubTexture name="flyMan_still_jump.png" x="698" y="1801" width="120" height="128"/>
 33     <SubTexture name="flyMan_still_stand.png" x="707" y="0" width="120" height="132"/>
 34     <SubTexture name="gold_1.png" x="698" y="1931" width="84" height="84"/>
 35     <SubTexture name="gold_2.png" x="829" y="0" width="66" height="84"/>
 36     <SubTexture name="gold_3.png" x="897" y="1574" width="50" height="84"/>
 37     <SubTexture name="gold_4.png" x="645" y="651" width="15" height="84"/>
 38     <SubTexture name="grass1.png" x="868" y="1877" width="58" height="57"/>
 39     <SubTexture name="grass2.png" x="784" y="1931" width="82" height="70"/>
 40     <SubTexture name="grass_brown1.png" x="534" y="1063" width="58" height="57"/>
 41     <SubTexture name="grass_brown2.png" x="801" y="752" width="82" height="70"/>
 42     <SubTexture name="ground_cake.png" x="0" y="576" width="380" height="94"/>
 43     <SubTexture name="ground_cake_broken.png" x="0" y="0" width="380" height="94"/>
 44     <SubTexture name="ground_cake_small.png" x="218" y="1456" width="201" height="100"/>
 45     <SubTexture name="ground_cake_small_broken.png" x="262" y="1152" width="200" height="100"/>
 46     <SubTexture name="ground_grass.png" x="0" y="288" width="380" height="94"/>
 47     <SubTexture name="ground_grass_broken.png" x="0" y="384" width="380" height="94"/>
 48     <SubTexture name="ground_grass_small.png" x="213" y="1662" width="201" height="100"/>
 49     <SubTexture name="ground_grass_small_broken.png" x="382" y="204" width="200" height="100"/>
 50     <SubTexture name="ground_sand.png" x="0" y="672" width="380" height="94"/>
 51     <SubTexture name="ground_sand_broken.png" x="0" y="1056" width="380" height="94"/>
 52     <SubTexture name="ground_sand_small.png" x="208" y="1879" width="201" height="100"/>
 53     <SubTexture name="ground_sand_small_broken.png" x="382" y="102" width="200" height="100"/>
 54     <SubTexture name="ground_snow.png" x="0" y="768" width="380" height="94"/>
 55     <SubTexture name="ground_snow_broken.png" x="0" y="480" width="380" height="94"/>
 56     <SubTexture name="ground_snow_small.png" x="213" y="1764" width="201" height="100"/>
 57     <SubTexture name="ground_snow_small_broken.png" x="382" y="306" width="200" height="100"/>
 58     <SubTexture name="ground_stone.png" x="0" y="96" width="380" height="94"/>
 59     <SubTexture name="ground_stone_broken.png" x="0" y="192" width="380" height="94"/>
 60     <SubTexture name="ground_stone_small.png" x="382" y="408" width="200" height="100"/>
 61     <SubTexture name="ground_stone_small_broken.png" x="232" y="1288" width="200" height="100"/>
 62     <SubTexture name="ground_wood.png" x="0" y="960" width="380" height="94"/>
 63     <SubTexture name="ground_wood_broken.png" x="0" y="864" width="380" height="94"/>
 64     <SubTexture name="ground_wood_small.png" x="218" y="1558" width="200" height="100"/>
 65     <SubTexture name="ground_wood_small_broken.png" x="382" y="0" width="200" height="100"/>
 66     <SubTexture name="jetpack.png" x="563" y="1843" width="133" height="160"/>
 67     <SubTexture name="jetpack_item.png" x="852" y="1089" width="65" height="77"/>
 68     <SubTexture name="lifes.png" x="868" y="1936" width="52" height="71"/>
 69     <SubTexture name="lighting_blue.png" x="895" y="453" width="55" height="114"/>
 70     <SubTexture name="lighting_yellow.png" x="897" y="0" width="55" height="114"/>
 71     <SubTexture name="mushroom_brown.png" x="814" y="1574" width="81" height="85"/>
 72     <SubTexture name="mushroom_red.png" x="812" y="453" width="81" height="99"/>
 73     <SubTexture name="particle_beige.png" x="563" y="2005" width="58" height="41"/>
 74     <SubTexture name="particle_blue.png" x="852" y="1168" width="51" height="49"/>
 75     <SubTexture name="particle_brown.png" x="820" y="1877" width="44" height="48"/>
 76     <SubTexture name="particle_darkBrown.png" x="784" y="2003" width="54" height="43"/>
 77     <SubTexture name="particle_darkGrey.png" x="826" y="1364" width="51" height="43"/>
 78     <SubTexture name="particle_green.png" x="800" y="1003" width="48" height="46"/>
 79     <SubTexture name="particle_grey.png" x="623" y="2005" width="38" height="41"/>
 80     <SubTexture name="particle_pink.png" x="829" y="86" width="53" height="46"/>
 81     <SubTexture name="portal_orange.png" x="0" y="1288" width="230" height="82"/>
 82     <SubTexture name="portal_orangeParticle.png" x="262" y="1254" width="20" height="20"/>
 83     <SubTexture name="portal_yellow.png" x="0" y="1372" width="230" height="82"/>
 84     <SubTexture name="portal_yellowParticle.png" x="284" y="1254" width="20" height="20"/>
 85     <SubTexture name="powerup_bubble.png" x="826" y="134" width="71" height="70"/>
 86     <SubTexture name="powerup_bunny.png" x="826" y="1220" width="71" height="70"/>
 87     <SubTexture name="powerup_empty.png" x="894" y="1661" width="71" height="70"/>
 88     <SubTexture name="powerup_jetpack.png" x="820" y="1805" width="71" height="70"/>
 89     <SubTexture name="powerup_wings.png" x="826" y="1292" width="71" height="70"/>
 90     <SubTexture name="silver_1.png" x="584" y="406" width="84" height="84"/>
 91     <SubTexture name="silver_2.png" x="852" y="1003" width="66" height="84"/>
 92     <SubTexture name="silver_3.png" x="899" y="1219" width="50" height="84"/>
 93     <SubTexture name="silver_4.png" x="662" y="651" width="14" height="84"/>
 94     <SubTexture name="smoke.png" x="879" y="1364" width="51" height="48"/>
 95     <SubTexture name="spikeBall1.png" x="534" y="763" width="142" height="148"/>
 96     <SubTexture name="spikeBall_2.png" x="464" y="1122" width="148" height="141"/>
 97     <SubTexture name="spikeMan_jump.png" x="736" y="1063" width="114" height="155"/>
 98     <SubTexture name="spikeMan_stand.png" x="814" y="1417" width="90" height="155"/>
 99     <SubTexture name="spikeMan_walk1.png" x="704" y="1256" width="120" height="159"/>
100     <SubTexture name="spikeMan_walk2.png" x="812" y="296" width="90" height="155"/>
101     <SubTexture name="spike_bottom.png" x="894" y="206" width="51" height="87"/>
102     <SubTexture name="spike_top.png" x="885" y="752" width="51" height="87"/>
103     <SubTexture name="spikes_bottom.png" x="147" y="1988" width="95" height="53"/>
104     <SubTexture name="spikes_top.png" x="232" y="1390" width="95" height="53"/>
105     <SubTexture name="spring.png" x="420" y="1558" width="145" height="77"/>
106     <SubTexture name="springMan_hurt.png" x="800" y="860" width="110" height="141"/>
107     <SubTexture name="springMan_stand.png" x="801" y="609" width="110" height="141"/>
108     <SubTexture name="spring_in.png" x="0" y="1988" width="145" height="57"/>
109     <SubTexture name="spring_out.png" x="434" y="1265" width="145" height="110"/>
110     <SubTexture name="sun1.png" x="534" y="913" width="142" height="148"/>
111     <SubTexture name="sun2.png" x="421" y="1390" width="148" height="142"/>
112     <SubTexture name="wingMan1.png" x="382" y="635" width="174" height="126"/>
113     <SubTexture name="wingMan2.png" x="0" y="1879" width="206" height="107"/>
114     <SubTexture name="wingMan3.png" x="0" y="1559" width="216" height="101"/>
115     <SubTexture name="wingMan4.png" x="0" y="1456" width="216" height="101"/>
116     <SubTexture name="wingMan5.png" x="382" y="510" width="182" height="123"/>
117     <SubTexture name="wing_left.png" x="558" y="651" width="85" height="74"/>
118     <SubTexture name="wing_right.png" x="571" y="1458" width="85" height="74"/>
119 </TextureAtlas>
View Code

相关文章: