pygame教你从0到1一步步实现点到点的智能追踪系统(其二)

续上篇,上篇实现了点到点的智能追踪系统,这篇将实现图片到目标点的智能追踪。
还是参照上篇的代码,将追踪点修改为图片,实现一个版本。

文章目录

一、失败的图片到目标点版本

(一)核心代码

增加plane的对象:

plane = pygame.image.load('plane.png').convert_alpha()

在循环过程中blit图形到界面上显示:

screen.blit(plane,  (int(x1), int(y1)))

(二)完整代码

import pygame,sys
from math import *
pygame.init()
screen=pygame.display.set_mode((800,700))
plane = pygame.image.load('plane.png').convert_alpha()
x1,y1=100,600
x,y =500,200
velocity=0.8
clock=pygame.time.Clock()
while True:
    x, y = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
    clock.tick(300)
    distance=sqrt(pow(x1-x,2)+pow(y1-y,2))
    section=velocity
    sina=(y1-y)/distance
    cosa=(x-x1)/distance
    angle=atan2(y-y1,x-x1)
    x1,y1=(x1+section*cosa,y1-section*sina)
    screen.fill((0,0,0))
    screen.blit(plane,  (int(x1), int(y1)))

    pygame.draw.circle(screen, (0, 0, 255), (x,y), 10)
    pygame.display.update()

(三)效果显示

pygame教你从0到1一步步实现点到点的智能追踪系统(其二)
可以发现,有两个问题。

; 二、失败原因分析

1、图片在运行过程中,不会旋转角度,让机头跟随鼠标运动。
2、图片框的中心点位置不对,需要做矫正。

三、修订版本

(一)让图片跟随鼠标运动旋转

1、核心代码

    angle=atan2(y-y1,x-x1)
    angle = atan2(y - y1, x - x1)
    fangle = degrees(angle)
    x1,y1=(x1+section*cosa,y1-section*sina)
    planed = pygame.transform.rotate(plane, -(fangle))
    screen.blit(planed,  (int(x1), int(y1)))

2、完整代码

import pygame,sys
from math import *
pygame.init()
screen=pygame.display.set_mode((800,700))
plane = pygame.image.load('plane.png').convert_alpha()
x1,y1=100,600
x,y =500,200
velocity=0.8
clock=pygame.time.Clock()
while True:
    x, y = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
    clock.tick(300)
    distance=sqrt(pow(x1-x,2)+pow(y1-y,2))
    section=velocity
    sina=(y1-y)/distance
    cosa=(x-x1)/distance
    angle=atan2(y-y1,x-x1)
    angle = atan2(y - y1, x - x1)
    fangle = degrees(angle)
    x1,y1=(x1+section*cosa,y1-section*sina)
    planed = pygame.transform.rotate(plane, -(fangle))
    screen.fill((0,0,0))
    screen.blit(planed,  (int(x1), int(y1)))

    pygame.draw.circle(screen, (0, 0, 255), (x,y), 10)
    pygame.display.update()

3、运行效果

pygame教你从0到1一步步实现点到点的智能追踪系统(其二)

; (二)矫正图形的位置

1、核心代码

height = plane.get_height()
width = plane.get_width()

if 0  -fangle  90:
    A = (width * cosa + x1 - width, y1 - height / 2)
    B = (A[0] + height * sina, A[1] + height * cosa)
if 90 < -fangle  180:
    A = (x1 - width, y1 - height / 2 + height * (-cosa))
    B = (x1 - width + height * sina, y1 - height / 2)
if -90  -fangle < 0:
    A = (x1 - width + planed.get_width(), y1 - height / 2 + planed.get_height() - height * cosa)
    B = (A[0] + height * sina, y1 - height / 2 + planed.get_height())
if -180 < -fangle < -90:
    A = (x1 - width - height * sina, y1 - height / 2 + planed.get_height())
    B = (x1 - width, A[1] + height * cosa)
C = ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2)
screen.blit(planed, (x1 - width + (x1 - C[0]), y1 - height / 2 + (y1 - C[1])))

2、完整代码

