精华内容
下载资源
问答
  • flash 弹力小球游戏

    2011-11-14 11:48:21
    一款弹力小球的游戏,里面很好的讲述了小球精确碰撞的代码!有兴趣的可以下载试试!!
  • Java弹弹球游戏

    千次阅读 2009-10-09 16:49:00
    这是我参加疯狂Java训练营时写的第一个Java游戏,用来纪念...按P可暂停,游戏过程中有声音播放,嘻嘻(*^__^*)弹球游戏图片 主界面: 撞击时界面: 游戏结束界面: 下载地址http://download.csdn.net/source/1723432

    这是我参加疯狂Java训练营时写的第一个Java游戏,用来纪念一下吧。按P可暂停,游戏过程中有声音播放,嘻嘻(*^__^*)

    弹球游戏图片

     主界面:

     主界面

     

    撞击时界面:

     

    撞击时界面

     

    游戏结束界面:

    游戏结束

     

     

    下载地址

    http://download.csdn.net/source/1723432

    展开全文
  • python开发_tkinter_小球完全弹性碰撞游戏_源码下载 今天利用业余时间,写了一个功能要强大一些的小球完全碰撞游戏游戏名称: 小球完全弹性碰撞 游戏规则: 1.游戏初始化的时候,有5个不同颜色的小球进行碰撞 2...

    之前做了一个基于python的tkinter的小球完全碰撞游戏:

    python开发_tkinter_小球完全弹性碰撞游戏_源码下载

    今天利用业余时间,写了一个功能要强大一些的小球完全碰撞游戏:

    游戏名称:
      小球完全弹性碰撞
    游戏规则:
      1.游戏初始化的时候,有5个不同颜色的小球进行碰撞
      2.玩家可以通过在窗口中单击鼠标左键进行增加小球个数
      3.玩家可以通过在窗口中单击鼠标右键进行删减小球个数
      4.玩家可以通过键盘的方向键:上,右键进行对小球加速
      5.玩家可以通过键盘的方向键:下,左键进行对小球减速
      6.玩家可以按键盘:f键实现全屏显示
      7.玩家可以按键盘:Esc键实现退出全屏操作
      8.窗口左下角显示小球个数,右下角显示作者邮箱

    先看看图:

    hongten_pygame

    hongten_pygame

    =======================================================

    源码部分:

    =======================================================

      1 #pygame draw
      2 
      3 import pygame
      4 from pygame.locals import *
      5 from sys import exit
      6 from random import *
      7 
      8 '''
      9     游戏名称:
     10         小球完全弹性碰撞
     11     游戏规则:
     12         1.游戏初始化的时候,有5个不同颜色的小球进行碰撞
     13         2.玩家可以通过在窗口中单击鼠标左键进行增加小球个数
     14         3.玩家可以通过在窗口中单击鼠标右键进行删减小球个数
     15         4.玩家可以通过键盘的方向键:上,右键进行对小球加速
     16         5.玩家可以通过键盘的方向键:下,左键进行对小球减速
     17         6.玩家可以按键盘:f键实现全屏显示
     18         7.玩家可以按键盘:Esc键实现退出全屏操作
     19         8.窗口左下角显示小球个数,右下角显示作者邮箱
     20     
     21 '''
     22 __author__ = {'name' : 'Hongten',
     23               'mail' : 'hongtenzone@foxmail.com',
     24               'blog' : 'http://www.cnblogs.com/hongten',
     25               'version' : '1.0'}
     26 
     27 pygame.init()
     28 pygame.display.set_caption('Ball Game')
     29 
     30 SCREEN_WIDTH = 600
     31 SCREEN_HEIGHT = 500
     32 SPEED = 1
     33 SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)
     34 SCREEN_DEFAULT_COLOR = (255, 255 ,255)
     35 READY = 0
     36 
     37 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
     38 screen.fill(SCREEN_DEFAULT_COLOR)
     39 bg = pygame.image.load('data\\image\\bg.jpg').convert()
     40 font = pygame.font.Font('data\\font\\TORK____.ttf', 14)
     41 
     42 balls = []
     43 BALL_R = 30
     44 BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]
     45 BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]
     46 BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]]
     47 
     48 for i in range(len(BALL_COLORS)):
     49     screen.fill(SCREEN_DEFAULT_COLOR)
     50     b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)
     51     balls.append(b)
     52 
     53 while 1:
     54     for event in pygame.event.get():
     55         if event.type ==  QUIT:
     56             exit()
     57         elif event.type == KEYDOWN:
     58             if event.key == K_UP:
     59                 SPEED += 0.1
     60             elif event.key == K_DOWN:
     61                 SPEED -= 0.1
     62             elif event.key == K_LEFT:
     63                 SPEED -= 0.1
     64             elif event.key == K_RIGHT:
     65                 SPEED += 0.1
     66             elif event.key == K_f:
     67                 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)
     68             elif event.key == 27:
     69                 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
     70         elif event.type == MOUSEBUTTONDOWN:
     71             pressed_array = pygame.mouse.get_pressed()
     72             for index in range(len(pressed_array)):
     73                 if pressed_array[index]:
     74                     if index == 0:
     75                         c_color = (randint(0, 255), randint(0, 255), randint(0, 255))
     76                         x, y = (BALL_R+1, BALL_R+1)
     77                         c_r = randint(10, 100)
     78                         c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]
     79                         c = pygame.draw.circle(screen, c_color, (x, y), BALL_R)
     80                         BALL_COLORS.append(c_color)
     81                         BALL_POINTS.append([x, y])
     82                         BALL_VELOCITY.append(c_v)
     83                         balls.append(c)
     84                     elif index == 2:
     85                         if len(balls) > 1:
     86                             balls.pop(0)
     87                             BALL_COLORS.pop(0)
     88                             BALL_POINTS.pop(0)
     89                             BALL_VELOCITY.pop(0)
     90                         
     91     #print(balls)
     92     for i in range(len(balls)):
     93         screen.blit(bg, (-300, -100))
     94         for n in range(len(balls)):
     95             '''
     96             if ((BALL_POINTS[i][0] - BALL_R) > 0 and (BALL_POINTS[i][0] - BALL_R) < BALL_R):
     97                 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] + BALL_R),int(BALL_POINTS[n][1])), BALL_R)
     98             elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_WIDTH and (BALL_POINTS[i][1] + BALL_R) > SCREEN_WIDTH - BALL_R):
     99                 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] - BALL_R),int(BALL_POINTS[n][1])), BALL_R)
    100             elif ((BALL_POINTS[i][1] - BALL_R) > 0 and (BALL_POINTS[i][1] - BALL_R) < BALL_R):
    101                 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] + BALL_R)), BALL_R)
    102             elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_HEIGHT and  (BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT - BALL_R):
    103                 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] - BALL_R)), BALL_R)
    104             '''
    105             pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)
    106         if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) > SCREEN_WIDTH))):
    107             BALL_VELOCITY[i][0] = -1 * BALL_VELOCITY[i][0]
    108         if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT))):
    109             BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1]
    110         
    111     for j in range(len(balls)):
    112         for k in range(len(balls)):
    113             b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2
    114             b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2
    115             b_r =(BALL_R*2)**2
    116             if (round((b_x + b_y), 2) <= round(b_r, 2)):
    117                 temp_x = BALL_VELOCITY[j][0]
    118                 temp_y = BALL_VELOCITY[j][1]
    119                 BALL_VELOCITY[j][0] = BALL_VELOCITY[k][0]
    120                 BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]
    121                 BALL_VELOCITY[k][0] = temp_x
    122                 BALL_VELOCITY[k][1] = temp_y
    123     
    124         BALL_POINTS[j][0] += SPEED * BALL_VELOCITY[j][0]
    125         BALL_POINTS[j][1] += SPEED * BALL_VELOCITY[j][1]
    126         
    127     pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))
    128     game_info = 'Balls: ' + str(len(balls))
    129     text = font.render(game_info, True, (255,255,255))
    130     author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))
    131     screen.blit(text, (0, SCREEN_HEIGHT+5))
    132     screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5))
    133     
    134     pygame.display.update()

    源码下载:http://files.cnblogs.com/hongten/pygame_pong_v1.0.zip

    ============================================

    v1.1 Edit by Hongten

     

    v1.1修改如下:
    1.增加了背景音乐
    2.增加小球的时候,会伴随音乐产生
    3.窗口左下角显示小球个数,速度,以及最后一个小球的位置

     

    ============================================

     

      1 #pygame draw
      2 
      3 import pygame
      4 from pygame.locals import *
      5 from sys import exit
      6 from random import *
      7 
      8 '''
      9     游戏名称:
     10         小球完全弹性碰撞
     11     游戏规则:
     12         1.游戏初始化的时候,有5个不同颜色的小球进行碰撞
     13         2.玩家可以通过在窗口中单击鼠标左键进行增加小球个数
     14         3.玩家可以通过在窗口中单击鼠标右键进行删减小球个数
     15         4.玩家可以通过键盘的方向键:上,右键进行对小球加速
     16         5.玩家可以通过键盘的方向键:下,左键进行对小球减速
     17         6.玩家可以按键盘:f键实现全屏显示
     18         7.玩家可以按键盘:Esc键实现退出全屏操作
     19         8.窗口左下角显示小球个数,右下角显示作者邮箱
     20 
     21         v1.1修改如下:
     22         1.增加了背景音乐
     23         2.增加小球的时候,会伴随音乐产生
     24         3.窗口左下角显示小球个数,速度,以及最后一个小球的位置
     25     
     26 '''
     27 __author__ = {'name' : 'Hongten',
     28               'mail' : 'hongtenzone@foxmail.com',
     29               'blog' : 'http://www.cnblogs.com/hongten',
     30               'version' : '1.1'}
     31 
     32 if not pygame.font: print('Warning, fonts disabled')
     33 if not pygame.mixer: print('Warning, sound disabled')
     34 
     35 pygame.init()
     36 pygame.display.set_caption('Ball Game')
     37 
     38 SCREEN_WIDTH = 600
     39 SCREEN_HEIGHT = 500
     40 SPEED = 1
     41 SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)
     42 SCREEN_DEFAULT_COLOR = (255, 255 ,255)
     43 READY = 0
     44 
     45 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
     46 screen.fill(SCREEN_DEFAULT_COLOR)
     47 bg = pygame.image.load('data\\image\\bg.jpg').convert()
     48 font = pygame.font.Font('data\\font\\TORK____.ttf', 14)
     49 new_sound = pygame.mixer.Sound('data\\sound\\new.wav')
     50 bg_sound = pygame.mixer.Sound('data\\sound\\bg.ogg')
     51 bg_sound.set_volume(0.5)
     52 bg_sound.play(-1)
     53 new_sound.set_volume(1.0)
     54 
     55 
     56 balls = []
     57 BALL_R = 30
     58 BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]
     59 BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]
     60 BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]]
     61 
     62 for i in range(len(BALL_COLORS)):
     63     screen.fill(SCREEN_DEFAULT_COLOR)
     64     b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)
     65     balls.append(b)
     66 
     67 while 1:
     68     for event in pygame.event.get():
     69         if event.type ==  QUIT:
     70             bg_sound.stop()
     71             exit()
     72         elif event.type == KEYDOWN:
     73             if event.key == K_UP:
     74                 SPEED += 0.1
     75             elif event.key == K_DOWN:
     76                 SPEED -= 0.1
     77             elif event.key == K_LEFT:
     78                 SPEED -= 0.1
     79             elif event.key == K_RIGHT:
     80                 SPEED += 0.1
     81             elif event.key == K_f:
     82                 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)
     83             elif event.key == 27:
     84                 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
     85         elif event.type == MOUSEBUTTONDOWN:
     86             pressed_array = pygame.mouse.get_pressed()
     87             for index in range(len(pressed_array)):
     88                 if pressed_array[index]:
     89                     if index == 0:
     90                         new_sound.play(-1)
     91                         c_color = (randint(0, 255), randint(0, 255), randint(0, 255))
     92                         x, y = (BALL_R+1, BALL_R+1)
     93                         c_r = randint(10, 100)
     94                         c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]
     95                         c = pygame.draw.circle(screen, c_color, (x, y), BALL_R)
     96                         BALL_COLORS.append(c_color)
     97                         BALL_POINTS.append([x, y])
     98                         BALL_VELOCITY.append(c_v)
     99                         balls.append(c)
    100                     elif index == 2:
    101                         if len(balls) > 1:
    102                             balls.pop(0)
    103                             BALL_COLORS.pop(0)
    104                             BALL_POINTS.pop(0)
    105                             BALL_VELOCITY.pop(0)
    106         elif event.type == MOUSEBUTTONUP:
    107             new_sound.stop()
    108                         
    109     #print(balls)
    110     for i in range(len(balls)):
    111         screen.blit(bg, (-300, -100))
    112         for n in range(len(balls)):
    113             '''
    114             if ((BALL_POINTS[i][0] - BALL_R) > 0 and (BALL_POINTS[i][0] - BALL_R) < BALL_R):
    115                 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] + BALL_R),int(BALL_POINTS[n][1])), BALL_R)
    116             elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_WIDTH and (BALL_POINTS[i][1] + BALL_R) > SCREEN_WIDTH - BALL_R):
    117                 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] - BALL_R),int(BALL_POINTS[n][1])), BALL_R)
    118             elif ((BALL_POINTS[i][1] - BALL_R) > 0 and (BALL_POINTS[i][1] - BALL_R) < BALL_R):
    119                 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] + BALL_R)), BALL_R)
    120             elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_HEIGHT and  (BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT - BALL_R):
    121                 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] - BALL_R)), BALL_R)
    122             '''
    123             pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)
    124         if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) > SCREEN_WIDTH))):
    125             BALL_VELOCITY[i][0] = -1 * BALL_VELOCITY[i][0]
    126         if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT))):
    127             BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1]
    128         
    129     for j in range(len(balls)):
    130         for k in range(len(balls)):
    131             b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2
    132             b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2
    133             b_r =(BALL_R*2)**2
    134             if (round((b_x + b_y), 2) <= round(b_r, 2)):
    135                 temp_x = BALL_VELOCITY[j][0]
    136                 temp_y = BALL_VELOCITY[j][1]
    137                 BALL_VELOCITY[j][0] = BALL_VELOCITY[k][0]
    138                 BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]
    139                 BALL_VELOCITY[k][0] = temp_x
    140                 BALL_VELOCITY[k][1] = temp_y
    141     
    142         BALL_POINTS[j][0] += round(SPEED, 1) * BALL_VELOCITY[j][0]
    143         BALL_POINTS[j][1] += round(SPEED, 1) * BALL_VELOCITY[j][1]
    144         
    145     pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))
    146     game_info = 'Balls: ' + str(len(balls)) + '     Speed: ' + str(round(SPEED, 2)) + '   LastBall: ' + str(round(BALL_POINTS[-1][0])) + ',' + str(round(BALL_POINTS[-1][1]))
    147     text = font.render(game_info, True, (255,255,255))
    148     author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))
    149     screen.blit(text, (0, SCREEN_HEIGHT+5))
    150     screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5))
    151     
    152     pygame.display.update()

     

    源码下载:http://files.cnblogs.com/hongten/pygame_pong_v1.1.rar

    ==================================================

     

    Edit By Hongten

    v1.2修改如下:
    1.修改键盘方向键:左,右键为调节音量(0, 10)
    2.在状态栏添加音量状态信息:数字和图形显示

    ==================================================

      1 #pygame draw
      2 
      3 import pygame
      4 from pygame.locals import *
      5 from sys import exit
      6 from random import *
      7 
      8 '''
      9     游戏名称:
     10         小球完全弹性碰撞
     11     游戏规则:
     12         1.游戏初始化的时候,有5个不同颜色的小球进行碰撞
     13         2.玩家可以通过在窗口中单击鼠标左键进行增加小球个数
     14         3.玩家可以通过在窗口中单击鼠标右键进行删减小球个数
     15         4.玩家可以通过键盘的方向键:上,右键进行对小球加速
     16         5.玩家可以通过键盘的方向键:下,左键进行对小球减速
     17         6.玩家可以按键盘:f键实现全屏显示
     18         7.玩家可以按键盘:Esc键实现退出全屏操作
     19         8.窗口左下角显示小球个数,右下角显示作者邮箱
     20 
     21         v1.1修改如下:
     22         1.增加了背景音乐
     23         2.增加小球的时候,会伴随音乐产生
     24         3.窗口左下角显示小球个数,速度,以及最后一个小球的位置
     25 
     26         v1.2修改如下:
     27         1.修改键盘方向键:左,右键为调节音量(0, 10)
     28         2.在状态栏添加音量状态信息:数字和图形显示
     29     
     30 '''
     31 __author__ = {'name' : 'Hongten',
     32               'mail' : 'hongtenzone@foxmail.com',
     33               'blog' : 'http://www.cnblogs.com/hongten',
     34               'version' : '1.2'}
     35 
     36 if not pygame.font: print('Warning, fonts disabled')
     37 if not pygame.mixer: print('Warning, sound disabled')
     38 
     39 pygame.init()
     40 pygame.display.set_caption('Ball Game')
     41 
     42 SCREEN_WIDTH = 600
     43 SCREEN_HEIGHT = 500
     44 SPEED = 1
     45 VOLUME = 5
     46 SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)
     47 SCREEN_DEFAULT_COLOR = (255, 255 ,255)
     48 READY = 0
     49 
     50 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
     51 screen.fill(SCREEN_DEFAULT_COLOR)
     52 bg = pygame.image.load('data\\image\\bg.jpg').convert()
     53 font = pygame.font.Font('data\\font\\TORK____.ttf', 14)
     54 new_sound = pygame.mixer.Sound('data\\sound\\new.wav')
     55 bg_sound = pygame.mixer.Sound('data\\sound\\bg.ogg')
     56 bg_sound.set_volume(0.1 * VOLUME)
     57 bg_sound.play(-1)
     58 new_sound.set_volume(0.1 * VOLUME)
     59 
     60 
     61 balls = []
     62 BALL_R = 30
     63 BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]
     64 BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]
     65 BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]]
     66 
     67 VOLUME_POINTS = []
     68 VOLUME_POINTS_START = []
     69 VOLUME_RECT_COLORS = []
     70 for p in range(170, 250, 7):
     71     VOLUME_POINTS.append([SCREEN_WIDTH - p,SCREEN_HEIGHT + 20])
     72 for ps in range(175, 250, 7):
     73     VOLUME_POINTS_START.append([SCREEN_WIDTH - ps, SCREEN_HEIGHT])
     74     VOLUME_RECT_COLORS.append((randint(0, 255), randint(0, 255), randint(0, 255)))
     75 
     76 
     77 print(VOLUME_POINTS[-10])
     78 print(VOLUME_POINTS_START[-10])
     79 
     80 for i in range(len(BALL_COLORS)):
     81     screen.fill(SCREEN_DEFAULT_COLOR)
     82     b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)
     83     balls.append(b)
     84 
     85 while 1:
     86     for event in pygame.event.get():
     87         if event.type ==  QUIT:
     88             bg_sound.stop()
     89             exit()
     90         elif event.type == KEYDOWN:
     91             if event.key == K_UP:
     92                 SPEED += 0.1
     93             elif event.key == K_DOWN:
     94                 SPEED -= 0.1
     95             elif event.key == K_LEFT:
     96                 if VOLUME > 0:
     97                     VOLUME -= 1
     98             elif event.key == K_RIGHT:
     99                 if VOLUME <= 9:
    100                     VOLUME += 1
    101             elif event.key == K_f:
    102                 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)
    103             elif event.key == 27:
    104                 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
    105         elif event.type == MOUSEBUTTONDOWN:
    106             pressed_array = pygame.mouse.get_pressed()
    107             for index in range(len(pressed_array)):
    108                 if pressed_array[index]:
    109                     if index == 0:
    110                         new_sound.play(-1)
    111                         c_color = (randint(0, 255), randint(0, 255), randint(0, 255))
    112                         x, y = (BALL_R+1, BALL_R+1)
    113                         c_r = randint(10, 100)
    114                         c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]
    115                         c = pygame.draw.circle(screen, c_color, (x, y), BALL_R)
    116                         BALL_COLORS.append(c_color)
    117                         BALL_POINTS.append([x, y])
    118                         BALL_VELOCITY.append(c_v)
    119                         balls.append(c)
    120                     elif index == 2:
    121                         if len(balls) > 1:
    122                             balls.pop(0)
    123                             BALL_COLORS.pop(0)
    124                             BALL_POINTS.pop(0)
    125                             BALL_VELOCITY.pop(0)
    126         elif event.type == MOUSEBUTTONUP:
    127             new_sound.stop()
    128                         
    129     #print(balls)
    130     for i in range(len(balls)):
    131         screen.blit(bg, (-300, -100))
    132         #screen.fill(SCREEN_DEFAULT_COLOR)
    133         for n in range(len(balls)):
    134             '''
    135             if ((BALL_POINTS[i][0] - BALL_R) > 0 and (BALL_POINTS[i][0] - BALL_R) < BALL_R):
    136                 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] + BALL_R),int(BALL_POINTS[n][1])), BALL_R)
    137             elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_WIDTH and (BALL_POINTS[i][1] + BALL_R) > SCREEN_WIDTH - BALL_R):
    138                 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] - BALL_R),int(BALL_POINTS[n][1])), BALL_R)
    139             elif ((BALL_POINTS[i][1] - BALL_R) > 0 and (BALL_POINTS[i][1] - BALL_R) < BALL_R):
    140                 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] + BALL_R)), BALL_R)
    141             elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_HEIGHT and  (BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT - BALL_R):
    142                 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] - BALL_R)), BALL_R)
    143             '''
    144             pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)
    145         if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) > SCREEN_WIDTH))):
    146             BALL_VELOCITY[i][0] = -1 * BALL_VELOCITY[i][0]
    147         if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT))):
    148             BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1]
    149         
    150     for j in range(len(balls)):
    151         for k in range(len(balls)):
    152             b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2
    153             b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2
    154             b_r =(BALL_R*2)**2
    155             if (round((b_x + b_y), 2) <= round(b_r, 2)):
    156                 temp_x = BALL_VELOCITY[j][0]
    157                 temp_y = BALL_VELOCITY[j][1]
    158                 BALL_VELOCITY[j][0] = BALL_VELOCITY[k][0]
    159                 BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]
    160                 BALL_VELOCITY[k][0] = temp_x
    161                 BALL_VELOCITY[k][1] = temp_y
    162     
    163         BALL_POINTS[j][0] += round(SPEED, 1) * BALL_VELOCITY[j][0]
    164         BALL_POINTS[j][1] += round(SPEED, 1) * BALL_VELOCITY[j][1]
    165         
    166     pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))
    167     bg_sound.set_volume(0.1 * VOLUME)
    168     new_sound.set_volume(0.1 * VOLUME)
    169     pygame.draw.rect(screen,
    170                      (255, 255, 255),
    171                      Rect((VOLUME_POINTS_START[-1][0],
    172                            VOLUME_POINTS_START[-1][1]),
    173                           (VOLUME_POINTS[-10][0] - VOLUME_POINTS_START[-1][0],
    174                            20)))
    175     for v in range(VOLUME+1):
    176         if v > 0:
    177             pygame.draw.rect(screen,
    178                              VOLUME_RECT_COLORS[v],
    179                              Rect((VOLUME_POINTS_START[-v][0],
    180                                    VOLUME_POINTS_START[-v][1]),
    181                                   (VOLUME_POINTS[-v][0] - VOLUME_POINTS_START[-v][0],
    182                                    20)))
    183     
    184     game_info = 'Balls: ' + str(len(balls)) + '     Speed: ' + str(round(SPEED, 2)) + '   LastBall: ' + str(round(BALL_POINTS[-1][0])) + ',' + str(round(BALL_POINTS[-1][1]))
    185     text = font.render(game_info, True, (255,255,255))
    186     author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))
    187     volume_text = font.render('Volume: ' + str(VOLUME), True, (255, 255, 255))
    188     screen.blit(text, (0, SCREEN_HEIGHT+5))
    189     screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5))
    190     screen.blit(volume_text, (SCREEN_WIDTH - 310, SCREEN_HEIGHT+5))
    191     pygame.display.update()

    源码下载:http://files.cnblogs.com/hongten/pygame_pong_v1.2.zip

     ================================================

    v1.3 Edity by Hongten

    Change Chinese to English

     ================================================

    The View of the Game:

    hongten_pygame

      1 #pygame draw
      2 
      3 import pygame
      4 from pygame.locals import *
      5 from sys import exit
      6 from random import *
      7 
      8 '''
      9     Game Name:
     10         Ball Perfectly Elastic Collision(BPEC)
     11     Rules or Description:
     12         1.There are five balls whth different color have elastic collision
     13           after the game loaded.
     14         2.The player can click the window with the LEFT mouse button
     15           and create the new ball,which with the different color,but
     16           sometimes maybe like other balls.
     17         3.The player can click teh window with RIGHT mouse button
     18           and minus a few balls.
     19         4.You can change all ball speed by pressing the UP and DOWN
     20           the keyboard direction key.
     21         5.Also you can change the background music by pressing the LEFT
     22           and RIGHT the keyboard direction key(volume:0-10).
     23         6.Maybe you want to full screen view,By pressing the F key
     24           and ESC key switch.
     25         7.Ball number,the speed,the volume,author E-mail is written
     26           in the status bar at the bottom.    
     27 '''
     28 __version__ = '1.3'
     29 __author__ = {'name' : 'Hongten',
     30               'mail' : 'hongtenzone@foxmail.com',
     31               'blog' : 'http://www.cnblogs.com/hongten',
     32               'version' : __version__}
     33 
     34 if not pygame.font: print('Warning, fonts disabled')
     35 if not pygame.mixer: print('Warning, sound disabled')
     36 
     37 pygame.init()
     38 pygame.display.set_caption('Ball Game')
     39 
     40 SCREEN_WIDTH = 600
     41 SCREEN_HEIGHT = 500
     42 SPEED = 1
     43 VOLUME = 5
     44 SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)
     45 SCREEN_DEFAULT_COLOR = (255, 255 ,255)
     46 READY = 0
     47 
     48 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
     49 screen.fill(SCREEN_DEFAULT_COLOR)
     50 bg = pygame.image.load('data\\image\\bg.jpg').convert()
     51 font = pygame.font.Font('data\\font\\TORK____.ttf', 14)
     52 new_sound = pygame.mixer.Sound('data\\sound\\new.wav')
     53 bg_sound = pygame.mixer.Sound('data\\sound\\bg.ogg')
     54 bg_sound.set_volume(0.1 * VOLUME)
     55 bg_sound.play(-1)
     56 new_sound.set_volume(0.1 * VOLUME)
     57 
     58 
     59 balls = []
     60 BALL_R = 30
     61 BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]
     62 BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]
     63 BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]]
     64 
     65 VOLUME_POINTS = []
     66 VOLUME_POINTS_START = []
     67 VOLUME_RECT_COLORS = []
     68 for p in range(170, 250, 7):
     69     VOLUME_POINTS.append([SCREEN_WIDTH - p,SCREEN_HEIGHT + 20])
     70 for ps in range(175, 250, 7):
     71     VOLUME_POINTS_START.append([SCREEN_WIDTH - ps, SCREEN_HEIGHT])
     72     VOLUME_RECT_COLORS.append((randint(0, 255), randint(0, 255), randint(0, 255)))
     73 
     74 for i in range(len(BALL_COLORS)):
     75     screen.fill(SCREEN_DEFAULT_COLOR)
     76     b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)
     77     balls.append(b)
     78 
     79 while 1:
     80     for event in pygame.event.get():
     81         if event.type ==  QUIT:
     82             bg_sound.stop()
     83             exit()
     84         elif event.type == KEYDOWN:
     85             if event.key == K_UP:
     86                 SPEED += 0.1
     87             elif event.key == K_DOWN:
     88                 SPEED -= 0.1
     89             elif event.key == K_LEFT:
     90                 if VOLUME > 0:
     91                     VOLUME -= 1
     92             elif event.key == K_RIGHT:
     93                 if VOLUME <= 9:
     94                     VOLUME += 1
     95             elif event.key == K_f:
     96                 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)
     97             elif event.key == 27:
     98                 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
     99         elif event.type == MOUSEBUTTONDOWN:
    100             pressed_array = pygame.mouse.get_pressed()
    101             for index in range(len(pressed_array)):
    102                 if pressed_array[index]:
    103                     if index == 0:
    104                         new_sound.play(-1)
    105                         c_color = (randint(0, 255), randint(0, 255), randint(0, 255))
    106                         x, y = (BALL_R+1, BALL_R+1)
    107                         c_r = randint(10, 100)
    108                         c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]
    109                         c = pygame.draw.circle(screen, c_color, (x, y), BALL_R)
    110                         BALL_COLORS.append(c_color)
    111                         BALL_POINTS.append([x, y])
    112                         BALL_VELOCITY.append(c_v)
    113                         balls.append(c)
    114                     elif index == 2:
    115                         if len(balls) > 1:
    116                             balls.pop(0)
    117                             BALL_COLORS.pop(0)
    118                             BALL_POINTS.pop(0)
    119                             BALL_VELOCITY.pop(0)
    120         elif event.type == MOUSEBUTTONUP:
    121             new_sound.stop()
    122                         
    123     #print(balls)
    124     for i in range(len(balls)):
    125         screen.blit(bg, (-300, -100))
    126         #screen.fill(SCREEN_DEFAULT_COLOR)
    127         for n in range(len(balls)):
    128             pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)
    129         if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) > SCREEN_WIDTH))):
    130             BALL_VELOCITY[i][0] = -1 * BALL_VELOCITY[i][0]
    131         if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT))):
    132             BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1]
    133         
    134     for j in range(len(balls)):
    135         for k in range(len(balls)):
    136             b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2
    137             b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2
    138             b_r =(BALL_R*2)**2
    139             if (round((b_x + b_y), 2) <= round(b_r, 2)):
    140                 temp_x = BALL_VELOCITY[j][0]
    141                 temp_y = BALL_VELOCITY[j][1]
    142                 BALL_VELOCITY[j][0] = BALL_VELOCITY[k][0]
    143                 BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]
    144                 BALL_VELOCITY[k][0] = temp_x
    145                 BALL_VELOCITY[k][1] = temp_y
    146     
    147         BALL_POINTS[j][0] += round(SPEED, 1) * BALL_VELOCITY[j][0]
    148         BALL_POINTS[j][1] += round(SPEED, 1) * BALL_VELOCITY[j][1]
    149         
    150     pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))
    151     bg_sound.set_volume(0.1 * VOLUME)
    152     new_sound.set_volume(0.1 * VOLUME)
    153     pygame.draw.rect(screen,
    154                      (255, 255, 255),
    155                      Rect((VOLUME_POINTS_START[-1][0],
    156                            VOLUME_POINTS_START[-1][1]),
    157                           (VOLUME_POINTS[-10][0] - VOLUME_POINTS_START[-1][0],
    158                            20)))
    159     for v in range(VOLUME+1):
    160         if v > 0:
    161             pygame.draw.rect(screen,
    162                              VOLUME_RECT_COLORS[v],
    163                              Rect((VOLUME_POINTS_START[-v][0],
    164                                    VOLUME_POINTS_START[-v][1]),
    165                                   (VOLUME_POINTS[-v][0] - VOLUME_POINTS_START[-v][0],
    166                                    20)))
    167     
    168     game_info = 'Balls: ' + str(len(balls)) + '     Speed: ' + str(round(SPEED, 2)) + '   LastBall: ' + str(round(BALL_POINTS[-1][0])) + ',' + str(round(BALL_POINTS[-1][1]))
    169     text = font.render(game_info, True, (255,255,255))
    170     author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))
    171     volume_text = font.render('Volume: ' + str(VOLUME), True, (255, 255, 255))
    172     screen.blit(text, (0, SCREEN_HEIGHT+5))
    173     screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5))
    174     screen.blit(volume_text, (SCREEN_WIDTH - 310, SCREEN_HEIGHT+5))
    175     pygame.display.update()

     源码下载:http://files.cnblogs.com/hongten/pygame_pong_v1.3.zip

     

     

    ========================================================

     

    More reading,and english is important.

     

    I'm Hongten

     

     

     

    大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
    Hongten博客排名在100名以内。粉丝过千。
    Hongten出品,必是精品。

     

    E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

     

    ========================================================

     

    转载于:https://www.cnblogs.com/hongten/p/hongten_pygame_pong.html

    展开全文
  • 之前做了一个基于python的tkinter的小球完全碰撞游戏:今天利用业余时间,写了一个功能要强大一些的小球完全碰撞游戏游戏名称:小球完全弹性碰撞游戏规则:1.游戏初始化的时候,有5个不同颜色的小球进行碰撞2.玩家...

    之前做了一个基于python的tkinter的小球完全碰撞游戏:

    今天利用业余时间,写了一个功能要强大一些的小球完全碰撞游戏:

    游戏名称:

    小球完全弹性碰撞

    游戏规则:

    1.游戏初始化的时候,有5个不同颜色的小球进行碰撞

    2.玩家可以通过在窗口中单击鼠标左键进行增加小球个数

    3.玩家可以通过在窗口中单击鼠标右键进行删减小球个数

    4.玩家可以通过键盘的方向键:上,右键进行对小球加速

    5.玩家可以通过键盘的方向键:下,左键进行对小球减速

    6.玩家可以按键盘:f键实现全屏显示

    7.玩家可以按键盘:Esc键实现退出全屏操作

    8.窗口左下角显示小球个数,右下角显示作者邮箱

    先看看图:

    =======================================================

    源码部分:

    =======================================================

    1 #pygame draw

    2

    3 importpygame4 from pygame.locals import *

    5 from sys importexit6 from random import *

    7

    8 '''

    9 游戏名称:10 小球完全弹性碰撞11 游戏规则:12 1.游戏初始化的时候,有5个不同颜色的小球进行碰撞13 2.玩家可以通过在窗口中单击鼠标左键进行增加小球个数14 3.玩家可以通过在窗口中单击鼠标右键进行删减小球个数15 4.玩家可以通过键盘的方向键:上,右键进行对小球加速16 5.玩家可以通过键盘的方向键:下,左键进行对小球减速17 6.玩家可以按键盘:f键实现全屏显示18 7.玩家可以按键盘:Esc键实现退出全屏操作19 8.窗口左下角显示小球个数,右下角显示作者邮箱20

    21 '''

    22 __author__ = {'name' : 'Hongten',23 'mail' : 'hongtenzone@foxmail.com',24 'blog' : 'http://www.cnblogs.com/hongten',25 'version' : '1.0'}26

    27 pygame.init()28 pygame.display.set_caption('Ball Game')29

    30 SCREEN_WIDTH = 600

    31 SCREEN_HEIGHT = 500

    32 SPEED = 1

    33 SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)34 SCREEN_DEFAULT_COLOR = (255, 255 ,255)35 READY =036

    37 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)38 screen.fill(SCREEN_DEFAULT_COLOR)39 bg = pygame.image.load('data\\image\\bg.jpg').convert()40 font = pygame.font.Font('data\\font\\TORK____.ttf', 14)41

    42 balls =[]43 BALL_R = 30

    44 BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]45 BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]46 BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]]47

    48 for i inrange(len(BALL_COLORS)):49 screen.fill(SCREEN_DEFAULT_COLOR)50 b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)51 balls.append(b)52

    53 while 1:54 for event inpygame.event.get():55 if event.type ==QUIT:56 exit()57 elif event.type ==KEYDOWN:58 if event.key ==K_UP:59 SPEED += 0.1

    60 elif event.key ==K_DOWN:61 SPEED -= 0.1

    62 elif event.key ==K_LEFT:63 SPEED -= 0.1

    64 elif event.key ==K_RIGHT:65 SPEED += 0.1

    66 elif event.key ==K_f:67 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)68 elif event.key == 27:69 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)70 elif event.type ==MOUSEBUTTONDOWN:71 pressed_array =pygame.mouse.get_pressed()72 for index inrange(len(pressed_array)):73 ifpressed_array[index]:74 if index ==0:75 c_color = (randint(0, 255), randint(0, 255), randint(0, 255))76 x, y = (BALL_R+1, BALL_R+1)77 c_r = randint(10, 100)78 c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]79 c =pygame.draw.circle(screen, c_color, (x, y), BALL_R)80 BALL_COLORS.append(c_color)81 BALL_POINTS.append([x, y])82 BALL_VELOCITY.append(c_v)83 balls.append(c)84 elif index == 2:85 if len(balls) > 1:86 balls.pop(0)87 BALL_COLORS.pop(0)88 BALL_POINTS.pop(0)89 BALL_VELOCITY.pop(0)90

    91 #print(balls)

    92 for i inrange(len(balls)):93 screen.blit(bg, (-300, -100))94 for n inrange(len(balls)):95 '''

    96 if ((BALL_POINTS[i][0] - BALL_R) > 0 and (BALL_POINTS[i][0] - BALL_R) < BALL_R):97 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] + BALL_R),int(BALL_POINTS[n][1])), BALL_R)98 elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_WIDTH and (BALL_POINTS[i][1] + BALL_R) > SCREEN_WIDTH - BALL_R):99 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] - BALL_R),int(BALL_POINTS[n][1])), BALL_R)100 elif ((BALL_POINTS[i][1] - BALL_R) > 0 and (BALL_POINTS[i][1] - BALL_R) < BALL_R):101 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] + BALL_R)), BALL_R)102 elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_HEIGHT and (BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT - BALL_R):103 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] - BALL_R)), BALL_R)104 '''

    105 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)106 if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) >SCREEN_WIDTH))):107 BALL_VELOCITY[i][0] = -1 *BALL_VELOCITY[i][0]108 if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) >SCREEN_HEIGHT))):109 BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1]110

    111 for j inrange(len(balls)):112 for k inrange(len(balls)):113 b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2

    114 b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2

    115 b_r =(BALL_R*2)**2

    116 if (round((b_x + b_y), 2) <= round(b_r, 2)):117 temp_x =BALL_VELOCITY[j][0]118 temp_y = BALL_VELOCITY[j][1]119 BALL_VELOCITY[j][0] =BALL_VELOCITY[k][0]120 BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]121 BALL_VELOCITY[k][0] =temp_x122 BALL_VELOCITY[k][1] =temp_y123

    124 BALL_POINTS[j][0] += SPEED *BALL_VELOCITY[j][0]125 BALL_POINTS[j][1] += SPEED * BALL_VELOCITY[j][1]126

    127 pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))128 game_info = 'Balls:' +str(len(balls))129 text = font.render(game_info, True, (255,255,255))130 author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))131 screen.blit(text, (0, SCREEN_HEIGHT+5))132 screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5))133

    134 pygame.display.update()

    ============================================

    v1.1 Edit by Hongten

    v1.1修改如下:

    1.增加了背景音乐

    2.增加小球的时候,会伴随音乐产生

    3.窗口左下角显示小球个数,速度,以及最后一个小球的位置

    ============================================

    1 #pygame draw

    2

    3 importpygame4 from pygame.locals import *

    5 from sys importexit6 from random import *

    7

    8 '''

    9 游戏名称:10 小球完全弹性碰撞11 游戏规则:12 1.游戏初始化的时候,有5个不同颜色的小球进行碰撞13 2.玩家可以通过在窗口中单击鼠标左键进行增加小球个数14 3.玩家可以通过在窗口中单击鼠标右键进行删减小球个数15 4.玩家可以通过键盘的方向键:上,右键进行对小球加速16 5.玩家可以通过键盘的方向键:下,左键进行对小球减速17 6.玩家可以按键盘:f键实现全屏显示18 7.玩家可以按键盘:Esc键实现退出全屏操作19 8.窗口左下角显示小球个数,右下角显示作者邮箱20

    21 v1.1修改如下:22 1.增加了背景音乐23 2.增加小球的时候,会伴随音乐产生24 3.窗口左下角显示小球个数,速度,以及最后一个小球的位置25

    26 '''

    27 __author__ = {'name' : 'Hongten',28 'mail' : 'hongtenzone@foxmail.com',29 'blog' : 'http://www.cnblogs.com/hongten',30 'version' : '1.1'}31

    32 if not pygame.font: print('Warning, fonts disabled')33 if not pygame.mixer: print('Warning, sound disabled')34

    35 pygame.init()36 pygame.display.set_caption('Ball Game')37

    38 SCREEN_WIDTH = 600

    39 SCREEN_HEIGHT = 500

    40 SPEED = 1

    41 SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)42 SCREEN_DEFAULT_COLOR = (255, 255 ,255)43 READY =044

    45 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)46 screen.fill(SCREEN_DEFAULT_COLOR)47 bg = pygame.image.load('data\\image\\bg.jpg').convert()48 font = pygame.font.Font('data\\font\\TORK____.ttf', 14)49 new_sound = pygame.mixer.Sound('data\\sound\\new.wav')50 bg_sound = pygame.mixer.Sound('data\\sound\\bg.ogg')51 bg_sound.set_volume(0.5)52 bg_sound.play(-1)53 new_sound.set_volume(1.0)54

    55

    56 balls =[]57 BALL_R = 30

    58 BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]59 BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]60 BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]]61

    62 for i inrange(len(BALL_COLORS)):63 screen.fill(SCREEN_DEFAULT_COLOR)64 b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)65 balls.append(b)66

    67 while 1:68 for event inpygame.event.get():69 if event.type ==QUIT:70 bg_sound.stop()71 exit()72 elif event.type ==KEYDOWN:73 if event.key ==K_UP:74 SPEED += 0.1

    75 elif event.key ==K_DOWN:76 SPEED -= 0.1

    77 elif event.key ==K_LEFT:78 SPEED -= 0.1

    79 elif event.key ==K_RIGHT:80 SPEED += 0.1

    81 elif event.key ==K_f:82 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)83 elif event.key == 27:84 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)85 elif event.type ==MOUSEBUTTONDOWN:86 pressed_array =pygame.mouse.get_pressed()87 for index inrange(len(pressed_array)):88 ifpressed_array[index]:89 if index ==0:90 new_sound.play(-1)91 c_color = (randint(0, 255), randint(0, 255), randint(0, 255))92 x, y = (BALL_R+1, BALL_R+1)93 c_r = randint(10, 100)94 c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]95 c =pygame.draw.circle(screen, c_color, (x, y), BALL_R)96 BALL_COLORS.append(c_color)97 BALL_POINTS.append([x, y])98 BALL_VELOCITY.append(c_v)99 balls.append(c)100 elif index == 2:101 if len(balls) > 1:102 balls.pop(0)103 BALL_COLORS.pop(0)104 BALL_POINTS.pop(0)105 BALL_VELOCITY.pop(0)106 elif event.type ==MOUSEBUTTONUP:107 new_sound.stop()108

    109 #print(balls)

    110 for i inrange(len(balls)):111 screen.blit(bg, (-300, -100))112 for n inrange(len(balls)):113 '''

    114 if ((BALL_POINTS[i][0] - BALL_R) > 0 and (BALL_POINTS[i][0] - BALL_R) < BALL_R):115 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] + BALL_R),int(BALL_POINTS[n][1])), BALL_R)116 elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_WIDTH and (BALL_POINTS[i][1] + BALL_R) > SCREEN_WIDTH - BALL_R):117 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] - BALL_R),int(BALL_POINTS[n][1])), BALL_R)118 elif ((BALL_POINTS[i][1] - BALL_R) > 0 and (BALL_POINTS[i][1] - BALL_R) < BALL_R):119 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] + BALL_R)), BALL_R)120 elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_HEIGHT and (BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT - BALL_R):121 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] - BALL_R)), BALL_R)122 '''

    123 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)124 if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) >SCREEN_WIDTH))):125 BALL_VELOCITY[i][0] = -1 *BALL_VELOCITY[i][0]126 if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) >SCREEN_HEIGHT))):127 BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1]128

    129 for j inrange(len(balls)):130 for k inrange(len(balls)):131 b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2

    132 b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2

    133 b_r =(BALL_R*2)**2

    134 if (round((b_x + b_y), 2) <= round(b_r, 2)):135 temp_x =BALL_VELOCITY[j][0]136 temp_y = BALL_VELOCITY[j][1]137 BALL_VELOCITY[j][0] =BALL_VELOCITY[k][0]138 BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]139 BALL_VELOCITY[k][0] =temp_x140 BALL_VELOCITY[k][1] =temp_y141

    142 BALL_POINTS[j][0] += round(SPEED, 1) *BALL_VELOCITY[j][0]143 BALL_POINTS[j][1] += round(SPEED, 1) * BALL_VELOCITY[j][1]144

    145 pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))146 game_info = 'Balls:' + str(len(balls)) + 'Speed:' + str(round(SPEED, 2)) + 'LastBall:' + str(round(BALL_POINTS[-1][0])) + ',' + str(round(BALL_POINTS[-1][1]))147 text = font.render(game_info, True, (255,255,255))148 author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))149 screen.blit(text, (0, SCREEN_HEIGHT+5))150 screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5))151

    152 pygame.display.update()

    ==================================================

    Edit By Hongten

    v1.2修改如下:

    1.修改键盘方向键:左,右键为调节音量(0, 10)

    2.在状态栏添加音量状态信息:数字和图形显示

    ==================================================

    1 #pygame draw

    2

    3 importpygame4 from pygame.locals import *

    5 from sys importexit6 from random import *

    7

    8 '''

    9 游戏名称:10 小球完全弹性碰撞11 游戏规则:12 1.游戏初始化的时候,有5个不同颜色的小球进行碰撞13 2.玩家可以通过在窗口中单击鼠标左键进行增加小球个数14 3.玩家可以通过在窗口中单击鼠标右键进行删减小球个数15 4.玩家可以通过键盘的方向键:上,右键进行对小球加速16 5.玩家可以通过键盘的方向键:下,左键进行对小球减速17 6.玩家可以按键盘:f键实现全屏显示18 7.玩家可以按键盘:Esc键实现退出全屏操作19 8.窗口左下角显示小球个数,右下角显示作者邮箱20

    21 v1.1修改如下:22 1.增加了背景音乐23 2.增加小球的时候,会伴随音乐产生24 3.窗口左下角显示小球个数,速度,以及最后一个小球的位置25

    26 v1.2修改如下:27 1.修改键盘方向键:左,右键为调节音量(0, 10)28 2.在状态栏添加音量状态信息:数字和图形显示29

    30 '''

    31 __author__ = {'name' : 'Hongten',32 'mail' : 'hongtenzone@foxmail.com',33 'blog' : 'http://www.cnblogs.com/hongten',34 'version' : '1.2'}35

    36 if not pygame.font: print('Warning, fonts disabled')37 if not pygame.mixer: print('Warning, sound disabled')38

    39 pygame.init()40 pygame.display.set_caption('Ball Game')41

    42 SCREEN_WIDTH = 600

    43 SCREEN_HEIGHT = 500

    44 SPEED = 1

    45 VOLUME = 5

    46 SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)47 SCREEN_DEFAULT_COLOR = (255, 255 ,255)48 READY =049

    50 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)51 screen.fill(SCREEN_DEFAULT_COLOR)52 bg = pygame.image.load('data\\image\\bg.jpg').convert()53 font = pygame.font.Font('data\\font\\TORK____.ttf', 14)54 new_sound = pygame.mixer.Sound('data\\sound\\new.wav')55 bg_sound = pygame.mixer.Sound('data\\sound\\bg.ogg')56 bg_sound.set_volume(0.1 *VOLUME)57 bg_sound.play(-1)58 new_sound.set_volume(0.1 *VOLUME)59

    60

    61 balls =[]62 BALL_R = 30

    63 BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]64 BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]65 BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]]66

    67 VOLUME_POINTS =[]68 VOLUME_POINTS_START =[]69 VOLUME_RECT_COLORS =[]70 for p in range(170, 250, 7):71 VOLUME_POINTS.append([SCREEN_WIDTH - p,SCREEN_HEIGHT + 20])72 for ps in range(175, 250, 7):73 VOLUME_POINTS_START.append([SCREEN_WIDTH -ps, SCREEN_HEIGHT])74 VOLUME_RECT_COLORS.append((randint(0, 255), randint(0, 255), randint(0, 255)))75

    76

    77 print(VOLUME_POINTS[-10])78 print(VOLUME_POINTS_START[-10])79

    80 for i inrange(len(BALL_COLORS)):81 screen.fill(SCREEN_DEFAULT_COLOR)82 b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)83 balls.append(b)84

    85 while 1:86 for event inpygame.event.get():87 if event.type ==QUIT:88 bg_sound.stop()89 exit()90 elif event.type ==KEYDOWN:91 if event.key ==K_UP:92 SPEED += 0.1

    93 elif event.key ==K_DOWN:94 SPEED -= 0.1

    95 elif event.key ==K_LEFT:96 if VOLUME >0:97 VOLUME -= 1

    98 elif event.key ==K_RIGHT:99 if VOLUME <= 9:100 VOLUME += 1

    101 elif event.key ==K_f:102 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)103 elif event.key == 27:104 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)105 elif event.type ==MOUSEBUTTONDOWN:106 pressed_array =pygame.mouse.get_pressed()107 for index inrange(len(pressed_array)):108 ifpressed_array[index]:109 if index ==0:110 new_sound.play(-1)111 c_color = (randint(0, 255), randint(0, 255), randint(0, 255))112 x, y = (BALL_R+1, BALL_R+1)113 c_r = randint(10, 100)114 c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]115 c =pygame.draw.circle(screen, c_color, (x, y), BALL_R)116 BALL_COLORS.append(c_color)117 BALL_POINTS.append([x, y])118 BALL_VELOCITY.append(c_v)119 balls.append(c)120 elif index == 2:121 if len(balls) > 1:122 balls.pop(0)123 BALL_COLORS.pop(0)124 BALL_POINTS.pop(0)125 BALL_VELOCITY.pop(0)126 elif event.type ==MOUSEBUTTONUP:127 new_sound.stop()128

    129 #print(balls)

    130 for i inrange(len(balls)):131 screen.blit(bg, (-300, -100))132 #screen.fill(SCREEN_DEFAULT_COLOR)

    133 for n inrange(len(balls)):134 '''

    135 if ((BALL_POINTS[i][0] - BALL_R) > 0 and (BALL_POINTS[i][0] - BALL_R) < BALL_R):136 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] + BALL_R),int(BALL_POINTS[n][1])), BALL_R)137 elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_WIDTH and (BALL_POINTS[i][1] + BALL_R) > SCREEN_WIDTH - BALL_R):138 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] - BALL_R),int(BALL_POINTS[n][1])), BALL_R)139 elif ((BALL_POINTS[i][1] - BALL_R) > 0 and (BALL_POINTS[i][1] - BALL_R) < BALL_R):140 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] + BALL_R)), BALL_R)141 elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_HEIGHT and (BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT - BALL_R):142 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] - BALL_R)), BALL_R)143 '''

    144 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)145 if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) >SCREEN_WIDTH))):146 BALL_VELOCITY[i][0] = -1 *BALL_VELOCITY[i][0]147 if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) >SCREEN_HEIGHT))):148 BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1]149

    150 for j inrange(len(balls)):151 for k inrange(len(balls)):152 b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2

    153 b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2

    154 b_r =(BALL_R*2)**2

    155 if (round((b_x + b_y), 2) <= round(b_r, 2)):156 temp_x =BALL_VELOCITY[j][0]157 temp_y = BALL_VELOCITY[j][1]158 BALL_VELOCITY[j][0] =BALL_VELOCITY[k][0]159 BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]160 BALL_VELOCITY[k][0] =temp_x161 BALL_VELOCITY[k][1] =temp_y162

    163 BALL_POINTS[j][0] += round(SPEED, 1) *BALL_VELOCITY[j][0]164 BALL_POINTS[j][1] += round(SPEED, 1) * BALL_VELOCITY[j][1]165

    166 pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))167 bg_sound.set_volume(0.1 *VOLUME)168 new_sound.set_volume(0.1 *VOLUME)169 pygame.draw.rect(screen,170 (255, 255, 255),171 Rect((VOLUME_POINTS_START[-1][0],172 VOLUME_POINTS_START[-1][1]),173 (VOLUME_POINTS[-10][0] - VOLUME_POINTS_START[-1][0],174 20)))175 for v in range(VOLUME+1):176 if v >0:177 pygame.draw.rect(screen,178 VOLUME_RECT_COLORS[v],179 Rect((VOLUME_POINTS_START[-v][0],180 VOLUME_POINTS_START[-v][1]),181 (VOLUME_POINTS[-v][0] - VOLUME_POINTS_START[-v][0],182 20)))183

    184 game_info = 'Balls:' + str(len(balls)) + 'Speed:' + str(round(SPEED, 2)) + 'LastBall:' + str(round(BALL_POINTS[-1][0])) + ',' + str(round(BALL_POINTS[-1][1]))185 text = font.render(game_info, True, (255,255,255))186 author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))187 volume_text = font.render('Volume:' + str(VOLUME), True, (255, 255, 255))188 screen.blit(text, (0, SCREEN_HEIGHT+5))189 screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5))190 screen.blit(volume_text, (SCREEN_WIDTH - 310, SCREEN_HEIGHT+5))191 pygame.display.update()

    ================================================

    v1.3 Edity by Hongten

    Change Chinese to English

    ================================================

    The View of the Game:

    1 #pygame draw

    2

    3 importpygame4 from pygame.locals import *

    5 from sys importexit6 from random import *

    7

    8 '''

    9 Game Name:10 Ball Perfectly Elastic Collision(BPEC)11 Rules or Description:12 1.There are five balls whth different color have elastic collision13 after the game loaded.14 2.The player can click the window with the LEFT mouse button15 and create the new ball,which with the different color,but16 sometimes maybe like other balls.17 3.The player can click teh window with RIGHT mouse button18 and minus a few balls.19 4.You can change all ball speed by pressing the UP and DOWN20 the keyboard direction key.21 5.Also you can change the background music by pressing the LEFT22 and RIGHT the keyboard direction key(volume:0-10).23 6.Maybe you want to full screen view,By pressing the F key24 and ESC key switch.25 7.Ball number,the speed,the volume,author E-mail is written26 in the status bar at the bottom.27 '''

    28 __version__ = '1.3'

    29 __author__ = {'name' : 'Hongten',30 'mail' : 'hongtenzone@foxmail.com',31 'blog' : 'http://www.cnblogs.com/hongten',32 'version' : __version__}33

    34 if not pygame.font: print('Warning, fonts disabled')35 if not pygame.mixer: print('Warning, sound disabled')36

    37 pygame.init()38 pygame.display.set_caption('Ball Game')39

    40 SCREEN_WIDTH = 600

    41 SCREEN_HEIGHT = 500

    42 SPEED = 1

    43 VOLUME = 5

    44 SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)45 SCREEN_DEFAULT_COLOR = (255, 255 ,255)46 READY =047

    48 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)49 screen.fill(SCREEN_DEFAULT_COLOR)50 bg = pygame.image.load('data\\image\\bg.jpg').convert()51 font = pygame.font.Font('data\\font\\TORK____.ttf', 14)52 new_sound = pygame.mixer.Sound('data\\sound\\new.wav')53 bg_sound = pygame.mixer.Sound('data\\sound\\bg.ogg')54 bg_sound.set_volume(0.1 *VOLUME)55 bg_sound.play(-1)56 new_sound.set_volume(0.1 *VOLUME)57

    58

    59 balls =[]60 BALL_R = 30

    61 BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]62 BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]63 BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]]64

    65 VOLUME_POINTS =[]66 VOLUME_POINTS_START =[]67 VOLUME_RECT_COLORS =[]68 for p in range(170, 250, 7):69 VOLUME_POINTS.append([SCREEN_WIDTH - p,SCREEN_HEIGHT + 20])70 for ps in range(175, 250, 7):71 VOLUME_POINTS_START.append([SCREEN_WIDTH -ps, SCREEN_HEIGHT])72 VOLUME_RECT_COLORS.append((randint(0, 255), randint(0, 255), randint(0, 255)))73

    74 for i inrange(len(BALL_COLORS)):75 screen.fill(SCREEN_DEFAULT_COLOR)76 b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)77 balls.append(b)78

    79 while 1:80 for event inpygame.event.get():81 if event.type ==QUIT:82 bg_sound.stop()83 exit()84 elif event.type ==KEYDOWN:85 if event.key ==K_UP:86 SPEED += 0.1

    87 elif event.key ==K_DOWN:88 SPEED -= 0.1

    89 elif event.key ==K_LEFT:90 if VOLUME >0:91 VOLUME -= 1

    92 elif event.key ==K_RIGHT:93 if VOLUME <= 9:94 VOLUME += 1

    95 elif event.key ==K_f:96 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)97 elif event.key == 27:98 pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)99 elif event.type ==MOUSEBUTTONDOWN:100 pressed_array =pygame.mouse.get_pressed()101 for index inrange(len(pressed_array)):102 ifpressed_array[index]:103 if index ==0:104 new_sound.play(-1)105 c_color = (randint(0, 255), randint(0, 255), randint(0, 255))106 x, y = (BALL_R+1, BALL_R+1)107 c_r = randint(10, 100)108 c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]109 c =pygame.draw.circle(screen, c_color, (x, y), BALL_R)110 BALL_COLORS.append(c_color)111 BALL_POINTS.append([x, y])112 BALL_VELOCITY.append(c_v)113 balls.append(c)114 elif index == 2:115 if len(balls) > 1:116 balls.pop(0)117 BALL_COLORS.pop(0)118 BALL_POINTS.pop(0)119 BALL_VELOCITY.pop(0)120 elif event.type ==MOUSEBUTTONUP:121 new_sound.stop()122

    123 #print(balls)

    124 for i inrange(len(balls)):125 screen.blit(bg, (-300, -100))126 #screen.fill(SCREEN_DEFAULT_COLOR)

    127 for n inrange(len(balls)):128 pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)129 if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) >SCREEN_WIDTH))):130 BALL_VELOCITY[i][0] = -1 *BALL_VELOCITY[i][0]131 if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) >SCREEN_HEIGHT))):132 BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1]133

    134 for j inrange(len(balls)):135 for k inrange(len(balls)):136 b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2

    137 b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2

    138 b_r =(BALL_R*2)**2

    139 if (round((b_x + b_y), 2) <= round(b_r, 2)):140 temp_x =BALL_VELOCITY[j][0]141 temp_y = BALL_VELOCITY[j][1]142 BALL_VELOCITY[j][0] =BALL_VELOCITY[k][0]143 BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]144 BALL_VELOCITY[k][0] =temp_x145 BALL_VELOCITY[k][1] =temp_y146

    147 BALL_POINTS[j][0] += round(SPEED, 1) *BALL_VELOCITY[j][0]148 BALL_POINTS[j][1] += round(SPEED, 1) * BALL_VELOCITY[j][1]149

    150 pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))151 bg_sound.set_volume(0.1 *VOLUME)152 new_sound.set_volume(0.1 *VOLUME)153 pygame.draw.rect(screen,154 (255, 255, 255),155 Rect((VOLUME_POINTS_START[-1][0],156 VOLUME_POINTS_START[-1][1]),157 (VOLUME_POINTS[-10][0] - VOLUME_POINTS_START[-1][0],158 20)))159 for v in range(VOLUME+1):160 if v >0:161 pygame.draw.rect(screen,162 VOLUME_RECT_COLORS[v],163 Rect((VOLUME_POINTS_START[-v][0],164 VOLUME_POINTS_START[-v][1]),165 (VOLUME_POINTS[-v][0] - VOLUME_POINTS_START[-v][0],166 20)))167

    168 game_info = 'Balls:' + str(len(balls)) + 'Speed:' + str(round(SPEED, 2)) + 'LastBall:' + str(round(BALL_POINTS[-1][0])) + ',' + str(round(BALL_POINTS[-1][1]))169 text = font.render(game_info, True, (255,255,255))170 author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))171 volume_text = font.render('Volume:' + str(VOLUME), True, (255, 255, 255))172 screen.blit(text, (0, SCREEN_HEIGHT+5))173 screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5))174 screen.blit(volume_text, (SCREEN_WIDTH - 310, SCREEN_HEIGHT+5))175 pygame.display.update()

    ========================================================

    More reading,and english is important.

    I'm Hongten

    大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。

    Hongten博客排名在100名以内。粉丝过千。

    Hongten出品,必是精品。

    E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

    ========================================================

    展开全文
  • 完成这个小球的完全弹性碰撞游戏灵感来自于: 下面是我花了一周下班时间所编写的一个小球完全弹性碰撞游戏游戏初始化状态: 最下面的游标和修改小球的移动速度 ======================================...

    完成这个小球的完全弹性碰撞游戏灵感来自于:

    下面是我花了一周下班时间所编写的一个小球完全弹性碰撞游戏:

    游戏初始化状态:

    最下面的游标和修改小球的移动速度

    ===================================================

    源码部分:

    ===================================================

      1 #python tkinter
      2 #python version 3.3.2
      3 
      4 from tkinter import *
      5 
      6 '''
      7     判断
      8     两个小球
      9     {
     10         圆心:A(x1,y1)  半径:r  X轴速度:Vax  Y轴速度:Vay
     11         圆心:B(x2,y2)  半径:R  X轴速度:Vbx  Y轴速度:Vby
     12     }
     13     碰撞的条件是:
     14     1.两个小球的圆心距离不大于两小球半径之和(r+R),即:
     15     {
     16         (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
     17     }
     18     2.小球碰撞后,两小球的数度交换,即:
     19     {
     20         tempVax = Vax
     21         tempVay = Vay
     22         Vax = Vbx
     23         Vay = Vby
     24         Vbx = tempVax
     25         Vby = tempVay
     26         或:
     27         Vax = Vax + Vbx
     28         Vbx = Vax - Vbx
     29         Vax = Vax - Vbx
     30         Vay = Vay + Vby
     31         Vby = Vay - Vby
     32         Vay = Vay - Vby
     33     }
     34 
     35     游戏规则:
     36     五小球在画布中移动,他们之间会产生碰撞,当然小球和上下左右都会产生碰撞
     37     碰撞后,小球会改变方向返回
     38     而最下面的游标则用于调节小球的移动速度,游标的范围是[-100, 100]
     39     
     40     缺陷或BUG:
     41     1.在修改游标数据从而改变小球移动速度的时候,小球移动的距离得不到及时的更新
     42     导致小球可能会逃离画布
     43     2.小球在运动的过程中,有时候也有可能逃离画布
     44 
     45     总结:
     46     完成这个游戏,花了一个星期的下班时间。在这个过程中不仅回去学习了高中的数学知识,
     47     物理知识,很多东西都忘得差不多了,不过很快又学返回来了。
     48     游戏其实很多就是数学问题。
     49     
     50     游戏中还存在缺陷或BUG,希望志同道合者可以共同完善。
     51 '''
     52 
     53 __author__ = {'author' : 'Hongten',
     54               'Email' : 'hongtenzone@foxmail.com',
     55               'Blog' : 'http://www.cnblogs.com/hongten/',
     56               'Created' : '2013-09-28',
     57               'Version' : '1.0'}
     58 
     59 class Pong(Frame):
     60     def createWidgets(self):
     61         ## 画布
     62         self.draw = Canvas(self, width="5i", height="5i", bg='white')
     63 
     64         ## 游标(控制小球移动速度,范围:[-100, 100])
     65         self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
     66                            from_=-100, to=100)
     67 
     68         self.speed.pack(side=BOTTOM, fill=X)
     69 
     70         #小球碰撞墙壁的范围
     71         self.scaling_right = 4.8
     72         self.scaling_left = 0.2
     73         #小球直径
     74         self.ball_d = 0.4
     75         #游标度数
     76         self.scale_value = self.speed.get()
     77         #放缩率
     78         self.scaling = 100.0
     79 
     80         #存放小球数组
     81         self.balls = []
     82         #存放小球x坐标数组
     83         self.ball_x = []
     84         #存放小球y坐标数组
     85         self.ball_y = []
     86         #存放小球x轴方向速度数组
     87         self.ball_v_x = []
     88         #存放小球y轴方向速度数组
     89         self.ball_v_y = []
     90 
     91         # 五个小球
     92         self.ball = self.draw.create_oval("0.10i", "0.10i", "0.50i", "0.50i",
     93                                           fill="red")
     94         self.second_ball = self.draw.create_oval("0.70i", "0.70i", "1.10i", "1.10i",
     95                                                  fill='black')
     96         self.three_ball = self.draw.create_oval("1.30i", "1.30i", "1.70i", "1.70i",
     97                                                  fill='brown')
     98         self.four_ball = self.draw.create_oval("2.0i", "2.0i", "2.40i", "2.40i",
     99                                                  fill='green')
    100         self.five_ball = self.draw.create_oval("3.0i", "3.0i", "3.40i", "3.40i",
    101                                                  fill='gray')
    102 
    103         #把五个小球放入数组
    104         self.balls.append(self.ball)
    105         self.balls.append(self.second_ball)
    106         self.balls.append(self.three_ball)
    107         self.balls.append(self.four_ball)
    108         self.balls.append(self.five_ball)
    109 
    110         #第一个小球,即self.ball的圆心坐标(self.x, self.y),这里进行了放缩,目的是为了
    111         #在小球移动的过程中更加流畅
    112         self.x = 0.3        
    113         self.y = 0.3
    114         #第一个小球的速度方向
    115         self.velocity_x = -0.2
    116         self.velocity_y = 0.5
    117 
    118         self.second_ball_x = 0.9
    119         self.second_ball_y = 0.9
    120         self.second_ball_v_x = 0.4
    121         self.second_ball_v_y = -0.5
    122 
    123         self.three_ball_x = 1.5
    124         self.three_ball_y = 1.5
    125         self.three_ball_v_x = -0.3
    126         self.three_ball_v_y = -0.5
    127 
    128         self.four_ball_x = 2.2
    129         self.four_ball_y = 2.2
    130         self.four_ball_v_x = 0.1
    131         self.four_ball_v_y = -0.5
    132 
    133         self.five_ball_x = 3.2
    134         self.five_ball_y = 3.2
    135         self.five_ball_v_x = 0.3
    136         self.five_ball_v_y = 0.5
    137 
    138         
    139         #更新小球的坐标
    140         self.update_ball_x_y()
    141         self.draw.pack(side=LEFT)
    142 
    143     def update_ball_x_y(self, *args):
    144         '''更新小球的坐标,即把各个小球的圆心坐标信息以及速度信息存放到数组中,
    145            便于在后面循环遍历的时候使用。'''
    146         #第一个小球信息
    147         self.ball_x.append(self.x)
    148         self.ball_y.append(self.y)
    149         self.ball_v_x.append(self.velocity_x)
    150         self.ball_v_y.append(self.velocity_y)
    151 
    152         self.ball_x.append(self.second_ball_x)
    153         self.ball_y.append(self.second_ball_y)
    154         self.ball_v_x.append(self.second_ball_v_x)
    155         self.ball_v_y.append(self.second_ball_v_y)
    156 
    157         self.ball_x.append(self.three_ball_x)
    158         self.ball_y.append(self.three_ball_y)
    159         self.ball_v_x.append(self.three_ball_v_x)
    160         self.ball_v_y.append(self.three_ball_v_y)
    161 
    162         self.ball_x.append(self.four_ball_x)
    163         self.ball_y.append(self.four_ball_y)
    164         self.ball_v_x.append(self.four_ball_v_x)
    165         self.ball_v_y.append(self.four_ball_v_y)
    166 
    167         self.ball_x.append(self.five_ball_x)
    168         self.ball_y.append(self.five_ball_y)
    169         self.ball_v_x.append(self.five_ball_v_x)
    170         self.ball_v_y.append(self.five_ball_v_y)
    171     
    172     def update_ball_velocity(self, index, *args):
    173         '''更新各个小球速度信息,即小球碰撞到四周和另外的小球索要更新的速度信息'''
    174         #游标值
    175         self.scale_value = self.speed.get()
    176         #碰撞墙壁
    177         if (self.ball_x[index] > self.scaling_right) or (self.ball_x[index] < self.scaling_left):
    178             self.ball_v_x[index] = -1.0 * self.ball_v_x[index]
    179         if (self.ball_y[index] > self.scaling_right) or (self.ball_y[index] < self.scaling_left):
    180             self.ball_v_y[index] = -1.0 *  self.ball_v_y[index]
    181 
    182         '''
    183         #TEST:
    184         for n in range(len(self.balls)):
    185             #print((self.ball_x[index] - self.ball_x[n])**2)
    186             #print(round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2))
    187             print(round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2) <= round(self.ball_d**2, 2))
    188         '''
    189         for n in range(len(self.balls)):
    190             #小球碰撞条件,即:(x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
    191             if (round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2) <= round(self.ball_d**2, 2)):
    192                 #两小球速度交换
    193                 temp_vx = self.ball_v_x[index]
    194                 temp_vy = self.ball_v_y[index]
    195                 self.ball_v_x[index] = self.ball_v_x[n]
    196                 self.ball_v_y[index] = self.ball_v_y[n]
    197                 self.ball_v_x[n] = temp_vx
    198                 self.ball_v_y[n] = temp_vy
    199         #print(self.ball_v_x, self.ball_v_y)
    200                
    201         '''
    202         #WRONG:
    203         for n in range(len(self.balls)):            
    204             if (((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2) <= self.ball_d**2):
    205                 #两小球速度交换
    206                 self.ball_v_x[index] = self.ball_v_x[index] + self.ball_v_x[n]
    207                 self.ball_v_x[n] = self.ball_v_x[0] - self.ball_v_x[n]
    208                 self.ball_v_x[index] = self.ball_v_x[index] - self.ball_v_x[n]
    209                 self.ball_v_y[index] = self.ball_v_y[index] + self.ball_v_y[n]
    210                 self.ball_v_y[n] = self.ball_v_y[index] - self.ball_v_y[n]
    211                 self.ball_v_y[index] = self.ball_v_y[index] - self.ball_v_y[n]
    212         print(self.ball_v_x, self.ball_v_y)
    213         '''
    214         
    215     def get_ball_deltax(self, index, *args):
    216         '''获取小球X轴坐标移动距离并且更新小球的圆心X坐标,返回X轴所需移动距离'''
    217         deltax = (self.ball_v_x[index] * self.scale_value / self.scaling)
    218         self.ball_x[index] = self.ball_x[index] + deltax
    219         return deltax
    220 
    221     def get_ball_deltay(self, index, *args):
    222         '''获取小球Y轴坐标移动距离并且更新小球的圆心Y坐标,返回Y轴所需移动距离'''
    223         deltay = (self.ball_v_y[index] * self.scale_value / self.scaling)
    224         self.ball_y[index] = self.ball_y[index] + deltay
    225         return deltay
    226     
    227     def moveBall(self, *args):
    228         '''移动第一个小球,编号为:0,这是根据数组:self.balls确定的。'''
    229         self.update_ball_velocity(0)       
    230         deltax = self.get_ball_deltax(0)
    231         deltay = self.get_ball_deltay(0)
    232         #小球移动
    233         self.draw.move(self.ball,  "%ri" % deltax, "%ri" % deltay)
    234         self.after(10, self.moveBall)
    235 
    236     def move_second_ball(self, *args):
    237         self.update_ball_velocity(1)       
    238         deltax = self.get_ball_deltax(1)
    239         deltay = self.get_ball_deltay(1)        
    240         self.draw.move(self.second_ball,  "%ri" % deltax, "%ri" % deltay)
    241         self.after(10, self.move_second_ball)
    242 
    243 
    244     def move_three_ball(self, *args):
    245         self.update_ball_velocity(2)       
    246         deltax = self.get_ball_deltax(2)
    247         deltay = self.get_ball_deltay(2)
    248         self.draw.move(self.three_ball,  "%ri" % deltax, "%ri" % deltay)
    249         self.after(10, self.move_three_ball)
    250 
    251     def move_four_ball(self, *args):
    252         self.update_ball_velocity(3)       
    253         deltax = self.get_ball_deltax(3)
    254         deltay = self.get_ball_deltay(3)
    255         self.draw.move(self.four_ball,  "%ri" % deltax, "%ri" % deltay)
    256         self.after(10, self.move_four_ball)
    257 
    258     def move_five_ball(self, *args):
    259         self.update_ball_velocity(4)       
    260         deltax = self.get_ball_deltax(4)
    261         deltay = self.get_ball_deltay(4)
    262         self.draw.move(self.five_ball,  "%ri" % deltax, "%ri" % deltay)
    263         self.after(10, self.move_five_ball)
    264 
    265             
    266     def __init__(self, master=None):
    267         '''初始化函数'''
    268         Frame.__init__(self, master)
    269         Pack.config(self)
    270         self.createWidgets()
    271         self.after(10, self.moveBall)
    272         self.after(10, self.move_three_ball)
    273         self.after(10, self.move_four_ball)
    274         self.after(10, self.move_five_ball)
    275         self.after(10, self.move_second_ball)
    276         
    277         
    278 game = Pong()
    279 
    280 game.mainloop()

    源码下载:http://files.cnblogs.com/hongten/python_tkinter_pong.1.0.zip

    ===================================================

    第二个版本 version.1.1:  Edit by Hongten 2013-09-28 17:40

    ===================================================

    修改记录:
    1.调整画布大小
    2.调整了小球的半径,以及小球的速度初始值,小球初始圆心坐标
    3.游标的范围修改为:[-200, 200]
    这些修改主要是针对上面的缺陷而进行的。

    优点:
    1.小球移动的过程更直观
    2.小球的移动速度变小,但是可以根据游标来修改小球移动速度
    3.界面比之前更加友好

    运行效果:

    ===================================================

      1 #python tkinter
      2 #python version 3.3.2
      3 
      4 from tkinter import *
      5 
      6 '''
      7     判断
      8     两个小球
      9     {
     10         圆心:A(x1,y1)  半径:r  X轴速度:Vax  Y轴速度:Vay
     11         圆心:B(x2,y2)  半径:R  X轴速度:Vbx  Y轴速度:Vby
     12     }
     13     碰撞的条件是:
     14     1.两个小球的圆心距离不大于两小球半径之和(r+R),即:
     15     {
     16         (x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
     17     }
     18     2.小球碰撞后,两小球的数度交换,即:
     19     {
     20         tempVax = Vax
     21         tempVay = Vay
     22         Vax = Vbx
     23         Vay = Vby
     24         Vbx = tempVax
     25         Vby = tempVay
     26         或:
     27         Vax = Vax + Vbx
     28         Vbx = Vax - Vbx
     29         Vax = Vax - Vbx
     30         Vay = Vay + Vby
     31         Vby = Vay - Vby
     32         Vay = Vay - Vby
     33     }
     34 
     35     游戏规则:
     36     五小球在画布中移动,他们之间会产生碰撞,当然小球和上下左右都会产生碰撞
     37     碰撞后,小球会改变方向返回
     38     而最下面的游标则用于调节小球的移动速度,游标的范围是[-100, 100]
     39     
     40     缺陷或BUG:
     41     1.在修改游标数据从而改变小球移动速度的时候,小球移动的距离得不到及时的更新
     42     导致小球可能会逃离画布
     43     2.小球在运动的过程中,有时候也有可能逃离画布
     44 
     45     总结:
     46     完成这个游戏,花了一个星期的下班时间。在这个过程中不仅回去学习了高中的数学知识,
     47     物理知识,很多东西都忘得差不多了,不过很快又学返回来了。
     48     游戏其实很多就是数学问题。
     49     
     50     游戏中还存在缺陷或BUG,希望志同道合者可以共同完善。
     51 
     52     修改记录:
     53     1.调整画布大小
     54     2.调整了小球的半径,以及小球的速度初始值,小球初始圆心坐标
     55     3.游标的范围修改为:[-200, 200]
     56     这些修改主要是针对上面的缺陷而进行的。
     57 
     58     优点:
     59     1.小球移动的过程更直观
     60     2.小球的移动速度变小,但是可以根据游标来修改小球移动速度
     61     3.界面比之前更加友好
     62 '''
     63 
     64 __author__ = {'author' : 'Hongten',
     65               'Email' : 'hongtenzone@foxmail.com',
     66               'Blog' : 'http://www.cnblogs.com/hongten/',
     67               'Created' : '2013-09-28',
     68               'Version' : '1.1'}
     69 
     70 class Pong(Frame):
     71     def createWidgets(self):
     72          #放缩率
     73         self.scaling = 100.0
     74         #画布比例
     75         self.canvas_width = 10
     76         self.canvas_height = 5.6
     77         ## 画布
     78         self.draw = Canvas(self, width=(self.canvas_width * self.scaling),
     79                            height=(self.canvas_height * self.scaling),
     80                            bg='white')
     81 
     82         ## 游标(控制小球移动速度,范围:[-100, 100])
     83         self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
     84                            from_=-200, to=200)
     85         
     86         self.speed.pack(side=BOTTOM, fill=X)
     87 
     88         #小球直径
     89         self.ball_d = 1.0
     90         #小球碰撞墙壁的范围
     91         self.scaling_left = round(self.ball_d / 2, 1)
     92         self.scaling_right = self.canvas_width - self.scaling_left
     93         self.scaling_bottom = self.canvas_height - self.scaling_left
     94         self.scaling_top = self.scaling_left
     95        
     96         #游标度数
     97         self.scale_value = self.speed.get() * 0.1
     98        
     99         #存放小球数组
    100         self.balls = []
    101         #存放小球x坐标数组
    102         self.ball_x = []
    103         #存放小球y坐标数组
    104         self.ball_y = []
    105         #存放小球x轴方向速度数组
    106         self.ball_v_x = []
    107         #存放小球y轴方向速度数组
    108         self.ball_v_y = []
    109 
    110         # 五个小球
    111         self.ball = self.draw.create_oval("0.60i", "0.60i", "1.60i", "1.60i",
    112                                           fill="red")
    113         self.second_ball = self.draw.create_oval("2.0i", "2.0i", "3.0i", "3.0i",
    114                                                  fill='black')
    115         self.three_ball = self.draw.create_oval("4.0i", "4.0i", "5.0i", "5.0i",
    116                                                  fill='brown')
    117         self.four_ball = self.draw.create_oval("6.0i", "2.0i", "7.0i", "3.0i",
    118                                                  fill='green')
    119         self.five_ball = self.draw.create_oval("8.0i", "3.0i", "9.0i", "4.0i",
    120                                                  fill='gray')
    121 
    122         #把五个小球放入数组
    123         self.balls.append(self.ball)
    124         self.balls.append(self.second_ball)
    125         self.balls.append(self.three_ball)
    126         self.balls.append(self.four_ball)
    127         self.balls.append(self.five_ball)
    128 
    129         #第一个小球,即self.ball的圆心坐标(self.x, self.y),这里进行了放缩,目的是为了
    130         #在小球移动的过程中更加流畅
    131         self.x = 1.1        
    132         self.y = 1.1
    133         #第一个小球的速度方向
    134         self.velocity_x = -0.2
    135         self.velocity_y = 0.1
    136 
    137         self.second_ball_x = 2.5
    138         self.second_ball_y = 2.5
    139         self.second_ball_v_x = 0.1
    140         self.second_ball_v_y = -0.2
    141 
    142         self.three_ball_x = 4.5
    143         self.three_ball_y = 4.5
    144         self.three_ball_v_x = -0.1
    145         self.three_ball_v_y = -0.2
    146 
    147         self.four_ball_x = 6.5
    148         self.four_ball_y = 2.5
    149         self.four_ball_v_x = 0.1
    150         self.four_ball_v_y = -0.2
    151 
    152         self.five_ball_x = 8.5
    153         self.five_ball_y = 3.5
    154         self.five_ball_v_x = 0.1
    155         self.five_ball_v_y = 0.2
    156 
    157         
    158         #更新小球的坐标
    159         self.update_ball_x_y()
    160         self.draw.pack(side=LEFT)
    161 
    162     def update_ball_x_y(self, *args):
    163         '''更新小球的坐标,即把各个小球的圆心坐标信息以及速度信息存放到数组中,
    164            便于在后面循环遍历的时候使用。'''
    165         #第一个小球信息
    166         self.ball_x.append(self.x)
    167         self.ball_y.append(self.y)
    168         self.ball_v_x.append(self.velocity_x)
    169         self.ball_v_y.append(self.velocity_y)
    170 
    171         self.ball_x.append(self.second_ball_x)
    172         self.ball_y.append(self.second_ball_y)
    173         self.ball_v_x.append(self.second_ball_v_x)
    174         self.ball_v_y.append(self.second_ball_v_y)
    175 
    176         self.ball_x.append(self.three_ball_x)
    177         self.ball_y.append(self.three_ball_y)
    178         self.ball_v_x.append(self.three_ball_v_x)
    179         self.ball_v_y.append(self.three_ball_v_y)
    180 
    181         self.ball_x.append(self.four_ball_x)
    182         self.ball_y.append(self.four_ball_y)
    183         self.ball_v_x.append(self.four_ball_v_x)
    184         self.ball_v_y.append(self.four_ball_v_y)
    185 
    186         self.ball_x.append(self.five_ball_x)
    187         self.ball_y.append(self.five_ball_y)
    188         self.ball_v_x.append(self.five_ball_v_x)
    189         self.ball_v_y.append(self.five_ball_v_y)
    190     
    191     def update_ball_velocity(self, index, *args):
    192         '''更新各个小球速度信息,即小球碰撞到四周和另外的小球索要更新的速度信息'''
    193         #游标值
    194         self.scale_value = self.speed.get() * 0.1
    195         #碰撞墙壁
    196         if (self.ball_x[index] > self.scaling_right) or (self.ball_x[index] < self.scaling_left):
    197             self.ball_v_x[index] = -1.0 * self.ball_v_x[index]
    198         if (self.ball_y[index] > self.scaling_bottom) or (self.ball_y[index] < self.scaling_top):
    199             self.ball_v_y[index] = -1.0 *  self.ball_v_y[index]
    200 
    201         '''
    202         #TEST:
    203         for n in range(len(self.balls)):
    204             #print((self.ball_x[index] - self.ball_x[n])**2)
    205             #print(round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2))
    206             print(round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2) <= round(self.ball_d**2, 2))
    207         '''
    208         for n in range(len(self.balls)):
    209             #小球碰撞条件,即:(x2 - x1)^2 + (y2 - y1)^2 <= (r + R)^2
    210             if (round((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2, 2) <= round(self.ball_d**2, 2)):
    211                 #两小球速度交换
    212                 temp_vx = self.ball_v_x[index]
    213                 temp_vy = self.ball_v_y[index]
    214                 self.ball_v_x[index] = self.ball_v_x[n]
    215                 self.ball_v_y[index] = self.ball_v_y[n]
    216                 self.ball_v_x[n] = temp_vx
    217                 self.ball_v_y[n] = temp_vy
    218         #print(self.ball_v_x, self.ball_v_y)
    219                
    220         '''
    221         #WRONG:
    222         for n in range(len(self.balls)):            
    223             if (((self.ball_x[index] - self.ball_x[n])**2 + (self.ball_y[index] - self.ball_y[n])**2) <= self.ball_d**2):
    224                 #两小球速度交换
    225                 self.ball_v_x[index] = self.ball_v_x[index] + self.ball_v_x[n]
    226                 self.ball_v_x[n] = self.ball_v_x[0] - self.ball_v_x[n]
    227                 self.ball_v_x[index] = self.ball_v_x[index] - self.ball_v_x[n]
    228                 self.ball_v_y[index] = self.ball_v_y[index] + self.ball_v_y[n]
    229                 self.ball_v_y[n] = self.ball_v_y[index] - self.ball_v_y[n]
    230                 self.ball_v_y[index] = self.ball_v_y[index] - self.ball_v_y[n]
    231         print(self.ball_v_x, self.ball_v_y)
    232         '''
    233         
    234     def get_ball_deltax(self, index, *args):
    235         '''获取小球X轴坐标移动距离并且更新小球的圆心X坐标,返回X轴所需移动距离'''
    236         deltax = (self.ball_v_x[index] * self.scale_value / self.scaling)
    237         self.ball_x[index] = self.ball_x[index] + deltax
    238         return deltax
    239 
    240     def get_ball_deltay(self, index, *args):
    241         '''获取小球Y轴坐标移动距离并且更新小球的圆心Y坐标,返回Y轴所需移动距离'''
    242         deltay = (self.ball_v_y[index] * self.scale_value / self.scaling)
    243         self.ball_y[index] = self.ball_y[index] + deltay
    244         return deltay
    245     
    246     def moveBall(self, *args):
    247         '''移动第一个小球,编号为:0,这是根据数组:self.balls确定的。'''
    248         self.update_ball_velocity(0)       
    249         deltax = self.get_ball_deltax(0)
    250         deltay = self.get_ball_deltay(0)
    251         #小球移动
    252         self.draw.move(self.ball,  "%ri" % deltax, "%ri" % deltay)
    253         self.after(10, self.moveBall)
    254 
    255     def move_second_ball(self, *args):
    256         self.update_ball_velocity(1)       
    257         deltax = self.get_ball_deltax(1)
    258         deltay = self.get_ball_deltay(1)        
    259         self.draw.move(self.second_ball,  "%ri" % deltax, "%ri" % deltay)
    260         self.after(10, self.move_second_ball)
    261 
    262 
    263     def move_three_ball(self, *args):
    264         self.update_ball_velocity(2)       
    265         deltax = self.get_ball_deltax(2)
    266         deltay = self.get_ball_deltay(2)
    267         self.draw.move(self.three_ball,  "%ri" % deltax, "%ri" % deltay)
    268         self.after(10, self.move_three_ball)
    269 
    270     def move_four_ball(self, *args):
    271         self.update_ball_velocity(3)       
    272         deltax = self.get_ball_deltax(3)
    273         deltay = self.get_ball_deltay(3)
    274         self.draw.move(self.four_ball,  "%ri" % deltax, "%ri" % deltay)
    275         self.after(10, self.move_four_ball)
    276 
    277     def move_five_ball(self, *args):
    278         self.update_ball_velocity(4)       
    279         deltax = self.get_ball_deltax(4)
    280         deltay = self.get_ball_deltay(4)
    281         self.draw.move(self.five_ball,  "%ri" % deltax, "%ri" % deltay)
    282         self.after(10, self.move_five_ball)
    283 
    284             
    285     def __init__(self, master=None):
    286         '''初始化函数'''
    287         Frame.__init__(self, master)
    288         Pack.config(self)
    289         self.createWidgets()
    290         self.after(10, self.moveBall)
    291         self.after(10, self.move_three_ball)
    292         self.after(10, self.move_four_ball)
    293         self.after(10, self.move_five_ball)
    294         self.after(10, self.move_second_ball)
    295         
    296         
    297 game = Pong()
    298 
    299 game.mainloop()

    源码下载:http://files.cnblogs.com/hongten/python_tkinter_pong.1.1.zip

     

    ========================================================

     

    More reading,and english is important.

     

    I'm Hongten

     

     

     

    大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
    Hongten博客排名在100名以内。粉丝过千。
    Hongten出品,必是精品。

     

    E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

     

    ========================================================

     

    转载于:https://www.cnblogs.com/hongten/p/hongten_python_pong.html

    展开全文
  • ios球体弹跳游戏源码

    2014-05-12 09:48:00
    一款耐玩的ios游戏源码,画面上有很多小星星,球体落下的时候,你需要在画面上画出一条条的线条让球体弹跳起来然后吃掉小星星,如果没借助球体就失败了。游戏有很多关卡。注意: <ignore_js_op> 153354...
  • 运用javaGUI界面制作一个小球砖块的小游戏,本次游戏只有一个关卡,只是做一个简单的效果不是全部出来的完整的下载链接:http://download.csdn.net/detail/zhuangjixiang/4874730定义一个小球的类,挡板的类和砖块...
  • 小球砖块游戏(JAVA)

    2012-12-11 21:46:00
    运用javaGUI界面制作一个小球砖块的小游戏,本次游戏只有一个关卡,只是做一个简单的效果不是全部出来的 完整的下载链接:http://download.csdn.net/detail/zhuangjixiang/4874730 定义一个小球的类,挡板的类和...
  • 运用javaGUI界面制作一个小球砖块的小游戏,本次游戏只有一个关卡,只是做一个简单的效果不是全部出来的完整的下载链接:http://download.csdn.net/detail/zhuangjixiang/4874730定义一个小球的类,挡板的类和砖块...
  • package com.swift; import ... ...上边是源码,下边是打包好的程序,下载解压后双击start文件可以运行(不需安装jre) 链接: https://pan.baidu.com/s/1eSyArOe 密码: nb28 效果如下图:  
  • 弹弹堂s应用宝版本是一款休闲益智手游,游戏采用卡通的画风,Q版的人物形象非常可爱,经典的PVP对战模式,让玩家感受竞技的乐趣。丰富的游戏玩法,总有一种适合你!还有多样的副本活动,海量的奖励等你来拿!还在等...
  • 网络台球游戏的网络编程(vc++)

    千次阅读 2013-04-03 13:06:56
    网络台球游戏的网络编程 转载请注明出处 本文章的源代码下载地址,请单击此链接 ...在游戏开始的时候,程序会出一个类型选择 框,如果4.14 所示,玩家可以选择作为服务器、客 户机或者是单人练习赛。  图4.14
  • 撞球(乒乓)_C#游戏源码下载,一个撞球游戏的源代码,用了一些贴图,小时候玩游戏的时候,经常会玩撞球游戏,像打乒乓一样将球弹出回去,消除方块,然后再回去,直到把方块打消完就胜了。如果你喜欢,就下载...
  • MFC 分裂游戏

    2011-06-10 17:15:22
    A D 左右移动 J 子弹 F2 重新开始 欢迎下载
  • 内有:吃钱游戏,打鸟,点泡泡游戏小球,俄罗斯3,俄罗斯方块2,翻拍,记忆纸牌,经典游戏连连看,开车,老虎机,拳击,扫雷,扫雷2,太空射击,网球,五子棋,抓蜘蛛等。
  • -源码

    2021-02-10 06:26:05
    游戏的目的是使穿过屏幕右侧的门柱,然后将画布在重力作用下留在底部。 您可以通过以适当的间隔通过单击鼠标来放置平台,使在屏幕上并通过门柱反弹来完成此任务。 理想情况下,您将需要尽可能少的时间并...
  • 用Flash CS3的AS制作跟随鼠标弹性小游戏,主要是练习AS的使用。 先看演示: 源文件下载: [img]/Files/BeyondPic/2007-9/28/rar.gif[/img]   [url=...
  • 一个可以边玩边下载游戏式Flash Loading动画源文件,在加载的过程中,鼠标可以随意拖动进度条到任意位置,且进度条就像一个档板一样,可以击中下落的小球,小球弹跳到其它地方,你可以像玩游戏一样一边玩一边等待...
  • brew 坠空

    2013-08-14 16:20:40
    brew写的一个小游戏,可以直接下载的运行的,vs2005,BREW SDK2.01,坠空的小游戏,可以实现小球的下落,起,方块的移动,地图的生成等等
  • 微信小程序游戏排行榜top1:欢乐斗地主欢乐斗地主小程序是一款打开就能和好友斗地主的...微信小程序游戏排行榜top2:欢乐球球欢乐球球小程序是一款3D效果的小游戏,玩法极其简单却能带给玩家刺激和乐趣,弹性十足...
  • 微信小程序游戏排行榜top1:欢乐斗地主欢乐斗地主小程序是一款打开就能和好友斗地主的...微信小程序游戏排行榜top2:欢乐球球欢乐球球小程序是一款3D效果的小游戏,玩法极其简单却能带给玩家刺激和乐趣,弹性十足...
  • Unity5.x入门游戏

    千次阅读 2016-11-08 12:43:35
    //游戏的项目素材和工程源码可以已经上传到CSDN我的资源,可以自行下载游戏介绍】 如图: //游戏由一个平面,球体,方块组成 //点击鼠标左键,方块起;点击鼠标右键,球体出 【游戏实现】 //创建一个...
  • JS抖音弹球打砖块游戏代码是一款一个球弹去的游戏特效下载
  • JS抖音弹球打砖块游戏代码是一款一个球弹去的游戏特效下载
  • 实现还不够完善,工程代码、图片音乐素材可以从百度网盘下载:链接:https://pan.baidu.com/s/1gcjDdn8HY373TcUSyrdC7Q 提取码:7q33 1 设计思路该程序功能的主体设计参考了网页小游戏“炮弹进框”(Cannon Strike)...

空空如也

空空如也

1 2 3 4 5
收藏数 85
精华内容 34
关键字:

弹弹球游戏下载