亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何讓物體自行移動?

如何讓物體自行移動?

12345678_0001 2023-04-18 17:31:34
對于我的模擬,我試圖讓我需要的形狀具有重力效果。換句話說,當我點擊按鈕創建一個新形狀時,我如何讓它自己掉下來?任何幫助,將不勝感激。注意:我現在只是試圖讓圓圈工作,所以其他形狀的代碼落后了。我還需要完成碰撞檢測,這就是為什么那里有一些隨機的東西。main.py:import pygameimport timefrom 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 Variablesiwidth = 100iheight = 100# Inventory Classesicir = InvCir(10, 10, iwidth, iheight)irect = InvRect(15 + iwidth, 10, iwidth, iheight)itri = InvTri(20 + (iwidth * 2), 10, iwidth, iheight)# Object Classescir = Circle(40, (97, 160, 255), 500, 300)rect = Rect(300, 300, 80, 80)# Shape ListscirList = []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()
查看完整描述

1 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

彈跳球的技巧是增加重力。重力總是加速 Y(垂直)速度朝向地板。

  • 當球下落時,速度增加

  • 隨著球上升,速度下降

當球撞擊地面時,速度反轉并且球“彈跳”。當球速在頂部達到零時,重力會反轉速度并將球拉向地面。

這是一個彈跳球的簡單示例:

import pygame as pg

from time import sleep, time


pg.init()


Height = Width = 500  # window dimensions


pg.display.set_caption("Bounce") # window title

win = pg.display.set_mode((Height, Width)) # create window


ball = {'x':Width/2, 'y':100, 'xs':3, 'ys':3 } # ball start position and speed

radius = 20  # ball radius


while True:   # main loop

   for event in pg.event.get(): # required for OS events

      if event.type == pg.QUIT:

         pg.quit()

         

   pg.time.Clock().tick(30)  # 30 FPS

   win.fill((255, 255, 255))  # clear screen

   pg.draw.circle(win, (200, 0, 0), (int(ball['x']), int(ball['y'])), 20) # draw ball


   ball['x'] += ball['xs'] # move ball left \ right

   ball['y'] += ball['ys'] # move ball up \ down

   if ball['y'] >= Height - radius: ball['ys'] = -ball['ys']  # bounce on floor

   else: ball['ys'] += .2   # accelerate toward floor, increase speed down, decrease up

   if ball['x'] <= radius or ball['x'] > Width - radius: ball['xs'] = -ball['xs']  # bounce on wall


   pg.display.update()


查看完整回答
反對 回復 2023-04-18
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號