Python 如何在pygame中使圆从一个角对角移动到另一个角
我创建了一个程序来设置形状的动画,我只移动了x轴或y轴,从来没有同时移动过这两个轴。所以对角移动对我来说是完全陌生的。下面是我的代码:Python 如何在pygame中使圆从一个角对角移动到另一个角,python,pygame,Python,Pygame,我创建了一个程序来设置形状的动画,我只移动了x轴或y轴,从来没有同时移动过这两个轴。所以对角移动对我来说是完全陌生的。下面是我的代码: """ Author: Victor Xu Date: January 20th, 2021 Description: Animating Shapes with pygame """ """ 1. Modify the animati
"""
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()
“”“
作者:维克多·徐
日期:2021年1月20日
描述:使用pygame设置形状动画
"""
"""
1.修改红色_框的动画,使其在点击时
改变方向的窗口左侧或右侧。(已完成)
2.添加上下移动的绿色矩形(40x20)曲面。
当它撞到顶部或底部时,它应该改变其y方向
(已完成)
3.添加一个沿对角线路径移动的蓝色圆圈(24x24)曲面。
如果它碰到窗口的顶部或底部,则将其反转
y方向,如果它击中窗口的左侧或右侧
反转其x方向。
"""
导入pygame
def main():
''此函数定义了我们游戏的'主线逻辑'''
#I-初始化
pygame.init()
#展示
screen=pygame.display.set_模式((640480))
pygame.display.set_标题(“疯狂形状动画”)
#实体
background=pygame.Surface(screen.get_size())
background=background.convert()
背景。填充((255,255,255))#白色背景
#做一个红色的25x25盒子
红盒=pygame.Surface((25,25))
red\u box=red\u box.convert()
红盒填充((255,0,0))
#制作一个40 x 20的绿色矩形
green_rect=pygame.Surface((40,20))
green\u rect=green\u rect.convert()
绿色矩形填充((0,255,0))
#做一个24 x 24的蓝色圆圈
blue_circ=pygame.Surface((24,24))
蓝色循环=蓝色循环转换()
蓝圈填充((0,0,255))
pygame.draw.circle(蓝色圆圈,(0,0,255)、(25,25,24)
#A-动作(分成不同的步骤)
#分配
clock=pygame.time.clock()
继续=正确
红色方框x=0#指定开始(x,y)
红色盒子y=200#用于我们的红色盒子
移动\u红色\u x=5
绿色_rect_x=300#指定开始(x,y)
绿色矩形y=200#表示我们的绿色矩形
移动方向y=5
蓝圈x=640#分配启动(x,y)
蓝色圆圈y=0表示我们的蓝色圆圈
移动循环x=5
移动循环y=5
#环路
继续进行时:
#计时器
时钟滴答(30)
#事件处理
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
继续=错误
#更改红色框的x坐标
红色方框x+=移动红色方框x
#检查边界,以便从墙上反弹
如果红色\u框\u x>=屏幕。获取\u宽度():
红色方框x=屏幕。获取宽度()
移动红色x*=-1
如果红色方框x=屏幕,则获取高度()
绿色=屏幕。获取高度()
移动方向y*=-1
如果green\u rect\y我将提供一种更通用的方法。定义对象在途中应该访问的位置列表(圆圈位置列表
)。设置开始位置(circle\u pos
)、速度(circle\u speed
)和列表中下一个目标位置的索引(next\u pos\u index
)
circle_pos_list=[(100100),(200150),(100150)]
圆圈位置=圆圈位置列表[0]
圆周速度=5
下一个位置索引=1
用于计算圆到列表中下一个目标位置的当前距离向量:
circle\u dir=pygame.math.Vector2(circle\u pos\u列表[下一个位置索引])-circle\u pos
如果到目标的距离小于圆圈速度,则踩下目标并将目标位置索引更改为下一个目标:
if circle\u dir.length()
否则,沿着向量向前走一步,到达下一个目的地:
circle\u dir.缩放到长度(circle\u speed)
new\u pos=pygame.math.Vector2(圆圈位置)+圆圈方向
圆圈位置=(新位置x,新位置y)
代码中的更改:
def main():
# [...]
blue_circ=pygame.Surface((24,24))
蓝色循环设置颜色键((0,0,0))
pygame.draw.circle(蓝色圆圈,(0,0,255),(12,12,12)
# [...]
圆圈位置列表=[(100100),(200150),(100150)]
圆圈位置=圆圈位置列表[0]
圆周速度=5
下一个位置索引=1
#环路
继续进行时:
# [...]
circle\u dir=pygame.math.Vector2(circle\u pos\u列表[下一个位置索引])-circle\u pos
如果圆的方向长度()小于圆的速度:
圆圈位置=圆圈位置列表[下一个位置索引]
下一个位置索引=(下一个位置索引+1)%len(圆圈位置列表)
其他:
圆圈方向刻度至长度(圆圈速度)
new\u pos=pygame.math.Vector2(圆圈位置)+圆圈方向
圆圈位置=(新位置x,新位置y)
# [...]
blit(蓝色圆圈,(圆形(圆圈位置[0]),圆形(圆圈位置[1]))
# [...]
同时更改形状的x和y位置有什么困难只需复制一个轴的逻辑,并将其应用于相同形状的另一个轴。如果对每个轴应用相同的步长,将沿对角线移动形状。哦,我没想到。。。我做到了,而且成功了。非常感谢你!我如何摆脱“初学者”的心态?我最近才开始学习Python