import pygame,sys
from math import *
pygame.init()
screen=pygame.display.set_mode((800,700))
plane = pygame.image.load('plane.png').convert_alpha()
x1,y1=100,600
x,y =500,200
velocity=0.8
clock=pygame.time.Clock()
height = plane.get_height()
width = plane.get_width()
while True:
    x, y = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
    clock.tick(300)
    distance=sqrt(pow(x1-x,2)+pow(y1-y,2))
    section=velocity
    sina=(y1-y)/distance
    cosa=(x-x1)/distance
    angle=atan2(y-y1,x-x1)
    angle = atan2(y - y1, x - x1)
    fangle = degrees(angle)
    x1,y1=(x1+section*cosa,y1-section*sina)
    planed = pygame.transform.rotate(plane, -(fangle))
    screen.fill((0,0,0))
    if 0  -fangle  90:
        A = (width * cosa + x1 - width, y1 - height / 2)
        B = (A[0] + height * sina, A[1] + height * cosa)

    if 90 < -fangle  180:
        A = (x1 - width, y1 - height / 2 + height * (-cosa))
        B = (x1 - width + height * sina, y1 - height / 2)

    if -90  -fangle < 0:
        A = (x1 - width + planed.get_width(), y1 - height / 2 + planed.get_height() - height * cosa)
        B = (A[0] + height * sina, y1 - height / 2 + planed.get_height())

    if -180 < -fangle < -90:
        A = (x1 - width - height * sina, y1 - height / 2 + planed.get_height())
        B = (x1 - width, A[1] + height * cosa)

    C = ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2)

    screen.blit(planed, (x1 - width + (x1 - C[0]), y1 - height / 2 + (y1 - C[1])))

    pygame.draw.circle(screen, (0, 0, 255), (x,y), 10)
    pygame.display.update()

3、运行效果

pygame教你从0到1一步步实现点到点的智能追踪系统(其二)
大致的雏形已经出来了,后续是完善代码成函数和类,并增加更多趣味性的内容了。

; 四、核心代码块封装成函数

(一)核心代码块

def move(x,y,plane,x1,y1,velocity):
    height = plane.get_height()
    width = plane.get_width()
    distance = sqrt(pow(x1 - x, 2) + pow(y1 - y, 2))
    section = velocity
    sina = (y1 - y) / distance
    cosa = (x - x1) / distance
    angle = atan2(y - y1, x - x1)
    fangle = degrees(angle)
    x1, y1 = (x1 + section * cosa, y1 - section * sina)
    planed = pygame.transform.rotate(plane, -(fangle))
    if 0  -fangle  90:
        A = (width * cosa + x1 - width, y1 - height / 2)
        B = (A[0] + height * sina, A[1] + height * cosa)
    if 90 < -fangle  180:
        A = (x1 - width, y1 - height / 2 + height * (-cosa))
        B = (x1 - width + height * sina, y1 - height / 2)
    if -90  -fangle < 0:
        A = (x1 - width + planed.get_width(), y1 - height / 2 + planed.get_height() - height * cosa)
        B = (A[0] + height * sina, y1 - height / 2 + planed.get_height())
    if -180 < -fangle < -90:
        A = (x1 - width - height * sina, y1 - height / 2 + planed.get_height())
        B = (x1 - width, A[1] + height * cosa)
    C = ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2)
    screen.blit(planed, (x1 - width + (x1 - C[0]), y1 - height / 2 + (y1 - C[1])))

(二)增加碰撞检测

碰撞了返回True,否则返回False


if abs(x-x1)<5 and abs(y - y1)<5:
    print(x - x1, y - y1)
    return True,x1,y1
else:
    return False,x1,y1

(三)完整代码

import pygame,sys
from math import *

def move(x,y,plane,x1,y1,velocity):
    height = plane.get_height()
    width = plane.get_width()
    distance = sqrt(pow(x1 - x, 2) + pow(y1 - y, 2))
    section = velocity
    sina = (y1 - y) / distance
    cosa = (x - x1) / distance
    angle = atan2(y - y1, x - x1)
    fangle = degrees(angle)
    x1, y1 = (x1 + section * cosa, y1 - section * sina)
    planed = pygame.transform.rotate(plane, -(fangle))
    if 0  -fangle  90:
        A = (width * cosa + x1 - width, y1 - height / 2)
        B = (A[0] + height * sina, A[1] + height * cosa)
    if 90 < -fangle  180:
        A = (x1 - width, y1 - height / 2 + height * (-cosa))
        B = (x1 - width + height * sina, y1 - height / 2)
    if -90  -fangle < 0:
        A = (x1 - width + planed.get_width(), y1 - height / 2 + planed.get_height() - height * cosa)
        B = (A[0] + height * sina, y1 - height / 2 + planed.get_height())
    if -180 < -fangle < -90:
        A = (x1 - width - height * sina, y1 - height / 2 + planed.get_height())
        B = (x1 - width, A[1] + height * cosa)
    C = ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2)
    screen.blit(planed, (x1 - width + (x1 - C[0]), y1 - height / 2 + (y1 - C[1])))

    if abs(x-x1)<5 and abs(y - y1)<5:
        print(x - x1, y - y1)
        return True,x1,y1
    else:
        return False,x1,y1

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800, 700))
    plane = pygame.image.load('plane.png').convert_alpha()
    x1, y1 = 100, 600
    x, y = 500, 200
    velocity = 0.8
    clock = pygame.time.Clock()
    while True:
        x, y = pygame.mouse.get_pos()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        clock.tick(300)
        screen.fill((0, 0, 0))
        T, x1, y1 = move(x, y, plane, x1, y1, velocity)
        pygame.draw.circle(screen, (0, 0, 255), (x, y), 10)
        pygame.display.update()

