如何在pygame中使圆从一个角到另一个角对角移动 [英] How to make a circle move diagonally from corner to corner in pygame

查看:48
本文介绍了如何在pygame中使圆从一个角到另一个角对角移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序来为形状设置动画,我只移动了 x 轴或 y 轴,从来没有同时移动过这两个轴.所以对角移动对我来说是全新的.下面是我的代码:

"""添加一名作者日期:2021年1月20日描述:使用 pygame 动画形状""1.修改red_box的动画,当它命中时窗口的左侧或右侧改变方向.(完全的)2. 添加一个上下移动的 green_rect (40x20) Surface.当它到达顶部或底部时它应该改变它的 y 方向的窗户.(完全的)3. 添加一个以对角线路径移动的 blue_circle (24x24) 曲面.如果它碰到窗口的顶部或底部,则反转它的y 方向,如果它碰到窗口的左侧或右侧反转其 x 方向."导入pygame定义主():'''这个函数定义了我们游戏的'主线逻辑'.'''# I - 初始化pygame.init()# 展示屏幕 = pygame.display.set_mode((640, 480))pygame.display.set_caption(疯狂形状动画")# 实体背景 = pygame.Surface(screen.get_size())背景 = background.convert()background.fill((255, 255, 255)) # 白色背景# 制作一个红色的 25 x 25 盒子red_box = pygame.Surface((25, 25))red_box = red_box.convert()red_box.fill((255, 0, 0))# 制作一个绿色的 40 x 20 矩形green_rect = pygame.Surface((40, 20))green_rect = green_rect.convert()green_rect.fill((0, 255, 0))# 制作一个蓝色的 24 x 24 圆圈blue_circ = pygame.Surface((24, 24))blue_circ = blue_circ.convert()blue_circ.fill((0, 0, 255))pygame.draw.circle(blue_circ, (0, 0, 255), (25, 25), 24)# A - ACTION(分为 ALTER 步骤)# 分配时钟 = pygame.time.Clock()继续 = 真red_box_x = 0 # 分配起始(x,y)red_box_y = 200 # 对于我们的红框move_red_x = 5green_rect_x = 300 # 分配起始(x,y)green_rect_y = 200 # 用于我们的绿色矩形move_rect_y = 5blue_circ_x = 640 # 分配起始(x,y)blue_circ_y = 0 # 对于我们的蓝色圆圈move_circ_x = 5move_circ_y = 5# 环形同时继续:# 计时器时钟滴答(30)# 事件处理对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:继续 = 错误# 改变红框的x坐标red_box_x += move_red_x# 检查边界,从墙上反弹如果 red_box_x >= screen.get_width():red_box_x = screen.get_width()move_red_x *= -1如果 red_box_x <= 0:red_box_x = 0move_red_x *= -1# 改变绿色矩形的y坐标green_rect_y += move_rect_y# 检查边界,从墙上反弹如果 green_rect_y >= screen.get_height():green_rect_y = screen.get_height()move_rect_y *= -1如果 green_rect_y <= 0:green_rect_y = 0move_rect_y *= -1# REFRESH(更新窗口)screen.blit(背景, (0, 0))screen.blit(green_rect, (green_rect_x, green_rect_y)) # blit 矩形在新的 (x,y) 位置screen.blit(red_box, (red_box_x, red_box_y)) # blit box 在新的 (x,y) 位置pygame.display.flip()# 关闭游戏窗口pygame.quit()# 调用主函数主要的()

如果你能用我的风格完成它就太好了,这样我就能更好地理解它.非常感谢!

解决方案

我将提供一个更通用的方法.定义对象在途中应访问的位置列表 (circle_pos_list).设置起始位置(circle_pos)、速度(circle_speed)和列表中下一个目标位置的索引(next_pos_index).

circle_pos_list = [(100, 100), (200, 150), (100, 150)]circle_pos = circle_pos_list[0]圆圈速度 = 5next_pos_index = 1

使用

def main():# [...]blue_circ = pygame.Surface((24, 24))blue_circ.set_colorkey((0, 0, 0))pygame.draw.circle(blue_circ, (0, 0, 255), (12, 12), 12)# [...]circle_pos_list = [(100, 100), (200, 150), (100, 150)]circle_pos = circle_pos_list[0]圆圈速度 = 5next_pos_index = 1# 环形同时继续:# [...]circle_dir = pygame.math.Vector2(circle_pos_list[next_pos_index]) - circle_pos如果 circle_dir.length() <圈速:circle_pos = circle_pos_list[next_pos_index]next_pos_index = (next_pos_index + 1) % len(circle_pos_list)别的:circle_dir.scale_to_length(circle_speed)new_pos = pygame.math.Vector2(circle_pos) + circle_dircircle_pos = (new_pos.x, new_pos.y)# [...]screen.blit(blue_circ, (round(circle_pos[0]), round(circle_pos[1])))# [...]

I've created a program to animate shapes, and I've only moved either the x axis or y axis, never both at the same time. So moving diagonally is completely new to me. Below is my code:

"""
    Author: Victor Xu

    Date: January 20th, 2021

    Description: Animating Shapes with pygame
