Disclaimer: It is not easy to be original, and may not be reproduced without permission
0. Preface
hello, friends! I'm the cat lady who took you guys to write the game.
Today we are going to make a little game for children to eat mushrooms!
In the last episode, we made a small square and displayed it in the game window. Don't underestimate this small square, it is the real body of the game's protagonist ! Today we will write the code, 实现通过键盘的方向键
, to control the movement of the small square, the effect is like this.
1. Define a move function
We define a move
function in the Player
class, and get the user's keyboard input through the pygame.key
get_pressed
function to get the user's keyboard input. A, a sequence of boolean values assigned to key_pressed . Yourselves, you can print out this variable and see what it is!
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.image = pygame.Surface((60, 30))
self.image.fill("white")
self.rect = self.image.get_rect(left=WIDTH//2, top=HEIGHT//2)
def move(self):
key_pressed = pygame.key.get_pressed()
2. The next work arrangement
What we do now is:
When the left arrow of the keyboard is pressed, the square moves 10 pixels to the left ,
When the right arrow is pressed, the square moves 10 pixels to the right ;
In the same way, the up and down arrow keys are also processed in the same way.
By judging key_pressed
, it is easy to know whether the user has pressed the arrow keys.
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.image = pygame.Surface((60, 30))
self.image.fill("white")
self.rect = self.image.get_rect(left=WIDTH//2, top=HEIGHT//2)
def move(self):
key_pressed = pygame.key.get_pressed()
if key_pressed[K_UP]:
self.rect.move_ip(0, -10)
if key_pressed[K_LEFT]:
self.rect.move_ip(-10, 0)
If the key of ---ec3ef3f713da766822bbc2807224a200 key_pressed
is K_UP
, that is, the up arrow of the keyboard is pressed, the self.rect.move_ip
function of the small square is called to move the small square up 10 pixel.
In the parentheses of the move_ip
function, the first 0 means x, that is, x does not move, and the second number is -10, which means y, which moves the block up 10 pixels.
It is determined according to our coordinate system, the coordinate system of the game window is like this, the origin is at the left vertex, which is (0, 0), go right, x is positive, go down, y is positive!
Similarly, when the left arrow is pressed, the self.rect.move_ip
function of the block is called, and (-10,0) is passed in the parentheses;
Let's think about it, how to write the code that the block goes down and goes to the right? The code is this:
#...
def move(self):
key_pressed = pygame.key.get_pressed()
#...
if key_pressed[K_DOWN]:
self.rect.move_ip(0, 10)
#...
if key_pressed[K_RIGHT]:
self.rect.move_ip(10, 0)
Finally, in the while
loop, we also need to call the player.move
function, so that our little square can move!
while True:
for event in pygame.event.get():0
if event.type == QUIT:
sys.exit()
player.update()
player.move()
pygame.display.update()
Well, now let's run the program and see the effect.
When I press the arrow keys, it's over, and there is a problem again. The program didn't move the way I wanted.
This is because every time the while
cycle passes, the screen needs to be refreshed. In the while
loop, add a line of code:
screen.fill("black")
Run the program again:
3. Set the refresh rate of the screen
We can see that when pressing the up, down, left and right keys of the keyboard, the square runs very fast. What is the reason for this? It's because we don't have control over the refresh rate of the screen, which is the frame rate.
I add three lines of code to solve this problem:
pygame.init()
WIDTH = 600
HEIGHT = 800
# 设置帧速率大小
FPS = 30
# 创建Clock对象
clock = pygame.time.Clock()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
Then in the while
loop, below the pygame.display.update
update statement, call clock.tick(FPS)
to control the refresh rate of the screen.
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
screen.fill("black")
for sprite in all_sprites:
sprite.move()
sprite.update()
pygame.display.update()
# 控制屏幕的刷新频率
clock.tick(FPS)
Let's run the program again and see the effect! When we press the up, down, left, and right arrows of the keyboard, the square can also move normally, isn't it amazing?
I don't know if you have found a problem. When the block moves up and down, left and right, the block will run out of the window. Do you know how to solve this problem?
Follow Cat, we will deal with this bug in the next episode!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。