[Python] I installed the game from pip and played it

Motive Game programming is built in as Python Games by default on Raspberry Pi, but I wondered if it could be done on Mac as well. All the games on the Raspberry Pi are made by myself, so it seemed that I couldn't get them completely, but I will introduce them because there was a similar library. It is "Free Python Games", a library created in 2012 for school education purposes. It seems that it is usually used in middle and high schools in the United States.

Method

Virtual environment construction + activation

python3 -m venv venv_game
source ./venv_game/bin/activate

Install the game

pip install freegames

List games

python3 -m freegames list

ant
bagels
bounce
cannon
connect
crypto
fidget
flappy
guess
life
maze
memory
minesweeper
pacman
paint
pong
simonsays
snake
tictactoe
tiles
tron

There are 21 types.

Play the game

python3 -m freegames.pacman

You can play Pac-Man.

pacman (From Free Python Games --Main page)

It's a common -m option, but it seems to run insidemain ()in the module.

If you don't know how to use it ...

Guess from the code on the Free Python Games Main page or Github To do. There are some games that are difficult to operate at first glance because there is no instruction manual if there is no hint.

An example is pong (a table tennis game released by Atari. The world's first home video game).

pong.py


#Line:86-89
onkey(lambda: move(1, 20), 'w')
onkey(lambda: move(1, -20), 's')
onkey(lambda: move(2, 20), 'i')
onkey(lambda: move(2, -20), 'k')

player1 can operate the paddle up and down with w`` s key and player2 with ʻi`` k`.

Learn The comments in the code at pacman contain exercises.

python


"""Pacman, classic arcade game.
Exercises

1. Change the board. (Let's change the board)
2. Change the number of ghosts. (Let's change the number of enemy characters)
3. Change where pacman starts. (Let's change the starting position of Pac-Man)
4. Make the ghosts faster/slower. (Let's change the speed of the enemy character)
5. Make the ghosts smarter. (Let's make the enemy character smarter)

"""

It seems that 1 to 4 are good, but the final problem requires full-scale thinking such as using machine learning or deep learning.

When you want to modify the code and implement it

python3 -m freegames copy pacman
python3 pacman.py

If you simply change the color of the enemy character from red to green, it will be as follows.

pacman.py



    #Line:126-142
    #Maybe I'm moving an enemy character here
    for point, course in ghosts:
        if valid(point + course):
            point.move(course)
        else:
            options = [
                vector(5, 0),
                vector(-5, 0),
                vector(0, 5),
                vector(0, -5),
            ]
            plan = choice(options)
            course.x = plan.x
            course.y = plan.y

        up()
        goto(point.x + 10, point.y + 10)
        dot(20, 'green') #dot(20, 'red')

output.gif

Future I introduced a Demo game that runs on python. Since games like Pong introduced above are also made simply, there is no CPU, so if you want to play alone, you will have to make your own CPU. It's an old NES, but how did you implement the movement of enemy characters?: Sweat_smile:

Reference

Recommended Posts

[Python] I installed the game from pip and played it
[Python] I introduced Word2Vec and played with it.
I played with PyQt5 and Python3
I want to know the features of Python and pip
I installed the retro game engine pyxel for Python on Mac and started the sample code
Life game with Python [I made it] (on the terminal & Tkinter)
I installed DSX Desktop and tried it
I installed and used Numba with Python3.5
From Python 3.4, pip becomes the standard installer! ??
I set the environment variable with Docker and displayed it in Python
[python] Send the image captured from the webcam to the server and save it
Note that I understand the least squares algorithm. And I wrote it in Python.
I tried using Google Translate from Python and it was just too easy
[Python] pip and wheel
I tried to graph the packages installed in Python
I installed and used the Deep Learning library Chainer
I wanted to use the Python library from MATLAB
I checked out the versions of Blender and Python
Get mail from Gmail and label it with Python3
Install the latest Python from pyenv installed with homebrew
I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
I want to replace the variables in the python template file and mass-produce it in another file.
Read the function name from the DB and execute it dynamically
I tried using the Python library from Ruby with PyCall
I compared Java and Python!
When I installed python on macOS and used it, I got an error when I put an https connection
I tweeted from the terminal!
I made a server with Python socket and ssl and tried to access it from a browser
I tried programming the chi-square test in Python and Java.
The process of making Python code object-oriented and improving it
I tried to enumerate the differences between java and python
Object-oriented in C: Refactored "○ ✕ game" and ported it to Python
I want to cut out only the face from a person image with Python and save it ~ Face detection and trimming with face_recognition ~
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
[Python] Check the installed libraries
I downloaded the python source
After changing the account name on Mac, pip and ipython became unusable, so I reinstalled the whole python and it was cured
Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container
[Python] What is pip? Explain the command list and how to use it with actual examples
Read the old Gakushin DC application Word file (.doc) from Python and try to operate it.
I set the python operating environment on OSX Yosemite (pyenv + pip)
I compared the speed of Hash with Topaz, Ruby and Python
[First Ripple] [I am] Call JavaScript (Node.js) from Python and process Ripple
Install mecab on Sakura shared server and call it from python
[PEP8] Take over the Python source code and write it neatly
How to get followers and followers from python using the Mastodon API
[Introduction to Python] I compared the naming conventions of C # and Python.
I installed Swift tools to use ObjectStorage @ Bluemix (Python / pip, etc.)
[Python] I thoroughly explained the theory and implementation of decision trees
[Super basics of Python] I learned the basics of the basics, so I summarized it briefly.
Convert the result of python optparse to dict and utilize it
Operate Firefox with Selenium from python and save the screen capture
The story of Python and the story of NaN
Install Python 2.7.9 and Python 3.4.x with pip.
Python, yield, return, and sometimes yield from
I installed the IoT platform "Rimotte"
Existence from the viewpoint of Python
Read and use Python files from Python
About Python, from and import, as
I liked the tweet with python. ..