【问题标题】:How Would I Make an Object Move by Itself?如何让物体自行移动?
【发布时间】:2020-08-02 01:49:54
【问题描述】:

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

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

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()

shapes.py:

import pygame
import os
from inv import *

pygame.init()

bgc = (223, 255, 252)
width, height = (1000, 800)
screen = pygame.display.set_mode((width, height))

# Images
CIRCLE = pygame.image.load(os.path.join("assets", "circle.png"))

def collide(obj1, obj2):
    offsetX = obj2.x - obj1.x
    offsetY = obj2.y - obj1.y
    return obj1.mask.overlap(obj2.mask, (offsetX, offsetY)) != None

class Circle:
    def __init__(self, radius, color, x, y):
        self.radius = radius
        self.color = color
        self.x = x
        self.y = y
        self.img = CIRCLE
        self.mask = pygame.mask.from_surface(self.img)

    def itemDraw(self, window):
        window.blit(self.img, (self.x, self.y))

    def move(self, vel):
        self.y += vel

    def wallCol(self, height, width):
        pass

    def objCol(self, obj):
        return collide(self, obj)

class Rect:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = (255, 163, 76)

    def itemDraw(self, window):
        pygame.draw.rect(window, self.color, (self.x, self.y, self.width, self.height))

inv.py:

import pygame
import os

pygame.init()

CIRCLE = pygame.image.load(os.path.join("assets", "circ_button.png"))
SQUARE = pygame.image.load(os.path.join("assets", "square_button.png"))
TRIANGLE = pygame.image.load(os.path.join("assets", "tri_button.png"))

class Inv:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

class InvCir(Inv):
    def __init__(self, x, y, width, height):
        super().__init__(x, y, width, height)

    def draw(self, window, outline = None):
        if outline:
            pygame.draw.rect(window, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)

        window.blit(CIRCLE, (self.x, self.y))

    def checkClick(self, pos):
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True

        return False

class InvRect(Inv):
    def __init__(self, x, y, width, height):
        super().__init__(x, y, width, height)

    def draw(self, window, outline = None):
        if outline:
            pygame.draw.rect(window, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)

        window.blit(SQUARE, (self.x, self.y))
    
    def checkClick(self, pos):
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True

        return False

class InvTri(Inv):
    def __init__(self, x, y, width, height):
        super().__init__(x, y, width, height)

    def draw(self, window, outline = None):
        if outline:
            pygame.draw.rect(window, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)

        window.blit(TRIANGLE, (self.x, self.y))

    def checkClick(self):
        pass

【问题讨论】:

  • 一般的概念是每次通过游戏循环,你需要有某种“更新”过程(最好把它放在一个单独的函数中)更新所有游戏对象(最好有每个对象都能够自我更新,并且主要更新只是迭代它们)。然后游戏对象需要考虑已经过去的时间,并确定结果会发生什么。对于大多数普通的“物理”模拟,记住每个对象的位置、速度和加速度就足够了,然后Euler integration
  • 以后,您可能还会发现在gamedev.stackexchange.com 上提出此类问题会更​​有用。
  • 您可能还想为您的运动添加时间因素,因为更新时间可能不一致。但关键是,当您创建对象时,它们应该放在对象列表中。然后每次屏幕更新,调用每个对象应该具有的移动方法,以及自上次移动更新以来的时间。每个对象都应该跟踪自己的位置和速度(可能是从 MovingObject 父级继承的?)我想你明白了。

标签: python pygame game-physics


【解决方案1】:

弹跳球的诀窍是增加重力。重力总是加速 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()

【讨论】:

  • 落地时减速(如ball['y'] = ball['y'] * 0.8
猜你喜欢
  • 2023-02-23
  • 1970-01-01
  • 2017-08-04
  • 1970-01-01
  • 2023-03-28
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多