猿问

如何让物体自行移动?

对于我的模拟,我试图让我需要的形状具有重力效果。换句话说,当我点击按钮创建一个新形状时,我如何让它自己掉下来?任何帮助,将不胜感激。


注意:我现在只是试图让圆圈工作,所以其他形状的代码落后了。我还需要完成碰撞检测,这就是为什么那里有一些随机的东西。


main.py:


import pygame

import time

from shapes import *

from inv import *


pygame.init()


width, height = (1000, 800)

screen = pygame.display.set_mode((width, height))

pygame.display.set_caption("Physics Game")

bgc = (223, 255, 252)

screen.fill(bgc)


# Inv Button Variables

iwidth = 100

iheight = 100


# Inventory Classes

icir = InvCir(10, 10, iwidth, iheight)

irect = InvRect(15 + iwidth, 10, iwidth, iheight)

itri = InvTri(20 + (iwidth * 2), 10, iwidth, iheight)


# Object Classes

cir = Circle(40, (97, 160, 255), 500, 300)

rect = Rect(300, 300, 80, 80)


# Shape Lists

cirList = []

rectList = []


def main():

    run = True

    FPS = 60

    clock = pygame.time.Clock()


    def updateScreen():

        icir.draw(screen, (0, 0, 0))

        irect.draw(screen, (0, 0, 0))

        itri.draw(screen, (0, 0, 0))


        pygame.display.update()


    def addList(list, shape):

        list.append(shape)

        print(list)


    while run:

        clock.tick(FPS)


        mpos = pygame.mouse.get_pos()


        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                run = False


            if event.type == pygame.MOUSEBUTTONDOWN:

                if icir.checkClick(mpos):

                    cir.itemDraw(screen)

                    addList(cirList, cir)



            if event.type == pygame.MOUSEBUTTONDOWN:

                if irect.checkClick(mpos):

                    rect.itemDraw(screen)

                    addList(rectList, rect)


        if pygame.mouse.get_pressed()[0]:

                pass


        


        updateScreen()


main()


12345678_0001
浏览 113回答 1
1回答

达令说

弹跳球的技巧是增加重力。重力总是加速 Y(垂直)速度朝向地板。当球下落时,速度增加随着球上升,速度下降当球撞击地面时,速度反转并且球“弹跳”。当球速在顶部达到零时,重力会反转速度并将球拉向地面。这是一个弹跳球的简单示例:import pygame as pgfrom time import sleep, timepg.init()Height = Width = 500&nbsp; # window dimensionspg.display.set_caption("Bounce") # window titlewin = pg.display.set_mode((Height, Width)) # create windowball = {'x':Width/2, 'y':100, 'xs':3, 'ys':3 } # ball start position and speedradius = 20&nbsp; # ball radiuswhile True:&nbsp; &nbsp;# main loop&nbsp; &nbsp;for event in pg.event.get(): # required for OS events&nbsp; &nbsp; &nbsp; if event.type == pg.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pg.quit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;pg.time.Clock().tick(30)&nbsp; # 30 FPS&nbsp; &nbsp;win.fill((255, 255, 255))&nbsp; # clear screen&nbsp; &nbsp;pg.draw.circle(win, (200, 0, 0), (int(ball['x']), int(ball['y'])), 20) # draw ball&nbsp; &nbsp;ball['x'] += ball['xs'] # move ball left \ right&nbsp; &nbsp;ball['y'] += ball['ys'] # move ball up \ down&nbsp; &nbsp;if ball['y'] >= Height - radius: ball['ys'] = -ball['ys']&nbsp; # bounce on floor&nbsp; &nbsp;else: ball['ys'] += .2&nbsp; &nbsp;# accelerate toward floor, increase speed down, decrease up&nbsp; &nbsp;if ball['x'] <= radius or ball['x'] > Width - radius: ball['xs'] = -ball['xs']&nbsp; # bounce on wall&nbsp; &nbsp;pg.display.update()
随时随地看视频慕课网APP

相关分类

Python
我要回答