2016-11-16 1 views
0

파이썬으로 하나의 공을 창 4면에서 튀어 오르게하는 프로그램이 있습니다. 그러나 이제는 배열을 사용하여 10 개의 공을 만들어야합니다. 나는 여전히 이것에 익숙하지 않고 이것을 구현하는 방법에 대해 혼란스러워합니다. 코드와파이썬 Zelle Graphics를 사용하여 원 (공)을 바운스하게 만듭니다.

#create 10 balls bouncing off all 4 sides of the window (400 x 500) 
#The ball must start moving after a click, then stop after a given amount of time 
#After a second click, the program ends 

#----Algorithm----# 

#create screen 400 x 500 
#create array of 10 circles in different starting points 

#wait for click anywhere on screen 
#click anywhere on screen 
#all 10 balls move in different directions bouncing off all 4 walls 
    #if ball hits left or right wall 
      #dy=dy*-1 
    #if ball hits top or bottom 
      #dx=dx*-1 

#ball moves for no more than 30 seconds 
#ball stops 
#wait for next click 
#program ends 

#----ProgramStarts----# 

    from graphics import * 
    import time, random 

#create screen 
winWidth = 400; 
winHeight = 500; 
win = GraphWin('Ball Bounce', winWidth, winHeight); 
win.setBackground(color_rgb(255,255,255)); 
win.setCoords(0,0,winWidth, winHeight); 
numBalls= 10; 

#create 10 balls 
def makeBall(center, radius, win): 

    balls=[]; 
    radius=10; 

    for i in range (0,numBalls): 
     aBall=Circle(center, radius); 
     aBall.setFill("red"); 
     aBall.draw(win); 
     balls.append(aBall); 


#animate 10 balls bouncing off edges of window 
def bounceInWin(shape, dx, dy, xLow, xHigh, yLow, yHigh): 
    clickPoint=win.getMouse(); 
    delay = .005 
    for x in range(900): 
     shape.move(dx, dy) 
     center = shape.getCenter() 
     x = center.getX() 
     y = center.getY() 
     if x < xLow: 
      dx = -dx 
     if x > xHigh: 
      dx = -dx 
     if y < yLow: 
      dy = -dy 
     if y > yHigh: 
      dy = -dy 
     time.sleep(delay); 


#get a random Point 
def getRandomPoint(xLow, xHigh, yLow, yHigh): 
    x = random.randrange(xLow, xHigh+1) 
    y = random.randrange(yLow, yHigh+1) 
    return Point(x, y) 

#make ball bounce 
def bounceBall(dx, dy):  
    winWidth = 400 
    winHeight = 500 
    win = GraphWin('Ball Bounce', winWidth, winHeight); 
    win.setCoords(0,0,winWidth, winHeight); 

    radius = 10 
    xLow = radius 
    xHigh = winWidth - radius 
    yLow = radius 
    yHigh = winHeight - radius 

    center = getRandomPoint(xLow, xHigh, yLow, yHigh) 
    ball = makeBall(center, radius, win) 

    bounceInWin(ball, dx, dy, xLow, xHigh, yLow, yHigh) 

bounceBall(3, 5); 

#wait for another click to end program 
win.getMouse(); #close doesn't work, tried putting in loop 
win.close; 

답변

0

특정 문제 : 나는 지금까지 아래 무엇을 게시합니다 makeBall()balls을 반환하지; makeBall()은 모두 같은 위치에서 시작하는 것이 아니라 임의의 시작점을 생성해야합니다. bounceInWin()에는 별도의 업데이트가 필요하며 dx와 dy는 모든 공에 대해 별도로 업데이트됩니다. 이 코드는 기본 창을 두 번 만듭니다. 한 번만해도 좋습니다. 고정 된 반복 횟수 대신 모션을 종료하기 위해 시간 계산을 추가해야합니다. win.close()은 괄호가 필요한 메소드입니다. 세미콜론을 사용하지 마십시오 ";" 파이썬 코드.

# Create 10 balls bouncing off all 4 sides of a window (400 x 500) 
# The ball must start moving after a click, then stop after a given 
# amount of time. After a second click, the program ends. 

#----Algorithm----# 

# create screen 400 x 500 
# create array of 10 circles in different starting points 

# wait for click anywhere on screen 
# all 10 balls move in different directions bouncing off all 4 walls 
    # if ball hits left or right wall 
     # dx = -dx 
    # if ball hits top or bottom 
     # dy = -dy 

# balls move for no more than 30 seconds 
# balls stops 
# wait for next click 
# program ends 

#----ProgramStarts----# 

import time 
import random 
from graphics import * 

winWidth, winHeight = 400, 500 
ballRadius = 10 
ballColor = 'red' 
numBalls = 10 
delay = .005 
runFor = 30 # in seconds 

# create 10 balls, randomly located 
def makeBalls(xLow, xHigh, yLow, yHigh): 

    balls = [] 

    for _ in range(numBalls): 
     center = getRandomPoint(xLow, xHigh, yLow, yHigh) 

     aBall = Circle(center, ballRadius) 
     aBall.setFill(ballColor) 
     aBall.draw(win) 
     balls.append(aBall) 

    return balls 

# animate 10 balls bouncing off edges of window 
def bounceInWin(shapes, dx, dy, xLow, xHigh, yLow, yHigh): 
    movedShapes = [(getRandomDirection(dx, dy), shape) for shape in shapes] 

    start_time = time.time() 

    while time.time() < start_time + runFor: 
     shapes = movedShapes 
     movedShapes = [] 

     for (dx, dy), shape in shapes: 
      shape.move(dx, dy) 
      center = shape.getCenter() 

      x = center.getX() 
      if x < xLow or x > xHigh: 
       dx = -dx 

      y = center.getY() 
      if y < yLow or y > yHigh: 
       dy = -dy 

      # Could be so much simpler if Point had setX() and setY() methods 
      movedShapes.append(((dx, dy), shape)) 

     time.sleep(delay) 

# get a random direction 
def getRandomDirection(dx, dy): 
    x = random.randrange(-dx, dx) 
    y = random.randrange(-dy, dy) 

    return x, y 

# get a random Point 
def getRandomPoint(xLow, xHigh, yLow, yHigh): 
    x = random.randrange(xLow, xHigh + 1) 
    y = random.randrange(yLow, yHigh + 1) 

    return Point(x, y) 

# make balls bounce 
def bounceBalls(dx, dy): 

    xLow = ballRadius * 2 
    xHigh = winWidth - ballRadius * 2 
    yLow = ballRadius * 2 
    yHigh = winHeight - ballRadius * 2 

    balls = makeBalls(xLow, xHigh, yLow, yHigh) 

    win.getMouse() 

    bounceInWin(balls, dx, dy, xLow, xHigh, yLow, yHigh) 

# create screen 
win = GraphWin('Ball Bounce', winWidth, winHeight) 

bounceBalls(3, 5) 

# wait for another click to end program 
win.getMouse() 
win.close() 

enter image description here

: 아래

는 위의 변경 및 일부 스타일의 비틀기와 코드의 재 작업입니다
관련 문제