A line of Python code plays childhood games

Posted by psychomossel on Mon, 20 Dec 2021 11:04:26 +0100

Write in front

Snake eater, bean eater, maze, tic tac toe game I'm sure you played these games when you were young, and maybe you were addicted to them at some time
With the growth of age, these games are farther and farther away from us, but I believe everyone's childlike innocence is still there
Today, let's share a real GitHub project --- Free Python games

Installation and use

python, as we all know, is very simple to install a third-party library

pip install freegames
"""
Of course I'm learning Python It will be difficult on the road. Without good learning materials, how to learn? 
study Python If you don't understand, it is recommended to join the exchange Q Group No.: 928946953 
There are like-minded friends in the group to help each other. There are good videos and tutorials in the group PDF!
And Daniel's answer!
"""

Python

copy

Since all the Games in the project are made based on the Python built-in module Turtle, there are not many dependencies and installation will not be difficult

After installation, we can use Python - M freenames list to view the list of all games

Figure 1

Greedy snake

Now we can start related games with one line of code, such as snake

python -m freegames.snake

Python

copy

Figure 2

Greedy snake's play must not be explained too much. Use the keyboard ⬆️⬇️⬅️➡️ Can control

Pac Man

Pac Man has never played and should have heard of it. Use the following code to start a game similar to pac man

python -m freegames.pacman

Python

copy

Figure 3

Flappy

The Flappy game is very similar to the previous very popular Flappy bird

Figure 4

Just change the game name to start

python -m freegames.flappy

Python

copy

Figure 5

This time, you need to click the mouse continuously to control the flight of small green dots. The actual measurement is a little more difficult than flappy bird

Memory

Memory translates to memory. The game gives some squares. Clicking on each small square will display the numbers hidden behind it, but it will disappear quickly. When you successfully select two identical numbers, it will be displayed as a puzzle, which is still very playable

python -m freegames.memory

Python

copy

Figure 8

maze

This game should not be introduced, just find out the corresponding path out of the maze

python -m freegames.maze

Python

copy

Figure 7

Tic Tac Toe

I bet you've played this game. Click the screen to place an X or O. when three of the same patterns are in a straight line, you win

python3 -m freegames.tictactoe

Python

copy

Figure 8

View source code

There are more games that will not be introduced here. We are not unfamiliar with Turtle, so when we play games, we should think about how developers implement it. Use the following code to generate the source code of the corresponding game in the current directory

"Free python games combine game and learning in a flexible environment to reduce the pressure on difficult topics such as programming"

python3 -m freegames copy snake

Python

copy

After execution, there will be one more snake in the current directory Py file, open it to view the corresponding logic of the game

"""Snake, classic arcade game.

Exercises

1. How do you make the snake faster or slower?
2. How can you make the snake go around the edges?
3. How would you move the food?
4. Change the snake to respond to arrow keys.

"""

from turtle import *
from random import randrange
from freegames import square, vector

food = vector(0, 0)
snake = [vector(10, 0)]
aim = vector(0, -10)

def change(x, y):
    "Change snake direction."
    aim.x = x
    aim.y = y

def inside(head):
    "Return True if head inside boundaries."
    return -200 < head.x < 190 and -200 < head.y < 190

def move():
    "Move snake forward one segment."
    head = snake[-1].copy()
    head.move(aim)

    if not inside(head) or head in snake:
        square(head.x, head.y, 9, 'red')
        update()
        return

    snake.append(head)

    if head == food:
        print('Snake:', len(snake))
        food.x = randrange(-15, 15) * 10
        food.y = randrange(-15, 15) * 10
    else:
        snake.pop(0)

    clear()

    for body in snake:
        square(body.x, body.y, 9, 'black')

    square(food.x, food.y, 9, 'green')
    update()
    ontimer(move, 100)

setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
onkey(lambda: change(10, 0), 'Right')
onkey(lambda: change(-10, 0), 'Left')
onkey(lambda: change(0, 10), 'Up')
onkey(lambda: change(0, -10), 'Down')
move()
done()

Python

copy

Now we can learn the source code and modify it to achieve more functions and gameplay. Let's try it!

Topics: Python