如何向你的 Python 游戏中添加一个敌人
Pygame

> 
> 在本系列的第五部分，学习如何增加一个坏蛋与你的好人战斗。
> 
> 
> 


![](/data/attachment/album/201905/21/191627huku0uo0ngny39kd.png)


在本系列的前几篇文章中（参见 [第一部分](/article-9071-1.html)、[第二部分](/article-10850-1.html)、[第三部分](/article-10858-1.html) 以及 [第四部分](/article-10874-1.html))，你已经学习了如何使用 Pygame 和 Python 在一个空白的视频游戏世界中生成一个可玩的角色。但没有恶棍，英雄又将如何？


如果你没有敌人，那将会是一个非常无聊的游戏。所以在此篇文章中，你将为你的游戏添加一个敌人并构建一个用于创建关卡的框架。


在对玩家妖精实现全部功能之前，就来实现一个敌人似乎就很奇怪。但你已经学到了很多东西，创造恶棍与与创造玩家妖精非常相似。所以放轻松，使用你已经掌握的知识，看看能挑起怎样一些麻烦。


针对本次训练，你能够从 [Open Game Art](https://opengameart.org) 下载一些预创建的素材。此处是我使用的一些素材：


* 印加花砖（LCTT 译注：游戏中使用的花砖贴图）
* 一些侵略者
* 妖精、角色、物体以及特效


### 创造敌方妖精


是的，不管你意识到与否，你其实已经知道如何去实现敌人。这个过程与创造一个玩家妖精非常相似：


1. 创建一个类用于敌人生成
2. 创建 `update` 方法使得敌人能够检测碰撞
3. 创建 `move` 方法使得敌人能够四处游荡


从类入手。从概念上看，它与你的 `Player` 类大体相同。你设置一张或者一组图片，然后设置妖精的初始位置。


在继续下一步之前，确保你有一张你的敌人的图像，即使只是一张临时图像。将图像放在你的游戏项目的 `images` 目录（你放置你的玩家图像的相同目录）。


如果所有的活物都拥有动画，那么游戏看起来会好得多。为敌方妖精设置动画与为玩家妖精设置动画具有相同的方式。但现在，为了保持简单，我们使用一个没有动画的妖精。


在你代码 `objects` 节的顶部，使用以下代码创建一个叫做 `Enemy` 的类：



```
class Enemy(pygame.sprite.Sprite):
    '''
    生成一个敌人
    '''
    def __init__(self,x,y,img):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(os.path.join('images',img))
        self.image.convert_alpha()
        self.image.set_colorkey(ALPHA)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
```

如果你想让你的敌人动起来，使用让你的玩家拥有动画的 [相同方式](/article-10874-1.html)。


### 生成一个敌人


你能够通过告诉类，妖精应使用哪张图像，应出现在世界上的什么地方，来生成不只一个敌人。这意味着，你能够使用相同的敌人类，在游戏世界的任意地方生成任意数量的敌方妖精。你需要做的仅仅是调用这个类，并告诉它应使用哪张图像，以及你期望生成点的 X 和 Y 坐标。


再次，这从原则上与生成一个玩家精灵相似。在你脚本的 `setup` 节添加如下代码：



```
enemy   = Enemy(20,200,'yeti.png')  # 生成敌人
enemy_list = pygame.sprite.Group()  # 创建敌人组
enemy_list.add(enemy)               # 将敌人加入敌人组
```

在示例代码中，X 坐标为 20，Y 坐标为 200。你可能需要根据你的敌方妖精的大小，来调整这些数字，但尽量生成在一个范围内，使得你的玩家妖精能够碰到它。`Yeti.png` 是用于敌人的图像。


接下来，将敌人组的所有敌人绘制在屏幕上。现在，你只有一个敌人，如果你想要更多你可以稍后添加。一但你将一个敌人加入敌人组，它就会在主循环中被绘制在屏幕上。中间这一行是你需要添加的新行：



```
    player_list.draw(world)
    enemy_list.draw(world)  # 刷新敌人
    pygame.display.flip()
```

启动你的游戏，你的敌人会出现在游戏世界中你选择的 X 和 Y 坐标处。


### 关卡一


你的游戏仍处在襁褓期，但你可能想要为它添加另一个关卡。为你的程序做好未来规划非常重要，因为随着你学会更多的编程技巧，你的程序也会随之成长。即使你现在仍没有一个完整的关卡，你也应该按照假设会有很多关卡来编程。


思考一下“关卡”是什么。你如何知道你是在游戏中的一个特定关卡中呢？


你可以把关卡想成一系列项目的集合。就像你刚刚创建的这个平台中，一个关卡，包含了平台、敌人放置、战利品等的一个特定排列。你可以创建一个类，用来在你的玩家附近创建关卡。最终，当你创建了一个以上的关卡，你就可以在你的玩家达到特定目标时，使用这个类生成下一个关卡。


将你写的用于生成敌人及其群组的代码，移动到一个每次生成新关卡时都会被调用的新函数中。你需要做一些修改，使得每次你创建新关卡时，你都能够创建一些敌人。



```
class Level():
    def bad(lvl,eloc):
        if lvl == 1:
            enemy = Enemy(eloc[0],eloc[1],'yeti.png') # 生成敌人
            enemy_list = pygame.sprite.Group() # 生成敌人组
            enemy_list.add(enemy)              # 将敌人加入敌人组
        if lvl == 2:
            print("Level " + str(lvl) )

        return enemy_list
```

`return` 语句确保了当你调用 `Level.bad` 方法时，你将会得到一个 `enemy_list` 变量包含了所有你定义的敌人。


因为你现在将创造敌人作为每个关卡的一部分，你的 `setup` 部分也需要做些更改。不同于创造一个敌人，取而代之的是你必须去定义敌人在那里生成，以及敌人属于哪个关卡。



```
eloc = []
eloc = [200,20]
enemy_list = Level.bad( 1, eloc )
```

再次运行游戏来确认你的关卡生成正确。与往常一样，你应该会看到你的玩家，并且能看到你在本章节中添加的敌人。


### 痛击敌人


一个敌人如果对玩家没有效果，那么它不太算得上是一个敌人。当玩家与敌人发生碰撞时，他们通常会对玩家造成伤害。


因为你可能想要去跟踪玩家的生命值，因此碰撞检测发生在 `Player` 类，而不是 `Enemy` 类中。当然如果你想，你也可以跟踪敌人的生命值。它们之间的逻辑与代码大体相似，现在，我们只需要跟踪玩家的生命值。


为了跟踪玩家的生命值，你必须为它确定一个变量。代码示例中的第一行是上下文提示，那么将第二行代码添加到你的 Player 类中：



```
        self.frame  = 0
        self.health = 10
```

在你 `Player` 类的 `update` 方法中，添加如下代码块：



```
        hit_list = pygame.sprite.spritecollide(self, enemy_list, False)
        for enemy in hit_list:
            self.health -= 1
            print(self.health)
```

这段代码使用 Pygame 的 `sprite.spritecollide` 方法，建立了一个碰撞检测器，称作 `enemy_hit`。每当它的父类妖精（生成检测器的玩家妖精）的碰撞区触碰到 `enemy_list` 中的任一妖精的碰撞区时，碰撞检测器都会发出一个信号。当这个信号被接收，`for` 循环就会被触发，同时扣除一点玩家生命值。


一旦这段代码出现在你 `Player` 类的 `update` 方法，并且 `update` 方法在你的主循环中被调用，Pygame 会在每个时钟滴答中检测一次碰撞。


### 移动敌人


如果你愿意，静止不动的敌人也可以很有用，比如能够对你的玩家造成伤害的尖刺和陷阱。但如果敌人能够四处徘徊，那么游戏将更富有挑战。


与玩家妖精不同，敌方妖精不是由玩家控制，因此它必须自动移动。


最终，你的游戏世界将会滚动。那么，如何在游戏世界自身滚动的情况下，使游戏世界中的敌人前后移动呢？


举个例子，你告诉你的敌方妖精向右移动 10 步，向左移动 10 步。但敌方妖精不会计数，因此你需要创建一个变量来跟踪你的敌人已经移动了多少步，并根据计数变量的值来向左或向右移动你的敌人。


首先，在你的 `Enemy` 类中创建计数变量。添加以下代码示例中的最后一行代码：



```
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.counter = 0 # 计数变量
```

然后，在你的 `Enemy` 类中创建一个 `move` 方法。使用 if-else 循环来创建一个所谓的死循环：


* 如果计数在 0 到 100 之间，向右移动；
* 如果计数在 100 到 200 之间，向左移动；
* 如果计数大于 200，则将计数重置为 0。


死循环没有终点，因为循环判断条件永远为真，所以它将永远循环下去。在此情况下，计数器总是介于 0 到 100 或 100 到 200 之间，因此敌人会永远地从左向右再从右向左移动。


你用于敌人在每个方向上移动距离的具体值，取决于你的屏幕尺寸，更确切地说，取决于你的敌人移动的平台大小。从较小的值开始，依据习惯逐步提高数值。首先进行如下尝试：



```
    def move(self):
        '''
        敌人移动
        '''
        distance = 80
        speed = 8

        if self.counter >= 0 and self.counter <= distance:
            self.rect.x += speed
        elif self.counter >= distance and self.counter <= distance*2:
            self.rect.x -= speed
        else:
            self.counter = 0

        self.counter += 1
```

你可以根据需要调整距离和速度。


当你现在启动游戏，这段代码有效果吗？


当然不，你应该也知道原因。你必须在主循环中调用 `move` 方法。如下示例代码中的第一行是上下文提示，那么添加最后两行代码：



```
    enemy_list.draw(world) #refresh enemy
    for e in enemy_list:
        e.move()
```

启动你的游戏看看当你打击敌人时发生了什么。你可能需要调整妖精的生成地点，使得你的玩家和敌人能够碰撞。当他们发生碰撞时，查看 [IDLE](https://docs.python.org/3/library/idle.html) 或 [Ninja-IDE](http://ninja-ide.org/) 的控制台，你可以看到生命值正在被扣除。


![](/data/attachment/album/201905/21/191628exeuttrih3788s78.png)


你应该已经注意到，在你的玩家和敌人接触时，生命值在时刻被扣除。这是一个问题，但你将在对 Python 进行更多练习以后解决它。


现在，尝试添加更多敌人。记得将每个敌人加入 `enemy_list`。作为一个练习，看看你能否想到如何改变不同敌方妖精的移动距离。




---


via: <https://opensource.com/article/18/5/pygame-enemy>


作者：[Seth Kenlon](https://opensource.com/users/seth) 选题：[lujun9972](https://github.com/lujun9972) 译者：[cycoe](https://github.com/cycoe) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In the previous articles in this series (see [part 1](https://opensource.com/article/17/10/python-101), [part 2](https://opensource.com/article/17/12/game-framework-python), [part 3](https://opensource.com/article/17/12/game-python-add-a-player), and [part 4](https://opensource.com/article/17/12/game-python-moving-player)), you learned how to use Pygame and Python to spawn a playable hero character in an as-yet empty video game world. But what's a hero without a villain?

It would make for a pretty boring game if you had no enemies, so in this article, you'll add an enemy to your game and construct a framework for building levels.

It might seem strange to jump ahead to enemies when there's still more to be done to make the player sprite fully functional, but you've learned a lot already, and creating villains is very similar to creating a player sprite. So relax, use the knowledge you already have, and see what it takes to stir up some trouble.

For this exercise, you need an enemy sprite. If you haven't downloaded one already, you can find [Creative Commons](https://opensource.com/article/20/1/what-creative-commons) assets on [OpenGameArt.org](https://opengameart.org/content/opp2017-sprites-characters-objects-effects).

## Creating the enemy sprite

Whether you realize it or not, you already know how to implement enemies. The process is similar to creating a player sprite:

- Make a class so enemies can spawn.
- Create an
`update`

function for the enemy, and update the enemy in your main loop. - Create a
`move`

function so your enemy can roam around.

Start with the class. Conceptually, it's mostly the same as your Player class. You set an image or series of images, and you set the sprite's starting position.

Before continuing, make sure you have placed your enemy graphic in your game project's `images`

directory (the same directory where you placed your player image). In this article's example code, the enemy graphic is named `enemy.png`

.

A game looks a lot better when everything *alive* is animated. Animating an enemy sprite is done the same way as animating a player sprite. For now, though, keep it simple, and use a non-animated sprite.

At the top of the `objects`

section of your code, create a class called Enemy with this code:

```
class Enemy(pygame.sprite.Sprite):
"""
Spawn an enemy
"""
def __init__(self,x,y,img):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join('images',img))
self.image.convert_alpha()
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
```


If you want to animate your enemy, do it the [same way](https://opensource.com/article/17/12/game-python-moving-player)[ you animated your player](https://opensource.com/article/17/12/game-python-moving-player).

## Spawning an enemy

You can make the class useful for spawning more than just one enemy by allowing yourself to tell the class which image to use for the sprite and where in the world you want the sprite to appear. This means you can use this same enemy class to generate any number of enemy sprites anywhere in the game world. All you have to do is make a call to the class, and tell it which image to use, along with the X and Y coordinates of your desired spawn point.

Ao you did when spawning a player sprite, add code to designate a spawn point in the `setup`

section of your script:

```
enemy = Enemy(300,0,'enemy.png') # spawn enemy
enemy_list = pygame.sprite.Group() # create enemy group
enemy_list.add(enemy) # add enemy to group
```


In that sample code, you spawn an enemy by creating a new object (called `enemy`

), at 300 pixels on the X axis and 0 on the Y axis. Spawning the enemy at 0 on the Y axis means that its top left corner is located at 0, with the graphic itself descending down from that point. You might need to adjust these numbers, or the numbers for your hero sprite, depending on how big your sprites are, but try to get it to spawn in a place you can reach with your player sprite (accounting for your game's current lack of vertical movement). In the end, I placed my enemy at 0 pixels on the Y axis and my hero at 30 pixels to get them boh to appear on the same plane. Experiment with the spawn points for yourself, keeping in mind that greater Y axis numbers are lower on the screen.

Your hero graphic had an image "hard coded" into its class because there's only one hero, but you may want to use different graphics for each enemy, so the image file is something you can define at sprite creation. The image used for this enemy sprite is `enemy.png`

.

## Drawing a sprite on screen

If you were to launch your game now, it would run but you wouldn't see an enemy. You might recall the same problem when you created your player sprite. Do you remember how to fix it?

To get a sprite to appear on screen, you must add them to your main loop. If something is not in your main loop, then it only happens once, and only for a millisecond. If you want something to persist in your game, it must happen in the main loop.

You must add code to draw all enemies in the enemy group (called `enemy_list`

), which you established in your setup section, on the screen. The middle line in this example code is the new line you need to add:

```
player_list.draw(world)
enemy_list.draw(world) # refresh enemies
pygame.display.flip()
```


Right now, you have only one enemy, but you can add more later if you want. As long as you add an enemy to the enemies group, it will be drawn to the screen during the main loop.

Launch your game. Your enemy appears in the game world at whatever X and Y coordinate you chose.

## Level one

Your game is in its infancy, but you will probably want to add a series of levels, eventually. It's important to plan ahead when you program so your game can grow as you learn more about programming. Even though you don't even have one complete level yet, you should code as if you plan on having many levels.

Think about what a "level" is. How do you know you are at a certain level in a game?

You can think of a level as a collection of items. In a platformer, such as the one you are building here, a level consists of a specific arrangement of platforms, placement of enemies and loot, and so on. You can build a class that builds a level around your player. Eventually, when you create more than one level, you can use this class to generate the next level when your player reaches a specific goal.

Move the code you wrote to create an enemy and its group into a new function that gets called along with each new level. It requires some modification so that each time you create a new level, you can create and place several enemies:

```
class Level():
def bad(lvl,eloc):
if lvl == 1:
enemy = Enemy(eloc[0],eloc[1],'enemy.png') # spawn enemy
enemy_list = pygame.sprite.Group() # create enemy group
enemy_list.add(enemy) # add enemy to group
if lvl == 2:
print("Level " + str(lvl) )
return enemy_list
```


The `return`

statement ensures that when you use the `Level.bad`

function, you're left with an `enemy_list`

containing each enemy you defined.

Since you are creating enemies as part of each level now, your `setup`

section needs to change, too. Instead of creating an enemy, you must define where the enemy will spawn and what level it belongs to.

```
eloc = []
eloc = [300,0]
enemy_list = Level.bad( 1, eloc )
```


Run the game again to confirm your level is generating correctly. You should see your player, as usual, and the enemy you added in this chapter.

## Hitting the enemy

An enemy isn't much of an enemy if it has no effect on the player. It's common for enemies to cause damage when a player collides with them.

Since you probably want to track the player's health, the collision check happens in the Player class rather than in the Enemy class. You can track the enemy's health, too, if you want. The logic and code are pretty much the same, but, for now, just track the player's health.

To track player health, you must first establish a variable for the player's health. The first line in this code sample is for context, so add the second line to your Player class:

```
self.frame = 0
self.health = 10
```


In the `update`

function of your Player class, add this code block:

```
hit_list = pygame.sprite.spritecollide(self, enemy_list, False)
for enemy in hit_list:
self.health -= 1
print(self.health)
```


This code establishes a collision detector using the Pygame function `sprite.spritecollide`

, called `enemy_hit`

. This collision detector sends out a signal any time the hitbox of its parent sprite (the player sprite, where this detector has been created) touches the hitbox of any sprite in `enemy_list`

. The `for`

loop is triggered when such a signal is received and deducts a point from the player's health.

Since this code appears in the `update`

function of your player class and `update`

is called in your main loop, Pygame checks for this collision once every clock tick.

## Moving the enemy

An enemy that stands still is useful if you want, for instance, spikes or traps that can harm your player, but the game is more of a challenge if the enemies move around a little.

Unlike a player sprite, the enemy sprite is not controlled by the user. Its movements must be automated.

Eventually, your game world will scroll, so how do you get an enemy to move back and forth within the game world when the game world itself is moving?

You tell your enemy sprite to take, for example, 10 paces to the right, then 10 paces to the left. An enemy sprite can't count, so you have to create a variable to keep track of how many paces your enemy has moved and program your enemy to move either right or left depending on the value of your counting variable.

First, create the counter variable in your Enemy class. Add the last line in this code sample:

```
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.counter = 0 # counter variable
```


Next, create a `move`

function in your Enemy class. Use an if-else loop to create what is called an *infinite loop*:

- Move right if the counter is on any number from 0 to 100.
- Move left if the counter is on any number from 100 to 200.
- Reset the counter back to 0 if the counter is greater than 200.

An infinite loop has no end; it loops forever because nothing in the loop is ever untrue. The counter, in this case, is always either between 0 and 100 or 100 and 200, so the enemy sprite walks right to left and right to left forever.

The actual numbers you use for how far the enemy will move in either direction depending on your screen size, and possibly, eventually, the size of the platform your enemy is walking on. Start small and work your way up as you get used to the results. Try this first:

```
def move(self):
'''
enemy movement
'''
distance = 80
speed = 8
if self.counter >= 0 and self.counter <= distance:
self.rect.x += speed
elif self.counter >= distance and self.counter <= distance*2:
self.rect.x -= speed
else:
self.counter = 0
self.counter += 1
```


After you enter this code, PyCharm will offer to simplify the "chained comparison". You can accept its suggestion to optimize your code, and to learn some advanced Python syntax. You can also safely ignore PyCharm. The code works, either way.

You can adjust the distance and speed as needed.

The question is: does this code work if you launch your game now?

Of course not! And you know why: you must call the `move`

function in your main loop.

The first line in this sample code is for context, so add the last two lines:

```
enemy_list.draw(world) #refresh enemy
for e in enemy_list:
e.move()
```


Launch your game and see what happens when you hit your enemy. You might have to adjust where the sprites spawn so that your player and your enemy sprite can collide. When they do collide, look in the console of IDLE or PyCharm to see the health points being deducted.

![A hero and a nemesis A hero and a nemesis](https://opensource.com/sites/default/files/pygame-hero-enemy.jpg)

You may notice that health is deducted for every moment your player and enemy are touching. That's a problem, but it's a problem you'll solve later, after you've had more practice with Python.

For now, try adding some more enemies. Remember to add each enemy to the `enemy_list`

. As an exercise, see if you can think of how you can change how far different enemy sprites move.

## Code so far

For you reference, here's the code so far:

```
#!/usr/bin/env python3
# by Seth Kenlon
# GPLv3
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pygame
import sys
import os
'''
Variables
'''
worldx = 960
worldy = 720
fps = 40
ani = 4
world = pygame.display.set_mode([worldx, worldy])
BLUE = (25, 25, 200)
BLACK = (23, 23, 23)
WHITE = (254, 254, 254)
ALPHA = (0, 255, 0)
'''
Objects
'''
class Player(pygame.sprite.Sprite):
"""
Spawn a player
"""
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.movex = 0
self.movey = 0
self.frame = 0
self.health = 10
self.images = []
for i in range(1, 5):
img = pygame.image.load(os.path.join('images', 'hero' + str(i) + '.png')).convert()
img.convert_alpha()
img.set_colorkey(ALPHA)
self.images.append(img)
self.image = self.images[0]
self.rect = self.image.get_rect()
def control(self, x, y):
"""
control player movement
"""
self.movex += x
self.movey += y
def update(self):
"""
Update sprite position
"""
self.rect.x = self.rect.x + self.movex
self.rect.y = self.rect.y + self.movey
# moving left
if self.movex < 0:
self.frame += 1
if self.frame > 3*ani:
self.frame = 0
self.image = pygame.transform.flip(self.images[self.frame // ani], True, False)
# moving right
if self.movex > 0:
self.frame += 1
if self.frame > 3*ani:
self.frame = 0
self.image = self.images[self.frame//ani]
hit_list = pygame.sprite.spritecollide(self, enemy_list, False)
for enemy in hit_list:
self.health -= 1
print(self.health)
class Enemy(pygame.sprite.Sprite):
"""
Spawn an enemy
"""
def __init__(self, x, y, img):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join('images', img))
self.image.convert_alpha()
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.counter = 0
def move(self):
"""
enemy movement
"""
distance = 80
speed = 8
if self.counter >= 0 and self.counter <= distance:
self.rect.x += speed
elif self.counter >= distance and self.counter <= distance*2:
self.rect.x -= speed
else:
self.counter = 0
self.counter += 1
class Level():
def bad(lvl, eloc):
if lvl == 1:
enemy = Enemy(eloc[0], eloc[1], 'enemy.png')
enemy_list = pygame.sprite.Group()
enemy_list.add(enemy)
if lvl == 2:
print("Level " + str(lvl) )
return enemy_list
'''
Setup
'''
backdrop = pygame.image.load(os.path.join('images', 'stage.png'))
clock = pygame.time.Clock()
pygame.init()
backdropbox = world.get_rect()
main = True
player = Player() # spawn player
player.rect.x = 0 # go to x
player.rect.y = 30 # go to y
player_list = pygame.sprite.Group()
player_list.add(player)
steps = 10
eloc = []
eloc = [300, 0]
enemy_list = Level.bad(1, eloc )
'''
Main Loop
'''
while main:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
try:
sys.exit()
finally:
main = False
if event.type == pygame.KEYDOWN:
if event.key == ord('q'):
pygame.quit()
try:
sys.exit()
finally:
main = False
if event.key == pygame.K_LEFT or event.key == ord('a'):
player.control(-steps, 0)
if event.key == pygame.K_RIGHT or event.key == ord('d'):
player.control(steps, 0)
if event.key == pygame.K_UP or event.key == ord('w'):
print('jump')
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == ord('a'):
player.control(steps, 0)
if event.key == pygame.K_RIGHT or event.key == ord('d'):
player.control(-steps, 0)
world.blit(backdrop, backdropbox)
player.update()
player_list.draw(world)
enemy_list.draw(world)
for e in enemy_list:
e.move()
pygame.display.flip()
clock.tick(fps)
```



## 4 Comments