2017-12-25 17 views
0

나는 이상 기체 이론 시뮬레이션 프로젝트에있다. 나는 화살표를 누를 때 입자의 속도를 정확히 추가하는 방법을 묻고 싶다. 왜냐하면 내 코드에서 작동하지 않았기 때문입니다.내가 화살표를 눌러서 입자의 속도를 정확히 추가하는 방법

초기 프로세스 및 루프에서 속도를 추가했지만 아무런 효과가 없었습니다.

내가 지금하고있는 것을 바로 볼 수 있습니다. 난 당신이 목록에있는 모든 입자에 추가해야

import pygame, sys, random, math 
from pygame.locals import * 

pygame.init() 

#colour 
white = [255,255,255] 
black = [0,0,0] 
red = [255,0,0] 
green = [0,255,0] 
blue = [0,0,255] 

#setup window 
(width,height)=(800,600) 
window=pygame.display.set_mode((width,height)) 
pygame.display.set_caption('ideal gas') 


#setup fps window 
FPS=30 
clock=pygame.time.Clock() 

def bounceparticle(p1,p2): 
    dx=p1.x-p2.x 
    dy=p1.y-p2.y 

    distance=math.hypot(dx,dy) 
    if distance<p1.size+p2.size: 
     tangent=math.atan2(dy,dx) 
     angle=0.5*math.pi+tangent 

     angle1=2*tangent-p1.angle 
     angle2=2*tangent-p2.angle 
     speed1=p2.speed+p2.fire #so that every bounce increases speed 
     speed2=p1.speed+p1.fire #so that every bounce increases speed 

     (p1.angle, p1.speed) = (angle1, speed1) 
     (p2.angle, p2.speed) = (angle2, speed2) 

     p1.x += math.sin(angle) 
     p1.y -= math.cos(angle) 
     p2.x -= math.sin(angle) 
     p2.y += math.cos(angle) 

#partikel 
class Partikel: 
    def __init__(self,(x,y)): 
     self.x = x 
     self.y = y 
     self.size = 4 
     self.speed = 0 
     self.angle = 0 
     self.colour = blue #it need a recursion function to define changing particle's colour 
     self.fire = 0 

    def partikel_on_windows(self): 
     pygame.draw.circle(window,self.colour,[int(self.x),int(self.y)],self.size) 

    def move(self): 
     self.x += math.sin(self.angle) * self.speed 
     self.y -= math.cos(self.angle) * self.speed 
     #self.speed += self.fire #it can increase the speed but it takes 100 times pressed arrow up keyboard 

    def bounce(self): 
     if self.x>=width-self.size: 
     self.x=2*(width-self.size)-self.x 
     self.angle=-self.angle 
     self.speed += self.fire 

     elif self.x<=self.size: 
     self.x=2*self.size-self.x 
     self.angle=-self.angle 
     self.speed += self.fire 

     if self.y>=height-50-self.size: 
     self.y=2*(height-50-self.size)-self.y 
     self.angle=math.pi-self.angle 
     self.speed += self.fire 

     elif self.y<=self.size: 
     self.y=2*self.size-self.y 
     self.angle=math.pi-self.angle 
     self.speed += self.fire 

number_of_particles = 200 
myparticle = [] 

for n in range(number_of_particles): 
    centralpoint = random.randint(10,50) 
    x = random.randint(centralpoint,width-centralpoint) 
    y = random.randint(centralpoint,height-centralpoint) 

    partikel=Partikel((x,y)) 
    partikel.angle=random.uniform(0,math.pi*2) 
    partikel.speed = 2 
    #partikel.fire = 0 #it can increase the speed but it takes 100 times pressed arrow up keyboard 
    #partikel.colour= [0,0,[255-partikel.speed]] #can't change the colour 

    myparticle.append(partikel) 


# main game loop 
while True: 
    for event in pygame.event.get(): 
     pygame.key.set_repeat(1,50) 
     if event.type == QUIT: 
     pygame.quit() 
     sys.exit() 

     if event.type == pygame.KEYDOWN: 
     if event.key == K_UP: 
      partikel.fire += 5 
     if event.key == K_DOWN: 
      partikel.fire -= 5 

     """if event.type == pygame.KEYUP: 
     if event.key == K_UP: 
      partikel.fire = 0 
     if event.key == K_DOWN: 
      partikel.fire = 0""" 

    window.fill(black) 
    #partikel.fire = 1 #still can't 
    for i in range(len(myparticle)): 
     partikel = myparticle[i] 
     partikel.move() 
     partikel.bounce() 
     partikel.partikel_on_windows() 
     #partikel.fire = 0 #can't controling from keyboard 
     #partikel.colour= [0,0,[255-partikel.speed]] 
     for partikel2 in myparticle[i+1:]: 
     bounceparticle(partikel,partikel2) 



    pygame.display.update() 
    clock.tick(FPS) 
+0

읽어? 그리고 모든 입자 속도를 "정확하게"추가한다는 것은 무엇을 의미합니까? 또한 코드를 이미지에 넣지 마십시오. 문제를 설명하기 위해 [mcve]를 만듭니다. 그리고 대부분의 사용자가 다른 언어를 읽을 수 없으므로 코드를 영어로 작성하십시오. 도움이 더 어려워집니다. 내가 원하는 것은 입자 목록을 반복하고 모든 입자에 속도를 추가하는 것입니다. –

