1 import pygame
2 import sys
3 # -------------------------围墙堆叠---------------------------
4 brickImage =r"../image/brick.png"
5 ironImage = r"../image/iron.png"
6
7 pygame.init()
8 screen=pygame.display.set_mode([800,600])
9 # 砖块类
10 class Brick(pygame.sprite.Sprite):
11 def __init__(self):
12 pygame.sprite.Sprite.__init__(self)
13 self.image=pygame.image.load(brickImage)
14 self.rect=self.image.get_rect()
15
16 # 铁块类
17 class Iron(pygame.sprite.Sprite):
18 def __init__(self):
19 pygame.sprite.Sprite.__init__(self)
20 self.image=pygame.image.load(ironImage)
21 self.rect=self.image.get_rect()
22
23 class Map():
24 def __init__(self):
25 # 初始化砖块群组
26 self.brickGroup=pygame.sprite.Group()
27 # 初始化铁块群组
28 self.ironGroup=pygame.sprite.Group()
29 # 数字代表地图中的位置
30 # 画砖块
31 X1379 = [2, 3, 6, 7, 18, 19, 22, 23]
32 Y1379 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 17, 18, 19, 20, 21, 22, 23]
33
34 X28 = [10, 11, 14, 15]
35 Y28 = [2, 3, 4, 5, 6, 7, 8, 11, 12, 15, 16, 17, 18, 19, 20]
36
37 X46 = [4, 5, 6, 7, 18, 19, 20, 21]
38 Y46 = [13, 14]
39
40 X5 = [12, 13]
41 Y5 = [16, 17]
42 X0Y0 = [(11, 23), (12, 23), (13, 23), (14, 23), (11, 24), (14, 24), (11, 25), (14, 25)]
43 for x in X1379:
44 for y in Y1379:
45 # 实例化砖块类对象
46 self.brick=Brick()
47 # 生砖块的位置
48 self.brick.rect.left,self.brick.rect.top=3+x*24,3+y*24
49
50 每循环一次自动将动画添加到精灵组(下同)
51 self.brickGroup.add(self.brick)
52 for x in X28:
53 for y in Y28:
54 self.brick=Brick()
55 self.brick.rect.left,self.brick.rect.top=3+x*24,3+y*24
56 self.brickGroup.add(self.brick)
57 for x in X46:
58 for y in Y46:
59 self.brick=Brick()
60 self.brick.rect.left,self.brick.rect.top=3+x*24,3+y*24
61 self.brickGroup.add(self.brick)
62 for x in X5:
63 for y in Y5:
64 self.brick=Brick()
65 self.brick.rect.left,self.brick.rect.top=3+x*24,3+y*24
66 self.brickGroup.add(self.brick)
67 # for item in X0Y0:
68 for x,y in X0Y0:
69 self.brick=Brick()
70 # self.brick.rect.left,self.brick.rect.top=3+item[0]*24,3+item[1]*24
71 self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
72 self.brickGroup.add(self.brick)
73 for x, y in [(0, 14), (1, 14), (12, 6), (13, 6), (12, 7), (13, 7), (24, 14), (25, 14)]:
74 self.iron=Iron()
75 self.iron.rect.left,self.iron.rect.top=3+x*24,3+y*24
76 self.ironGroup.add(self.iron)
77
78 while True:
79 for event in pygame.event.get():
80 if event.type == pygame.QUIT:
81 pygame.quit()
82 sys.exit()
83 self.brickGroup.update()
84 self.ironGroup.update()
85 self.brickGroup.draw(screen)
86 self.ironGroup.draw(screen)
87 pygame.display.update()
88
89
90
91
92 Map()
更多pygame知识可以关注博客:http://eyehere.net/2011/python-pygame-novice-professional-index/