"""

"""
    1. Modify the animation of the red_box so that when it hits 
       the left or right side of the window it changes direction. (COMPLETED)

    2. Add a green_rect (40x20) Surface that moves in up and down.
       It should change its y direction when it hits to top or bottom 
       of the window. (COMPLETED)

    3. Add a blue_circle (24x24) Surface that moves in a diagonal path.
       If it hits the top or bottom of the window reverse its 
       y direction, and if it hits the left or right of the window
       reverse its x direction.
"""

import pygame


def main():
    '''This function defines the 'mainline logic' for our game.'''
    # I - INITIALIZE
    pygame.init()

    # DISPLAY
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("Crazy Shapes Animation")

    # ENTITIES
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((255, 255, 255))  # white background

    # Make a red 25 x 25 box
    red_box = pygame.Surface((25, 25))
    red_box = red_box.convert()
    red_box.fill((255, 0, 0))

    # Make a green 40 x 20 rectangle
    green_rect = pygame.Surface((40, 20))
    green_rect = green_rect.convert()
    green_rect.fill((0, 255, 0))

    # Make a blue 24 x 24 circle
    blue_circ = pygame.Surface((24, 24))
    blue_circ = blue_circ.convert()
    blue_circ.fill((0, 0, 255))
    pygame.draw.circle(blue_circ, (0, 0, 255), (25, 25), 24)

    # A - ACTION (broken into ALTER steps)

    # ASSIGN
    clock = pygame.time.Clock()
    keepGoing = True

    red_box_x = 0  # Assign starting (x,y)
    red_box_y = 200  # for our red box
    move_red_x = 5

    green_rect_x = 300  # Assign starting (x,y)
    green_rect_y = 200  # for our green rectangle
    move_rect_y = 5

    blue_circ_x = 640  # Assign starting (x,y)
    blue_circ_y = 0  # for our blue circle
    move_circ_x = 5
    move_circ_y = 5

    # LOOP
    while keepGoing:

        # TIMER
        clock.tick(30)

        # EVENT HANDLING
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False

        # change x coordinate of red box
        red_box_x += move_red_x
        # check boundaries, to bounce off wall
        if red_box_x >= screen.get_width():
            red_box_x = screen.get_width()
            move_red_x *= -1
        if red_box_x <= 0:
            red_box_x = 0
            move_red_x *= -1

        # change y coordinate of green rectangle
        green_rect_y += move_rect_y
        # check boundaries, to bounce off wall
        if green_rect_y >= screen.get_height():
            green_rect_y = screen.get_height()
            move_rect_y *= -1
        if green_rect_y <= 0:
            green_rect_y = 0
            move_rect_y *= -1

        # REFRESH (update window)
        screen.blit(background, (0, 0))
        screen.blit(green_rect, (green_rect_x, green_rect_y))  # blit rectangle at new (x,y) location
        screen.blit(red_box, (red_box_x, red_box_y))  # blit box at new (x,y) location
        pygame.display.flip()

    # Close the game window
    pygame.quit()


# Call the main function
main()

It would be great if you could complete it using my style, that way I would be able to understand it better. Thank you so much!

解决方案

I will offer a more general approach. Define a list of positions (circle_pos_list) that the object should visit on its way. Set the start position (circle_pos), the speed (circle_speed) and the index of the next target position in the list (next_pos_index).

circle_pos_list = [(100, 100), (200, 150), (100, 150)]
circle_pos = circle_pos_list[0]
circle_speed = 5
next_pos_index = 1

Use pygame.math.Vector2 to calculate the current distance vector of the circle to the next target position in the list:

circle_dir = pygame.math.Vector2(circle_pos_list[next_pos_index]) - circle_pos

If the distance to the target is less than the speed of the circle, step on the target and change the target location index to the next target:

if circle_dir.length() < circle_speed:
    circle_pos = circle_pos_list[next_pos_index]
    next_pos_index = (next_pos_index + 1) % len(circle_pos_list)

Otherwise, take a step along the vector to the next destination:

circle_dir.scale_to_length(circle_speed)
new_pos = pygame.math.Vector2(circle_pos) + circle_dir
circle_pos = (new_pos.x, new_pos.y)

Changes in your code:

def main():
    # [...]

    blue_circ = pygame.Surface((24, 24))
    blue_circ.set_colorkey((0, 0, 0))
    pygame.draw.circle(blue_circ, (0, 0, 255), (12, 12), 12)

    # [...]

    circle_pos_list = [(100, 100), (200, 150), (100, 150)]
    circle_pos = circle_pos_list[0]
    circle_speed = 5
    next_pos_index = 1

    # LOOP
    while keepGoing:
        # [...]

        circle_dir = pygame.math.Vector2(circle_pos_list[next_pos_index]) - circle_pos
        if circle_dir.length() < circle_speed:
            circle_pos = circle_pos_list[next_pos_index]
            next_pos_index = (next_pos_index + 1) % len(circle_pos_list)
        else:
            circle_dir.scale_to_length(circle_speed)
            new_pos = pygame.math.Vector2(circle_pos) + circle_dir
            circle_pos = (new_pos.x, new_pos.y)

        # [...]

        screen.blit(blue_circ, (round(circle_pos[0]), round(circle_pos[1])))

        # [...]

这篇关于如何在pygame中使圆从一个角到另一个角对角移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