+0

질문에 코드를 넣으십시오 – furas

+0

죄송합니다. 지금 시험 중입니다. 분명히 영어 버전 – szczynk

답변

0

영어 두지 않은 경우

미안 더 이해 편집. I 키를 사용하는 달라

는 I 가변 speed 사용 후에 I는 루프에서 모든 입자에 추가

for i in range(len(partikel_ku)): 
    partikel.cepat += speed 

클릭 할 때마다 증가/속도를 감소시킨다.

전체 코드 :

import pygame 
import random 
import math 
import sys 

# --- constants ---- (UPPER_CASE_NAMES) 

PUTIH = (255, 255, 255) 
HITAM = (0, 0, 0) 
MERAH = (255, 0, 0) 
HIJAU = (0, 255, 0) 
BIRU = (0, 0, 255) 

LEBAR = 800 
TINGGI = 600 

FPS = 30 

# --- classes --- (CamelCaseNames) 

class Partikel: 

    def __init__(self, pos): 
     x, y = pos 
     self.x = x 
     self.y = y 
     self.size = 4 
     self.cepat = 0 
     self.sudut = 0 
     self.warna = BIRU 
     self.api = 0 

    def partikel_di_layar(self): 
     pygame.draw.circle(jendela, self.warna, [int(self.x), int(self.y)], self.size) 

    def gerak(self): 
     self.x += math.sin(self.sudut) * self.cepat 
     self.y -= math.cos(self.sudut) * self.cepat 

    def mantul(self): 
     if self.x >= LEBAR-self.size: 
      self.x = 2 * (LEBAR-self.size) - self.x 
      self.sudut = -self.sudut 

     elif self.x <= self.size: 
      self.x = 2 * self.size - self.x 
      self.sudut = -self.sudut 

     if self.y >= TINGGI - 50 - self.size: 
      self.y = 2 * (TINGGI - 50 - self.size) - self.y 
      self.sudut = math.pi - self.sudut 

     elif self.y <= self.size: 
      self.y = 2 * self.size - self.y 
      self.sudut = math.pi - self.sudut 

# --- functions --- (lower_case_names) 

def pantulan(p1, p2): 

    dx = p1.x - p2.x 
    dy = p1.y - p2.y 

    jarak = math.hypot(dx,dy) 

    if jarak < p1.size + p2.size: 
     tangen = math.atan2(dy, dx) 
     sudut = 0.5 * math.pi + tangen 

     sudut1 = 2 * tangen - p1.sudut 
     sudut2 = 2 * tangen - p2.sudut 
     cepat1 = p2.cepat + p2.api 
     cepat2 = p1.cepat + p1.api 

     p1.sudut = sudut1 
     p1.cepat = cepat1 

     p2.sudut = sudut2 
     p2.cepat = cepat2 

     p1.x += math.sin(sudut) 
     p1.y -= math.cos(sudut) 
     p2.x -= math.sin(sudut) 
     p2.y += math.cos(sudut) 

# --- main --- 

bnyk_partikel = 100 
partikel_ku = [] 

# - init - 

pygame.init() 

jendela = pygame.display.set_mode((LEBAR, TINGGI)) 
pygame.display.set_caption('Animasi Bola') 

pygame.key.set_repeat(1, 50) 

# - objects - 

for n in range(bnyk_partikel): 
    cp = random.randint(10, 50) 
    x = random.randint(cp, LEBAR - cp) 
    y = random.randint(cp, TINGGI - cp) 

    partikel = Partikel((x,y)) 
    partikel.sudut = random.uniform(0, math.pi*2) 
    partikel.cepat = 2 
    partikel_ku.append(partikel) 

speed = 0 

# - mainloop - 

clock=pygame.time.Clock() 

while True: 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 

     elif event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_UP: 
       speed += 1 
      elif event.key == pygame.K_DOWN: 
       speed -= 1 

     elif event.type == pygame.KEYUP: 
      if event.key == pygame.K_UP: 
       speed = 0 
      elif event.key == pygame.K_DOWN: 
       speed = 0 

    jendela.fill(HITAM) 

    #partikel.api = 1 #masih g bisa 
    for i in range(len(partikel_ku)): 
     partikel = partikel_ku[i] 
     partikel.cepat += speed 
     partikel.gerak() 
     partikel.mantul() 
     partikel.partikel_di_layar() 
     partikel.api = 1 #tak bisa dipanggil dari kontrol keyboard 
     #partikel.warna= [0,0,[255-partikel.cepat]] 
     for partikel2 in partikel_ku[i+1:]: 
      pantulan(partikel,partikel2) 

    pygame.display.update() 
    clock.tick(FPS) 

가 BTW : 왜 작동하지 않습니다 PEP 8 -- Style Guide for Python Code

+0

을 만들었습니다. 입자 클래스에서이 '속도'가 필요하지 않았습니까? – szczynk

+0

당신은 영어 이름을 사용하지 않으므로'cepat'가'speed'를 의미하는지 모르겠지만 모든 입자에서 따로 따로 변경해야합니다. 키를 클릭 할 때'api '를 변경하지만이'api '를 사용하여 모든 입자에서'cepat '를 따로 변경하지 마십시오. – furas