Trump shot on the sun - how to make games with python

 

 

1. Prepare

Before you begin, you want to make sure Python and pip has been successfully installed on the computer Well, if not, please visit this article: ultra-detailed installation guide to install Python.

Open the Windows environment Cmd (Start - Run -CMD), at Apple's system environment, open Terminal (command + space enter Terminal), ready to enter the command to install the dependencies.

Enter the following command to install the dependent modules we need:

pip install freegames 
pip install turtle

Successfully installed xxx see the installation was successful.

2. Write code

The principle is very simple, is to use the Trump comic drawing sheets and freegames module, and turtle drawing module.

And setting image loading module 2.1

First, the introduction of turtle module, and freegames module, an experiment that we need to use the module in freegames vector vector (used to represent coordinates).

import turtle
from random import randrange
from freegames import vector
 
# 设定screen
screen = turtle.Screen()
screen.setup(420, 420, 370, 0)
 
# 加载trump图,并设为默认turtle
trump = '3.gif'
screen.addshape(trump)
turtle.shape(trump) 

2.2 Preparation Paint

Before you start drawing, due to the need to use the Trump picture as a mobile point, we need to hide the original turtle object and set does not display the tracer, namely Trump moved when not drawing a line.

The last set when the user clicks on the canvas when the tap function to perform.

turtle.hideturtle()
turtle.up()
turtle.tracer(False)
turtle.onscreenclick(tap) 

tap function is as follows, i.e., the setting position and the initial velocity of the ball.

def tap(x, y):
    """
    回应屏幕点击
    :param x: x轴位置
    :param y: y轴位置
    """
    if not inside(ball):
        ball.x = -199
        ball.y = -199
        speed.x = (x + 200) / 25
        speed.y = (y + 200) / 25 

2.3 Start Draw

The core function of this part is to move, but said before the move function, we need to focus on to talk about how to replace the original endpoint Trump's head, and that is the function draw function:

def draw():
    """
    绘画出太阳和trump
    """
    turtle.hideturtle()
    turtle.clear()
 
    for target in targets:
        turtle.goto(target.x, target.y)
        turtle.dot(20, 'red')
 
    if inside(ball):
        turtle.showturtle()
        turtle.goto(ball.x, ball.y)
 
    turtle.update() 

Remember we outset Trump's picture is set to turtle's shape yet? Then move before the function is run, turn turtle hiding spot, in fact, this is the time to hide Trump's head. And at the beginning of the movement, we only need to show the turtle back to back. Therefore, the core statement is:

turtle.showturtle()
turtle.goto(ball.x, ball.y)

These two control the head of the movement.

Then take a look move the function body:

def move():
    """
    移动太阳和trump
    :return:
    """
 
    # 生成“太阳”球体
    if randrange(40) == 0:
        y = randrange(-150, 150)
        target = vector(200, y)
        targets.append(target)
 
    # 移动太阳
    for target in targets:
        target.x -= 0.5
 
    # 如果Trump在屏幕内,减速并移动
    if inside(ball):
        speed.y -= 0.35
        ball.move(speed)
 
    # 重新渲染“太阳”位置
    dupe = targets.copy()
    targets.clear()
 
    # 和Trump距离太近,则消去球体
    for target in dupe:
        if abs(target - ball) > 13:
            targets.append(target)
 
    # 渲染画布
    draw()
 
    # 没有目标了则终止游戏
    for target in targets:
        if not inside(target):
            return
 
    # 每隔50毫秒递归调用本函数
    turtle.ontimer(move, 50) 

In fact, a comment written in very clear, but here we explain it in detail:

1. First need to generate the "sun" sphere, Vector is used here, is a function for generating coordinates, y is randomly generated, the initial position of the ball in the rightmost (200, y). 2. translation of all the sun, including the new. 3. If the screen memory at Trump head to make it move. 4. re-rendering position of the sun, instead of using the new coordinate, if too close, and Trump, then erasing the sphere. 5 render all spheres and Trump's new location. 6. If no ball, the game is terminated, or re call this function every 50 milliseconds.

Based on this logic you can write one of their own game, oh.

The final renderings:

 

 

If you want to know more about the application of python, can private letter to me, whether you are a zero-based foundation can still get into their corresponding learning package! Including Python software tools and the latest entry to the 2019 combat tutorial, (http://t.cn/A6Zvjdun) copied to the browser open! python technological learning exchange group: 695 185 429

Guess you like

Origin www.cnblogs.com/python0921/p/12567137.html