五、趣味性的小游戏

(一)让飞机跟着运动的小球跑

让飞机跟着运动的小球跑,然后碰撞了就重新更新小球的位置,持续动起来

1、BALL类

from random import randint
import pygame

class Ball:

    def __init__(self, width,height,screen):
        self.scrnwidth = width
        self.scrnheight = height
        self.screen = screen
        self.reset()
        self.color = (randint(0,255),randint(0,255),randint(0,255))

    def draw_ball(self):
        pygame.draw.circle(self.screen, self.color, (self.xpos, self.ypos), self.radius)

    def reset(self):
        self.radius = randint(2, 10)
        self.xpos = randint(self.radius, int(self.scrnwidth)-self.radius)
        self.ypos = randint(self.radius, int(self.scrnheight)-self.radius)
        self.xvelocity = randint(1, 2)
        self.yvelocity = randint(1, 2)
        self.color = (randint(0,255),randint(0,255),randint(0,255))

    def move(self):

        self.xpos += self.xvelocity
        self.ypos += self.yvelocity

        if self.ypos >= self.scrnheight - self.radius:
            self.yvelocity = -self.yvelocity

        if self.ypos  self.radius:
            self.yvelocity = abs(self.yvelocity)

        if self.xpos >= self.scrnwidth - self.radius:
            self.xvelocity = -self.xvelocity

        if self.xpos  self.radius:
            self.xvelocity = abs(self.xvelocity)

2、初始化BALL类

myball = Ball(800, 700, screen)

3、让BALL运动起来并画在画布上

myball.move()
myball.draw_ball()

4、让飞机跟随运动小球追踪

T, x1, y1 = move(myball.xpos, myball.ypos, plane, x1, y1, velocity)

5、碰撞监测,碰撞后重置小球

if T:
    myball.reset()

6、完整代码

import pygame,sys
from math import *
from Ball import Ball

def move(x,y,plane,x1,y1,velocity):
    height = plane.get_height()
    width = plane.get_width()
    distance = sqrt(pow(x1 - x, 2) + pow(y1 - y, 2))
    section = velocity
    sina = (y1 - y) / distance
    cosa = (x - x1) / distance
    angle = atan2(y - y1, x - x1)
    fangle = degrees(angle)
    x1, y1 = (x1 + section * cosa, y1 - section * sina)
    planed = pygame.transform.rotate(plane, -(fangle))
    if 0  -fangle  90:
        A = (width * cosa + x1 - width, y1 - height / 2)
        B = (A[0] + height * sina, A[1] + height * cosa)
    if 90 < -fangle  180:
        A = (x1 - width, y1 - height / 2 + height * (-cosa))
        B = (x1 - width + height * sina, y1 - height / 2)
    if -90  -fangle < 0:
        A = (x1 - width + planed.get_width(), y1 - height / 2 + planed.get_height() - height * cosa)
        B = (A[0] + height * sina, y1 - height / 2 + planed.get_height())
    if -180 < -fangle < -90:
        A = (x1 - width - height * sina, y1 - height / 2 + planed.get_height())
        B = (x1 - width, A[1] + height * cosa)
    C = ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2)
    screen.blit(planed, (x1 - width + (x1 - C[0]), y1 - height / 2 + (y1 - C[1])))

    if abs(x-x1)<5 and abs(y - y1)<5:
        print(x - x1, y - y1)
        return True,x1,y1
    else:
        return False,x1,y1

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800, 700))
    plane = pygame.image.load('plane.png').convert_alpha()
    x1, y1 = 100, 600
    x, y = 500, 200
    velocity = 0.8
    myball = Ball(800, 700, screen)
    clock = pygame.time.Clock()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        clock.tick(300)
        screen.fill((0, 0, 0))
        myball.move()
        myball.draw_ball()
        T, x1, y1 = move(myball.xpos, myball.ypos, plane, x1, y1, velocity)
        if T:
            myball.reset()

        pygame.display.update()

