Python and Pygame universal module constructs a game frame (2)

Each game requires a player, and each player needs a steerable role, which is we are going to be in this part of the third series of the need to do.

In Pygame, the player controls the icon or avatar is known as goblins, if you do not have any image can be used for players fairy, you can use  Krita  or
 or  Inkscape  to create some of their own image. If you lack confidence in their artistic, you can be in  OpenClipArt.org  or  OpenGameArt.org  search for some ready-made image. If you have not in accordance with the said article to create a separate  images folder, then you need to create it in your Python project directory. You will want to use in the game are put pictures  images folder.

In order to make your game real thrill, you should be a dynamic picture of fairies as your hero. This means that you need to draw more material, and they are to be different. The most common is to walk animation cycle through a series of images to make your fairy look like walking. Walking cycling fastest rough version requires four images.

Note: The sample code in this article is compatible with both static and dynamic players fairy.

Your players fairy named  hero.png. If you are creating a dynamic monster, you need to add a number after their name, from  hero1.png the beginning.

Create a Python class

In Python, when you create one you want to display objects on the screen, you need to create a class.

In your position near the top of the Python script, add the following code to create a player. In the following code example, already in the first three rows Python script that you are dealing in:

1.  `import pygame`
2.  `import sys`
3.  `import os #  以下是新代码`

5.  `class  Player(pygame.sprite.Sprite):`
6.  `'''`
7.  `    生成一个玩家`
8.  `    '''`
9.  `def __init__(self):`
10.  `        pygame.sprite.Sprite.__init__(self)`
11.  `self.images =  []`
12.  `img = pygame.image.load(os.path.join('images','hero.png')).convert()`
13.  `self.images.append(img)`
14.  `self.image =  self.images[0]`
15.  `self.rect  =  self.image.get_rect()`

If you can control role has a walk cycle, the  images folder will be saved as a picture corresponds  hero1.png to  hero4.png a separate file.

Use a loop to tell Python through each file.

1.  `'''`
2.  `对象`
3.  `'''`

5.  `class  Player(pygame.sprite.Sprite):`
6.  `'''`
7.  `    生成一个玩家`
8.  `    '''`
9.  `def __init__(self):`
10.  `        pygame.sprite.Sprite.__init__(self)`
11.  `self.images =  []`
12.  `for i in range(1,5):`
13.  `img = pygame.image.load(os.path.join('images','hero'  + str(i)  +  '.png')).convert()`
14.  `self.images.append(img)`
15.  `self.image =  self.images[0]`
16.  `self.rect  =  self.image.get_rect()`

The players into the game world

Has now created a Player class, you need to use it to generate a goblin player in your game world. If you do not call the Player class, then it will never work, (game world) there would be no players. You can run your game to immediately verify. An article on the game will see the end of the run like that, and get a clear result: an empty game world.

To a goblin player game brought to your world, you must call the Player class to generate a monster, and add it to the Pygame fairy group. In the sample code below, the first three lines of code that already exist, you need to add code thereafter:


1.  `world = pygame.display.set_mode([worldx,worldy])`
2.  `backdrop    = pygame.image.load(os.path.join('images','stage.png')).convert()`
3.  `backdropbox =  screen.get_rect()`

5.  `#  以下是新代码`

7.  `player =  Player()  #  生成玩家`
8.  `player.rect.x =  0  #  移动 x 坐标`
9.  `player.rect.y =  0  #  移动 y 坐标`
10.  `player_list = pygame.sprite.Group()`
11.  `player_list.add(player)`

Try to start your game to see what happens. High-energy warning: it does not work as you expect, when you start your project, the player does not appear goblins. In fact it generates, but there was only a millisecond. How do you fix a appears only for a millisecond things? You may recall the previous article, you need to add something to the main loop. In order to make the player's presence for more than a millisecond, you need to tell Python in each cycle are drawn once.

The bottom of your statement cycle changes as follows:


1.  `    world.blit(backdrop, backdropbox)`
2.  `    player_list.draw(screen)  #  绘制玩家`
3.  `    pygame.display.flip()`
4.  `    clock.tick(fps)`

Now start your game, your players appeared!

Alpha channel settings

Depending on how you create your player goblins around it might be a color. What you see is a space to be occupied by the alpha channel. It could have been invisible "color", but do not know Python to make it invisible. So you see, around the border area around the goblin (or modern game terminology "Hit Areahit box"Space inside).

你可以通过设置一个 alpha 通道和 RGB 值来告诉 Python 使哪种颜色不可见。如果你不知道你使用 alpha 通道的图像的 RGB 值,你可以使用 Krita 或 Inkscape 打开它,并使用一种独特的颜色,比如 #00ff00(差不多是“绿屏绿”)来填充图像周围的空白区域。记下颜色对应的十六进制值(此处为 #00ff00,绿屏绿)并将其作为 alpha 通道用于你的 Python 脚本。

使用 alpha 通道需要在你的妖精生成相关代码中添加如下两行。类似第一行的代码已经存在于你的脚本中,你只需要添加另外两行:


1.  `img = pygame.image.load(os.path.join('images','hero'  + str(i)  +  '.png')).convert()`
2.  `            img.convert_alpha()  #  优化 alpha`
3.  `            img.set_colorkey(ALPHA)  #  设置 alpha`

除非你告诉它,否则 Python 不知道将哪种颜色作为 alpha 通道。在你代码的设置相关区域,添加一些颜色定义。将如下的变量定义添加于你的设置相关区域的任意位置:

1.  `ALPHA =  (0,  255,  0)`

在以上示例代码中,0,255,0 被我们使用,它在 RGB 中所代表的值与 #00ff00 在十六进制中所代表的值相同。你可以通过一个优秀的图像应用程序,如 GIMP、Krita 或 Inkscape,来获取所有这些颜色值。或者,你可以使用一个优秀的系统级颜色选择器,如 KColorChooser,来检测颜色。

如果你的图像应用程序将你的妖精背景渲染成了其他的值,你可以按需调整 ALPHA 变量的值。不论你将 alpha 设为多少,最后它都将“不可见”。RGB 颜色值是非常严格的,因此如果你需要将 alpha 设为 000,但你又想将 000 用于你图像中的黑线,你只需要将图像中线的颜色设为 111。这样一来,(图像中的黑线)就足够接近黑色,但除了电脑以外没有人能看出区别。

运行你的游戏查看结果。

大家在学python的时候肯定会遇到很多难题,以及对于新技术的追求,这里推荐一下我们的Python学习扣qun:784758214,这里是python学习者聚集地!!同时,自己是一名高级python开发工程师,从基础的python脚本到web开发、爬虫、django、数据挖掘等,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!每日分享一些学习的方法和需要注意的小细节

点击:python技术分享交流

Guess you like

Origin blog.csdn.net/zhizhun88/article/details/90707089