2016-07-07 56 views
0

因此,我是pygame的新手,我製作了一個程序,它可以根據重力使球彈跳,它完美地工作,但現在我希望能夠通過創建一個無限的球無論我點擊哪裏。我知道如何獲得點擊的座標,但我不知道如何創建多個圈子。我會以某種方式使用功能或類?這是我的代碼,對不起,如果有點混亂。創建彈跳球pygame的多個實例

import pygame,random,time 
pygame.init() 
infoObject = pygame.display.Info() 
side = pygame.display.Info().current_h 
side = side - ((1.0/12)*(pygame.display.Info().current_h)) 
side = int(side) 
screen = pygame.display.set_mode([side, side]) 
pygame.display.set_caption("") 
clock = pygame.time.Clock() 
done = False 
BLACK = (0,0,0) 
WHITE = (255,255,255) 
RED = (250,30,25) 
GREEN = (25, 240, 25) 
YELLOW = (250,240,25) 
FPS = 60 
x=100 
y=100 
t=0 
r=10 
direction=0 
count = 0 
def faller(x,y,color,size): 
    pygame.draw.circle(screen,color,(x,y),size,0) 
def reverse(): 
    global t,direction 
    if direction == 0: 
     direction = 1 
     t=t*4/5 
    elif direction == 1: 
     direction = 0 
     t=1 
while not done: 
     clock.tick(FPS) 
     events = pygame.event.get() 
     screen.fill(BLACK) 
     for event in events: 
      if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): 
       done = True 
     if direction==0: 
      t+=1 
      y+=int((9.8/FPS)*t) 
     elif direction==1: 
      t-=1 
      y-=int((9.8/FPS)*t) 
     if t==0: 
      reverse() 
     if y+r>side: 
      y=side-r 
      faller(x,y,RED,r) 
      reverse() 
     else: 
      faller(x,y,RED,r) 
     if y==side-r: 
      count+=1 
     else: 
      count=0 
     if count>=3: 
      done = True 
     pygame.display.flip() 
pygame.quit() 
+0

是的,準備學習很多! :P – rtmh

+0

[here](http://www.tutorialspoint.com/python/python_functions.htm)和[here](http://www.learnpython.org/en/Functions)和[here](https:/ /docs.python.org/3/tutorial/controlflow.html#defining-functions)是關於函數 – rtmh

+0

[這裏](https://docs.python.org/2/tutorial/classes.html)和[這裏] (http://www.tutorialspoint.com/python/python_classes_objects.htm)和[這裏](https://www.codecademy.com/courses/python-intermediate-en-WL8e4/0/1?curriculum_id=4f89dab3d788890003000096)是關於類的網站。我在幾分鐘內用Google搜索了這些內容。這是一項很好的技能,當你學習編程時,你應該首先利用它。大部分信息已經存在,技能在學習如何學習。祝你好運。 – rtmh

回答

1

我鼓勵你試驗類和方法。一旦你有了一個Ball類,你可以製作一個Ball對象列表並且一次處理它們。然後,您可以使用.append()列表方法在點擊鼠標時添加一個新球。

我會建議你使用變量如: POS_X, POS_Y, vel_x, vel_y,

這會使事情變得更容易理解,如果你想實現的碰撞。不要忘記評論你的代碼:)