preface :

Article utilization Python pygame Do a snake game and make it clear what each piece of code is for .

It's said to be a snake game 1976 year ,Gremlin Classic arcade games launched by the company , Let's use it today Python This greedy Snake game is a pixel version , Although simple , But you can still play

What we need to do :

* Create game window
* Draw greedy snakes and food
* Snakes eat food
Chessboard model of greedy snake :

Start our code now , first , Or import module :
`import` `pygame` `import` `random` `import` `copy`
|

<>1. Create game window

<>1.1 Game initialization

|
`pygame.init()` `clock` `=` `pygame.time.Clock() ``# Set game clock ` `pygame.display.
set_caption(``" Greedy snake - answer , Source code , Relevant information can be sent to me privately "``) ``# Initialize title ` `screen` `=` `pygame.display.
set_mode((``500``,` `500``)) ``# Initialization window The size of the form is 500 500`
|

<>1.2 Initialize the location of the snake Snake length 10 10 That's the snake X Y coordinate

|

1

|
`snake_list` `=` `[[``10``,` `10``]]`
|

First, set a running direction of the snake Next, judge whether the keyboard event is determining the running direction of the snake

The snake can run , So what's next , Eating food increases their length and not eating food is displayed in different positions . For novices, Xiaobai wants to learn it more easily Python Basics ,Python Reptile ,web development , big data , Data analysis , Artificial intelligence and other technologies , Here to share the system teaching resources , Take off my lieutenant ( Tongying ):
2763177065 【 course / tool / method / Dispel doubts 】

Initial snake direction :

|
`move_up` `=` `False` `move_down` `=` `False` `move_left` `=` `False`
`move_right` `=` `True`
|

<>1.3 Initialize the location of the food

|
`x` `=` `random.randint(``10``,` `490``)` `y` `=` `random.randint(``10``,` `490
``)` `food_point` `=` `[x, y]`
|

<>1.4 Start game cycle

|
`running` `=` `True` `while` `running:` `# Game clock refresh frequency ` `clock.tick(``20``)`
|

<>1.5 The fill background is white

|
`screen.fill([``255``,` `255``,` `255``])` |
<>1.6 Draw background

|
`for` `x` `in` `range``(``0``,` `501``,` `10``):` `pygame.draw.line(screen, (``
195``,` `197``,` `199``), (x,` `0``), (x,` `500``),` `1``)` `pygame.draw.line(
screen, (``195``,` `197``,` `199``), (``0``, x), (``500``, x),` `1``)`
`food_rect` `=` `pygame.draw.circle(screen, [``255``,` `0``,` `0``], food_point,
` `15``,` `0``)`
|

<>1.7 Draw snake

|
`snake_rect` `=` `[]` `for` `pos` `in` `snake_list:` `# 1.7.1 Draw the body of a snake `
`snake_rect.append(pygame.draw.circle(screen, [``255``,` `0``,` `0``], pos,` `5
``,` `0``))`
|

<>2. Draw greedy snakes and food

<>2.1 Gets the length of the snake , Move the snake's body
`pos` `=` `len``(snake_list)` `-` `1` `while` `pos >` `0``:` `snake_list[pos]`
`=` `copy.deepcopy(snake_list[pos` `-` `1``])` `pos` `-``=` `1`
|

<>2.2 Change snakehead position
`if` `move_up:` `snake_list[pos][``1``]` `-``=` `10` `if` `snake_list[pos][``1
``] <` `0``:` `snake_list[pos][``1``]` `=` `500` `if` `move_down:` `snake_list[
pos][``1``]` `+``=` `10` `if` `snake_list[pos][``1``] >` `500``:` `snake_list[
pos][``1``]` `=` `0` `if` `move_left:` `snake_list[pos][``0``]` `-``=` `10` `if
` `snake_list[pos][``0``] <` `0``:` `snake_list[pos][``0``]` `=` `500` `if`
`move_right:` `snake_list[pos][``0``]` `+``=` `10` `if` `snake_list[pos][``0``]
>` `500``:` `snake_list[pos][``0``]` `=` `0` |
<>2.3 Keyboard controlled mobile position
`for` `event` `in` `pygame.event.get():` `# print(event)` `# Judge the pressed key ` `if`
`event.``type` `=``=` `pygame.KEYDOWN:` `# Up key ` `if` `event.key` `=``=` `pygame.
K_UP:` `move_up` `=` `True` `move_down` `=` `False` `move_left` `=` `False`
`move_right` `=` `False` `# Down key ` `if` `event.key` `=``=` `pygame.K_DOWN:`
`move_up` `=` `False` `move_down` `=` `True` `move_left` `=` `False`
`move_right` `=` `False` `# Left key ` `if` `event.key` `=``=` `pygame.K_LEFT:`
`move_up` `=` `False` `move_down` `=` `False` `move_left` `=` `True`
`move_right` `=` `False` `# Right click ` `if` `event.key` `=``=` `pygame.K_RIGHT:`
`move_up` `=` `False` `move_down` `=` `False` `move_left` `=` `False`
`move_right` `=` `True` |
<>2.4 Gets the length of the snake , Move the snake's body

|
`pos` `=` `len``(snake_list)` `-` `1` `while` `pos >` `0``:` `snake_list[pos]`
`=` `copy.deepcopy(snake_list[pos` `-` `1``])` `pos` `-``=` `1`
|

<>3. Snakes eat food

<>3.1 collision detection If snakes eat food
`if` `food_rect.collidepoint(pos):` `# Greedy snakes eat food ` `snake_list.append(food_point)
` `# Reset food location ` `food_point` `=` `[random.randint(``10``,` `490``), random.randint(
``10``,` `490``)]` `food_rect` `=` `pygame.draw.circle(screen, [``255``,` `0``,
` `0``], food_point,` `15``,` `0``)` `break` |
<>3.2 If the snake eats itself
| `head_rect` `=` `snake_rect[``0``]` `count` `=` `len``(snake_rect)` `while`
`count>` `1``:` `if` `head_rect.colliderect(snake_rect[count` `-` `1``]):`
`running` `=` `False` `count` `-``=` `1` `pygame.display.update()`
|

This is about Python Making the first mobile game of the year - Snake game ( practice ) That's all for my article , More relevant Python system
mobile game - Snake game content, please search python Tutorial introduction to learn the previous articles or continue to browse the relevant articles below. I hope you will support me in the future python Tutorial introduction !

Technology
©2019-2020 Toolsou All rights reserved,
evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possible Experiment 4 Automated test tools - software test mysql Export data sql sentence _mysql according to sql Query statement export data Create a thread ——— Javaweb (3) Data structure experiment ( three )—— Stacks and queues TS stay vue2 Writing in the project web Front end signature plug-in _signature_pad Plug in implements electronic signature function docker Where is the image stored Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control springboot How to get reality in ip address