7、运行效果

pygame教你从0到1一步步实现点到点的智能追踪系统(其二)

; 8、解决飞机追不上小球的问题

核心代码如下:

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        clock.tick(300)
        screen.fill((0, 0, 0))
        myball.move()
        myball.draw_ball()
        T, x1, y1 = move(myball.xpos, myball.ypos, plane, x1, y1, velocity)
        velocity += 0.001
        if T:
            myball.reset()
            velocity = 0.8

        pygame.display.update()

9、优化后的运行效果

pygame教你从0到1一步步实现点到点的智能追踪系统(其二)

; (二)让多个飞机争抢运动小球

1、让plane呈现不同的颜色

plane = pygame.image.load('hero.png').convert_alpha()
plane.fill((0,0,250,10),special_flags = pygame.BLEND_ADD)
plane1 = pygame.image.load('hero.png').convert_alpha()
plane1.fill((250,0,250,10),special_flags = pygame.BLEND_ADD)

2、完整代码

import pygame,sys
from math import *
from Ball import Ball
import random

def move(x,y,plane,x1,y1,velocity):
    height = plane.get_height()
    width = plane.get_width()
    distance = sqrt(pow(x1 - x, 2) + pow(y1 - y, 2))
    section = velocity
    sina = (y1 - y) / distance
    cosa = (x - x1) / distance
    angle = atan2(y - y1, x - x1)
    fangle = degrees(angle)
    x1, y1 = (x1 + section * cosa, y1 - section * sina)
    planed = pygame.transform.rotate(plane, -(fangle))
    if 0  -fangle  90:
        A = (width * cosa + x1 - width, y1 - height / 2)
        B = (A[0] + height * sina, A[1] + height * cosa)
    if 90 < -fangle  180:
        A = (x1 - width, y1 - height / 2 + height * (-cosa))
        B = (x1 - width + height * sina, y1 - height / 2)
    if -90  -fangle < 0:
        A = (x1 - width + planed.get_width(), y1 - height / 2 + planed.get_height() - height * cosa)
        B = (A[0] + height * sina, y1 - height / 2 + planed.get_height())
    if -180 < -fangle < -90:
        A = (x1 - width - height * sina, y1 - height / 2 + planed.get_height())
        B = (x1 - width, A[1] + height * cosa)
    C = ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2)
    screen.blit(planed, (x1 - width + (x1 - C[0]), y1 - height / 2 + (y1 - C[1])))

    if abs(x-x1)<5 and abs(y - y1)<5:
        print(x - x1, y - y1)
        return True,x1,y1
    else:
        return False,x1,y1

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800, 700))
    plane = pygame.image.load('plane.png').convert_alpha()
    plane.fill((0, 0, 250, 10), special_flags=pygame.BLEND_ADD)
    plane1 = pygame.image.load('plane.png').convert_alpha()
    plane1.fill((250, 0, 250, 10), special_flags=pygame.BLEND_ADD)
    x1, y1 = 100, 600
    x, y = 500, 200
    x2 = 300
    y2 = 200
    flag = random.randint(200, 300)/1000
    velocity = 0.8
    myball = Ball(800, 700, screen)
    clock = pygame.time.Clock()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        clock.tick(300)
        screen.fill((0, 0, 0))
        myball.move()
        myball.draw_ball()
        T, x1, y1 = move(myball.xpos, myball.ypos, plane, x1, y1, velocity)

        _, x2, y2 = move(myball.xpos, myball.ypos, plane1, x2, y2, velocity - flag)
        velocity += 0.001
        if T:
            myball.reset()
            velocity = 0.8

        pygame.display.update()

3、运行效果

pygame教你从0到1一步步实现点到点的智能追踪系统(其二)

感谢各位的阅读,比心!

Original: https://blog.csdn.net/dhjabc_1/article/details/116650392
Author: dhjabc_1
Title: pygame教你从0到1一步步实现点到点的智能追踪系统(其二)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/783361/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球