我正在关注来自youtube的这个用java编写的编码挑战。我想用python重新创建它。链接:https://www.youtube.com/watch?v=PoW8g67XNxA&t=586s 问题是我什么时候想把两个家伙都弹出来。由于它们在同一时间弹跳(它们在同一时刻改变速度),b1.s的变量(关于速度)改变了,当我试图用它来改变另一个速度(b2.speed)时,它已经被修改了。很难解释!请看一下我的代码
在Java中,它们使用常量变量。我不能在python中使用它们
import pygame, sys, math, time
import constant
pygame.init()
collitions = 0
pygame.mixer.music.load("C:\\Users\\Martín\\Desktop\\PYTHON\\abc.mp3")
clock = pygame.time.Clock()
screen = pygame.display.set_mode([800, 600])
class block:
def __init__(self, x, v, m, w):
self.x = x
self.y = 300 - w
self.w = w
self.v = v
self.m = m
def update(self):
self.x -= self.v
def col_1(self):
if self.x <= 50:
global collitions
collitions += 1
self.v = -(self.v)
return True
def col_2(self, other):
if self.x + self.w > other.x:
return True
def bounceb1(self, other):
sumM = self.m + other.m
newV = ((self.m - other.m) / sumM) * other.v
newV += ((2 * other.m) / sumM) * other.v
return newV
b1 = block(300, 0, 50, 50)
b2 = block(500, 3, 50, 100)
def draw():
b1.update()
b2.update()
b1.col_1()
if b1.col_2(b2):
b1.v = b1.bounceb1(b2)
b2.v =
text_1 = "Colisiones: " + str(collitions)
font = pygame.font.SysFont("ocraextended", 35)
label = font.render(text_1, 1, (0, 0, 0))
screen.blit(label, (400, 400))
pygame.draw.rect(screen, (255, 0, 0), (b1.x, b1.y, b1.w, b1.w))
pygame.draw.rect(screen, (255, 0, 0), (b2.x, b2.y, b2.w, b2.w))
pygame.draw.line(screen, (0, 0, 0), (0, 300), (800, 300))
pygame.draw.line(screen, (0, 0, 0), (50, 0), (50, 600))
def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill((255, 255, 255))
draw()
pygame.display.update()
clock.tick(60)
main()
目前没有回答
相关问题 更多 >
编程相关推荐