python-如何使球在pygame中从三角形d回?

python-如何使球在pygame中从三角形d回?,第1张

概述您好,我是一个非常新的程序员,我正在尝试使球从45度三角形d起.这是我的代码:这个程序使球碰到窗户的侧面时会d跳,但是我不知道如何使它d起三角形.import pygame # importing the pygame import sys # importing the system libraries import time # importing ti

您好,我是一个非常新的程序员,我正在尝试使球从45度三角形d起.这是我的代码:

这个程序使球碰到窗户的侧面时会d跳,但是我不知道如何使它d起三角形.

import pygame # importing the pygameimport sys # importing the system librarIEsimport time # importing timerimport randomfrom pygame.locals import * # importing the locals functions from the pygame library setpygame.init() # the function from pygame that initializes all relevant variable# setting length and wIDthwIDth = 500length = 300# colour variablesWHITE = (255,255,255)BLUE = (0,255)# importing ball imageball = pygame.image.load('ball.png')ballRect = ball.get_rect()ballRect.left = 300 ballRect.right = 300# setting speedx_speed = 2y_speed = 2# setting window sizeWINDOW = pygame.display.set_mode((wIDth,length))# setting the size of the windowpygame.display.update()# loopwhile True: for event in pygame.event.get():     if event.type == QUIT:         pygame.quit()         sys.exit() ballRect = ballRect.move(x_speed,y_speed) WINDOW.fill(WHITE) # changing screen colour to white WINDOW.blit(ball,ballRect) # printing the ball to screen pygame.display.update() pygame.display.flip() time.sleep(0.002) # to slow down the speed of bouncing pygame.display.update() # if the left sIDe of ballRect is in a position less than 0,or the right sIDe of ballRect is greater than 500 if ballRect.left < 0 or ballRect.right > (wIDth):     x_speed = x_speed * -1 # if the top of ballRect is in a position less than 0,or the bottom of ballRect is greater than the length elif ballRect.top < 0 or ballRect.bottom > (length):     y_speed = y_speed * -1 pygame.display.update()

我没有画三角形,因为我不知道要去哪里,但是我希望球会像碰到窗户两侧时那样从三角形d起.任何帮助将是巨大的!最佳答案有趣的任务.可以通过一个简单的列表定义一个三角形:

triangle = [(250,220),(400,300),(100,300)]

三角形可以由pygame.draw.polygon()绘制

pygame.draw.polygon(WINDOW,RED,triangle,0)

使用pygame.math.Vector2定义球的位置和运动矢量:

ballvec = pygame.math.Vector2(1,1)ballpos = pygame.math.Vector2(150,250)balldiameter = 64

创建一个函数,执行碰撞检测.该功能必须检测球是否命中线.如果击中线,则球的运动矢量将反映在线上.
该线由2个点(lp0,lp1)表示,它们是pygame.math.Vector2对象.
球的位置(pt)和运动矢量(dir)也是pygame.math.Vector2个对象:

def isect(lp0,lp1,pt,dir,radius):    # direction vector of the line    l_dir = (lp1 - lp0).normalize()    # normal vector to the line    nv = pygame.math.Vector2(-l_dir[1],l_dir[0])    # distance to line    d = (lp0-pt).dot(nv)    # intersection point on endless line    ptX = pt + nv * d    # test if the ball hits the line    if abs(d) > radius or dir.dot(ptX-pt) <= 0:        return dir    if (ptX-lp0).dot(l_dir) < 0 or (ptX-lp1).dot(l_dir) > 0:        return dir     # reflect the direction vector on the line (like a billiard ball)    r_dir = dir.reflect(nv)    return r_dir

将窗口矩形和三角形追加到线列表中.蚀刻线由2 pygame.math.Vector2个对象的元组表示:

# add screen rectscreen_rect = [(0,0),(0,(500,0)]for i in range(len(screen_rect)):    p0,p1 = screen_rect[i],screen_rect[(i+1) % len(screen_rect)]    line_List.append((pygame.math.Vector2(p0[0],p0[1]),pygame.math.Vector2(p1[0],p1[1])))# add red trianlgetriangle = [(250,300)]for i in range(len(triangle)):    p0,p1 = triangle[i],triangle[(i+1) % len(triangle)]    line_List.append((pygame.math.Vector2(p0[0],p1[1])))

遍历直线的循环中进行碰撞检测.如果球撞到线,则运动矢量将替换为反射的运动矢量:

for line in line_List:    ballvec = isect(*line,ballpos,ballvec,balldiameter/2)

最后更新球的位置和球矩形:

ballpos = ballpos + ballvecballRect.x,ballRect.y = ballpos[0]-ballRect.wIDth/2,ballpos[1]-ballRect.height/2

请参阅示例代码,在该示例中,我将建议的更改应用于原始代码.我的球图像的尺寸为64×64.球直径必须设置为此尺寸(球直径= 64):

import pygame # importing the pygameimport sys # importing the system librarIEsimport time # importing timerimport randomfrom pygame.locals import * # importing the locals functions from the pygame library setpygame.init() # the function from pygame that initializes all relevant variable# setting length and wIDthwIDth = 500length = 300# colour variablesWHITE = (255,255)RED = (255,0)GRAY = (128,128,128)# importing ball imageball = pygame.image.load("ball.png")ballRect = ball.get_rect()# setting ball databallvec = pygame.math.Vector2(1.5,1.5)ballpos = pygame.math.Vector2(150,250)balldiameter = 64# setting window sizeWINDOW = pygame.display.set_mode((wIDth,length))# setting the size of the windowdef isect(lp0,l_dir[0])    # distance to line    d = (lp0-pt).dot(nv)    # intersection point on endless line    ptX = pt + nv * d    # test if the ball hits the line    if abs(d) > radius or dir.dot(ptX-pt) <= 0:        return dir    if (ptX-lp0).dot(l_dir) < 0 or (ptX-lp1).dot(l_dir) > 0:        return dir     # reflect the direction vector on the line (like a billiard ball)    r_dir = dir.reflect(nv)    return r_dirline_List = []# add screen rectscreen_rect = [(0,p1[1])))# add blue triangletriangle2 = [(250,80),0)]for i in range(len(triangle2)):    p0,p1 = triangle2[i],triangle2[(i+1) % len(triangle2)]    line_List.append((pygame.math.Vector2(p0[0],p1[1])))# loopwhile True:    for event in pygame.event.get():        if event.type == QUIT:            pygame.quit()            sys.exit()    WINDOW.fill(GRAY)    pygame.draw.polygon(WINDOW,0)    pygame.draw.polygon(WINDOW,BLUE,triangle2,0)    WINDOW.blit(ball,ballRect)    pygame.display.update()    pygame.display.flip()    time.sleep(0.002) # to slow down the speed of bouncing    pygame.display.update()    for line in line_List:        ballvec = isect(*line,balldiameter/2)    ballpos = ballpos + ballvec    ballRect.x,ballpos[1]-ballRect.height/2    pygame.display.update()
总结

以上是内存溢出为你收集整理的python-如何使球在pygame中从三角形d回? 全部内容,希望文章能够帮你解决python-如何使球在pygame中从三角形d回? 所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1199459.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存