pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load(imagepath)
self.image = pygame.transform.scale(self.image, (blocksize, blocksize))
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = coordinate[ self.coordinate = coordinate
self.blocksize = blocksize
screen.blit(self.image, self.rect)
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(imagepath)
self.image = pygame.transform.scale(self.image, (blocksize, blocksize))
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = coordinate[ self.coordinate = coordinate
self.blocksize = blocksize
screen.blit(self.image, self.rect)
pygame.sprite.Sprite.__init_ elif raise ValueError(% screen.blit( True
pygame.sprite.Sprite.__init_ direction == < [ False elif direction == >= [ False
elif direction == < [ False
elif direction == >= [ False
raise ValueError(% direction)
True
action = random.choice([ flag = False
action [ flag = True
elif action [ flag = True
action, flag
Bomb(imagepath=imagepath, coordinate=copy.deepcopy( screen.blit( True
eaten_fruit = pygame.sprite.spritecollide( fruit directions = [ idx = directions.index(direction)
pygame.sprite.Sprite.__init_ * - * screen.blit( text = rect = text.get_rect(center=( screen.blit(text, rect)
False
False
explode_area = each image = pygame.image.load( image = pygame.transform.scale(image, ( rect = image.get_rect()
rect.left, rect.top = each[ screen.blit(image, rect)
explode_area
explode_area = []
ymin range( ymin < instances_list[ymin][[ explode_area.append([ ymax range( ymax >= len(instances_list) instances_list[ymax][[ explode_area.append([ xmin range( xmin < instances_list[[ explode_area.append([xmin, xmax range( xmax >= len(instances_list[instances_list[[ explode_area.append([xmax, explode_area
self.instances_list = self.__parse(mapfilepath) self.bg_paths = bg_paths
self.wall_paths = wall_paths
self.blocksize = blocksize
self.height = len(self.instances_list)
self.width = len(self.instances_list[ self.screen_size = (blocksize * self.width, blocksize * self.height)
j range(self.height):
i range(self.width):
instance = self.instances_list[j][i]
instance == elem = Wall(self.wall_paths[ instance == elem = Wall(self.wall_paths[ instance == elem = Wall(self.wall_paths[ instance == elem = Background(self.bg_paths[ instance == elem = Background(self.bg_paths[ instance == elem = Background(self.bg_paths[ ValueError( elem.draw(screen)
i = random.randint( j = random.randint( coordinate = [i, j]
used_spaces coordinate used_spaces:
instance = self.instances_list[j][i]
instance [ coordinate
self.instances_list[coordinate[ instances_list = []
open(mapfilepath) f:
line f.readlines():
instances_line_list = []
c line:
c [ instances_line_list.append(c)
instances_list.append(instances_line_list)
instances_list
pygame.init() pygame.mixer.init()
pygame.mixer.music.load(cfg.BGMPATH)
pygame.mixer.music.play( screen = pygame.display.set_mode(cfg.SCREENSIZE)
pygame.display.set_caption( Interface(screen, cfg, mode= font = pygame.font.SysFont( gamemap_path cfg.GAMEMAPPATHS:
map_parser = mapParser(gamemap_path, bg_paths=cfg.BACKGROUNDPATHS, wall_paths=cfg.WALLPATHS, blocksize=cfg.BLOCKSIZE)
fruit_sprite_group = pygame.sprite.Group()
used_spaces = []
i range( coordinate = map_parser.randomGetSpace(used_spaces)
used_spaces.append(coordinate)
fruit_sprite_group.add(Fruit(random.choice(cfg.FRUITPATHS), coordinate=coordinate, blocksize=cfg.BLOCKSIZE))
coordinate = map_parser.randomGetSpace(used_spaces)
used_spaces.append(coordinate)
ourhero = Hero(imagepaths=cfg.HEROZELDAPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name= aihero_sprite_group = pygame.sprite.Group()
coordinate = map_parser.randomGetSpace(used_spaces)
aihero_sprite_group.add(Hero(imagepaths=cfg.HEROBATMANPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name= used_spaces.append(coordinate)
coordinate = map_parser.randomGetSpace(used_spaces)
aihero_sprite_group.add(Hero(imagepaths=cfg.HERODKPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name= used_spaces.append(coordinate)
bomb_sprite_group = pygame.sprite.Group()
is_win_flag = screen = pygame.display.set_mode(map_parser.screen_size)
clock = pygame.time.Clock()
dt = clock.tick(cfg.FPS)
event pygame.event.get():
event.type == pygame.QUIT:
pygame.quit()
sys.exit( event.type == pygame.KEYDOWN:
event.key == pygame.K_UP:
ourhero.move( event.key == pygame.K_DOWN:
ourhero.move( event.key == pygame.K_LEFT:
ourhero.move( event.key == pygame.K_RIGHT:
ourhero.move( event.key == pygame.K_SPACE:
ourhero.bomb_cooling_count <= bomb_sprite_group.add(ourhero.generateBomb(imagepath=cfg.BOMBPATH, digitalcolor=cfg.YELLOW, explode_imagepath=cfg.FIREPATH))
screen.fill(cfg.WHITE)
hero aihero_sprite_group:
action, flag = hero.randomAction(dt)
flag action == bomb_sprite_group.add(hero.generateBomb(imagepath=cfg.BOMBPATH, digitalcolor=cfg.YELLOW, explode_imagepath=cfg.FIREPATH))
ourhero.eatFruit(fruit_sprite_group)
hero aihero_sprite_group:
hero.eatFruit(fruit_sprite_group)
map_parser.draw(screen)
bomb bomb_sprite_group:
bomb.is_being:
bomb_sprite_group.remove(bomb)
explode_area = bomb.draw(screen, dt, map_parser)
explode_area:
ourhero.coordinate explode_area:
ourhero.health_value -= bomb.harm_value
hero aihero_sprite_group:
hero.coordinate explode_area:
hero.health_value -= bomb.harm_value
fruit_sprite_group.draw(screen)
hero aihero_sprite_group:
hero.draw(screen, dt)
ourhero.draw(screen, dt)
pos_x = showText(screen, font, text=ourhero.hero_name+ hero aihero_sprite_group:
pos_x, pos_y = pos_x+ pos_x = showText(screen, font, text=hero.hero_name+ ourhero.health_value <= is_win_flag = hero aihero_sprite_group:
hero.health_value <= aihero_sprite_group.remove(hero)
len(aihero_sprite_group) == is_win_flag = pygame.display.update()
clock.tick(cfg.FPS)
is_win_flag:
Interface(screen, cfg, mode= break
Interface(screen, cfg, mode=\'game_end\')
逻辑很简单,就是初始化之后导入关卡地图开始游戏,结束一关之后,判断是游戏胜利还是游戏失败,游戏胜利的话就进入下一关,否则就退出主循环,让玩家选择是否重新开始游戏。具体细节自己看下代码就能懂了,必要的注释我都加过了。
All done~完整源代码详见相关文件~
JavaScript
JavaScript是网络上最常用也是支持者对多的客户端脚本语言。它可以收集用户的跟踪数据,不需要重载页面直接提交表单,在页面嵌入多媒体文件,甚至运行网页游戏。
我们可以在网页源代码的<script>
标签里看到,比如:
<script type="text/javascript" src="https://statics.huxiu.com/w/mini/static_2015/js/sea.js?v=201601150944"></script>
jQuery
jQuery是一个非常常见的库,70%最流行的网站(约200万)和约30%的其他网站(约2亿)都在使用。一个网站使用jQuery的特征,就是源代码里包含了jQuery入口,比如:
<script type="text/javascript" src="https://statics.huxiu.com/w/mini/static_2015/js/jquery-1.11.1.min.js?v=201512181512"></script>
如果你在一个网站上看到了jQuery,那么采集这个网站数据的时候要格外小心。jQuery可以动态地创建HTML内容,只有在JavaScript代码执行后才会显示。如果你使用传统的方法采集页面内容,就只能获得JavaScript代码执行之前页面的内容。
Ajax
我们与网站服务器通信的唯一方式,就是发出HTTP请求获取新页面。如果提交表单之后,或从服务器获取信息之后,网站的页面不需要重新刷新,那么你访问的网站就在用Ajax技术。
Ajax其实并不是一门语言,而是用来完成网络任务(可以认为它与网络数据采集差不多)的一系列技术。Ajax全称是Asynchronous JavaScript and XML(异步JavaScript和XML),网站不需要使用单独的页面请求就可以和网络服务器进行交互(收发信息)
DHTML
Ajax一样,动态HTML(Dynamic HTML, DHTML)也是一系列用于解决网络问题的技术集合。DHTML使用客户端语言改变页面的HTML元素(HTML, CSS,或者二者皆可改变)。比如页面上的按钮只有当用户移动鼠标之后才出现,背景色可能每次点击都会改变,或者用一个Ajax请求触发页面加载一段内容,网页是否属于DHTML,关键要看有没有用JavaScript控制HTML和CSS元素。
那么,如何搞定?
那些使用了Ajax或DHTML技术改变/加载内容的页面,可能有一些采集手段。但是用Python解决这个问题只有两种途径:
- 直接从JavaScript代码里采集内容(费时费力)
- 用Python的第三方库运行JavaScript,直接采集你在浏览器里看到的页面