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

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

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

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

先看看图:

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

源码部分:

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

 #pygame draw

 import pygame
from pygame.locals import *
from sys import exit
from random import * '''
游戏名称:
小球完全弹性碰撞
游戏规则:
1.游戏初始化的时候,有5个不同颜色的小球进行碰撞
2.玩家可以通过在窗口中单击鼠标左键进行增加小球个数
3.玩家可以通过在窗口中单击鼠标右键进行删减小球个数
4.玩家可以通过键盘的方向键:上,右键进行对小球加速
5.玩家可以通过键盘的方向键:下,左键进行对小球减速
6.玩家可以按键盘:f键实现全屏显示
7.玩家可以按键盘:Esc键实现退出全屏操作
8.窗口左下角显示小球个数,右下角显示作者邮箱 '''
__author__ = {'name' : 'Hongten',
'mail' : 'hongtenzone@foxmail.com',
'blog' : 'http://www.cnblogs.com/hongten',
'version' : '1.0'} pygame.init()
pygame.display.set_caption('Ball Game') SCREEN_WIDTH = 600
SCREEN_HEIGHT = 500
SPEED = 1
SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)
SCREEN_DEFAULT_COLOR = (255, 255 ,255)
READY = 0 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
screen.fill(SCREEN_DEFAULT_COLOR)
bg = pygame.image.load('data\\image\\bg.jpg').convert()
font = pygame.font.Font('data\\font\\TORK____.ttf', 14) balls = []
BALL_R = 30
BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]
BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]
BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]] for i in range(len(BALL_COLORS)):
screen.fill(SCREEN_DEFAULT_COLOR)
b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)
balls.append(b) while 1:
for event in pygame.event.get():
if event.type == QUIT:
exit()
elif event.type == KEYDOWN:
if event.key == K_UP:
SPEED += 0.1
elif event.key == K_DOWN:
SPEED -= 0.1
elif event.key == K_LEFT:
SPEED -= 0.1
elif event.key == K_RIGHT:
SPEED += 0.1
elif event.key == K_f:
pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)
elif event.key == 27:
pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
elif event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
for index in range(len(pressed_array)):
if pressed_array[index]:
if index == 0:
c_color = (randint(0, 255), randint(0, 255), randint(0, 255))
x, y = (BALL_R+1, BALL_R+1)
c_r = randint(10, 100)
c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]
c = pygame.draw.circle(screen, c_color, (x, y), BALL_R)
BALL_COLORS.append(c_color)
BALL_POINTS.append([x, y])
BALL_VELOCITY.append(c_v)
balls.append(c)
elif index == 2:
if len(balls) > 1:
balls.pop(0)
BALL_COLORS.pop(0)
BALL_POINTS.pop(0)
BALL_VELOCITY.pop(0) #print(balls)
for i in range(len(balls)):
screen.blit(bg, (-300, -100))
for n in range(len(balls)):
'''
if ((BALL_POINTS[i][0] - BALL_R) > 0 and (BALL_POINTS[i][0] - BALL_R) < BALL_R):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] + BALL_R),int(BALL_POINTS[n][1])), BALL_R)
elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_WIDTH and (BALL_POINTS[i][1] + BALL_R) > SCREEN_WIDTH - BALL_R):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] - BALL_R),int(BALL_POINTS[n][1])), BALL_R)
elif ((BALL_POINTS[i][1] - BALL_R) > 0 and (BALL_POINTS[i][1] - BALL_R) < BALL_R):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] + BALL_R)), BALL_R)
elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_HEIGHT and (BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT - BALL_R):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] - BALL_R)), BALL_R)
'''
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)
if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) > SCREEN_WIDTH))):
BALL_VELOCITY[i][0] = -1 * BALL_VELOCITY[i][0]
if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT))):
BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1] for j in range(len(balls)):
for k in range(len(balls)):
b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2
b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2
b_r =(BALL_R*2)**2
if (round((b_x + b_y), 2) <= round(b_r, 2)):
temp_x = BALL_VELOCITY[j][0]
temp_y = BALL_VELOCITY[j][1]
BALL_VELOCITY[j][0] = BALL_VELOCITY[k][0]
BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]
BALL_VELOCITY[k][0] = temp_x
BALL_VELOCITY[k][1] = temp_y BALL_POINTS[j][0] += SPEED * BALL_VELOCITY[j][0]
BALL_POINTS[j][1] += SPEED * BALL_VELOCITY[j][1] pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))
game_info = 'Balls: ' + str(len(balls))
text = font.render(game_info, True, (255,255,255))
author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))
screen.blit(text, (0, SCREEN_HEIGHT+5))
screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5)) pygame.display.update()

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

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

v1.1 Edit by Hongten

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

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

 #pygame draw

 import pygame
from pygame.locals import *
from sys import exit
from random import * '''
游戏名称:
小球完全弹性碰撞
游戏规则:
1.游戏初始化的时候,有5个不同颜色的小球进行碰撞
2.玩家可以通过在窗口中单击鼠标左键进行增加小球个数
3.玩家可以通过在窗口中单击鼠标右键进行删减小球个数
4.玩家可以通过键盘的方向键:上,右键进行对小球加速
5.玩家可以通过键盘的方向键:下,左键进行对小球减速
6.玩家可以按键盘:f键实现全屏显示
7.玩家可以按键盘:Esc键实现退出全屏操作
8.窗口左下角显示小球个数,右下角显示作者邮箱 v1.1修改如下:
1.增加了背景音乐
2.增加小球的时候,会伴随音乐产生
3.窗口左下角显示小球个数,速度,以及最后一个小球的位置 '''
__author__ = {'name' : 'Hongten',
'mail' : 'hongtenzone@foxmail.com',
'blog' : 'http://www.cnblogs.com/hongten',
'version' : '1.1'} if not pygame.font: print('Warning, fonts disabled')
if not pygame.mixer: print('Warning, sound disabled') pygame.init()
pygame.display.set_caption('Ball Game') SCREEN_WIDTH = 600
SCREEN_HEIGHT = 500
SPEED = 1
SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)
SCREEN_DEFAULT_COLOR = (255, 255 ,255)
READY = 0 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
screen.fill(SCREEN_DEFAULT_COLOR)
bg = pygame.image.load('data\\image\\bg.jpg').convert()
font = pygame.font.Font('data\\font\\TORK____.ttf', 14)
new_sound = pygame.mixer.Sound('data\\sound\\new.wav')
bg_sound = pygame.mixer.Sound('data\\sound\\bg.ogg')
bg_sound.set_volume(0.5)
bg_sound.play(-1)
new_sound.set_volume(1.0) balls = []
BALL_R = 30
BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]
BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]
BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]] for i in range(len(BALL_COLORS)):
screen.fill(SCREEN_DEFAULT_COLOR)
b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)
balls.append(b) while 1:
for event in pygame.event.get():
if event.type == QUIT:
bg_sound.stop()
exit()
elif event.type == KEYDOWN:
if event.key == K_UP:
SPEED += 0.1
elif event.key == K_DOWN:
SPEED -= 0.1
elif event.key == K_LEFT:
SPEED -= 0.1
elif event.key == K_RIGHT:
SPEED += 0.1
elif event.key == K_f:
pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)
elif event.key == 27:
pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
elif event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
for index in range(len(pressed_array)):
if pressed_array[index]:
if index == 0:
new_sound.play(-1)
c_color = (randint(0, 255), randint(0, 255), randint(0, 255))
x, y = (BALL_R+1, BALL_R+1)
c_r = randint(10, 100)
c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]
c = pygame.draw.circle(screen, c_color, (x, y), BALL_R)
BALL_COLORS.append(c_color)
BALL_POINTS.append([x, y])
BALL_VELOCITY.append(c_v)
balls.append(c)
elif index == 2:
if len(balls) > 1:
balls.pop(0)
BALL_COLORS.pop(0)
BALL_POINTS.pop(0)
BALL_VELOCITY.pop(0)
elif event.type == MOUSEBUTTONUP:
new_sound.stop() #print(balls)
for i in range(len(balls)):
screen.blit(bg, (-300, -100))
for n in range(len(balls)):
'''
if ((BALL_POINTS[i][0] - BALL_R) > 0 and (BALL_POINTS[i][0] - BALL_R) < BALL_R):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] + BALL_R),int(BALL_POINTS[n][1])), BALL_R)
elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_WIDTH and (BALL_POINTS[i][1] + BALL_R) > SCREEN_WIDTH - BALL_R):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] - BALL_R),int(BALL_POINTS[n][1])), BALL_R)
elif ((BALL_POINTS[i][1] - BALL_R) > 0 and (BALL_POINTS[i][1] - BALL_R) < BALL_R):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] + BALL_R)), BALL_R)
elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_HEIGHT and (BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT - BALL_R):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] - BALL_R)), BALL_R)
'''
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)
if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) > SCREEN_WIDTH))):
BALL_VELOCITY[i][0] = -1 * BALL_VELOCITY[i][0]
if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT))):
BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1] for j in range(len(balls)):
for k in range(len(balls)):
b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2
b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2
b_r =(BALL_R*2)**2
if (round((b_x + b_y), 2) <= round(b_r, 2)):
temp_x = BALL_VELOCITY[j][0]
temp_y = BALL_VELOCITY[j][1]
BALL_VELOCITY[j][0] = BALL_VELOCITY[k][0]
BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]
BALL_VELOCITY[k][0] = temp_x
BALL_VELOCITY[k][1] = temp_y BALL_POINTS[j][0] += round(SPEED, 1) * BALL_VELOCITY[j][0]
BALL_POINTS[j][1] += round(SPEED, 1) * BALL_VELOCITY[j][1] pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))
game_info = 'Balls: ' + str(len(balls)) + ' Speed: ' + str(round(SPEED, 2)) + ' LastBall: ' + str(round(BALL_POINTS[-1][0])) + ',' + str(round(BALL_POINTS[-1][1]))
text = font.render(game_info, True, (255,255,255))
author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))
screen.blit(text, (0, SCREEN_HEIGHT+5))
screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5)) pygame.display.update()

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

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

Edit By Hongten

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

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

 #pygame draw

 import pygame
from pygame.locals import *
from sys import exit
from random import * '''
游戏名称:
小球完全弹性碰撞
游戏规则:
1.游戏初始化的时候,有5个不同颜色的小球进行碰撞
2.玩家可以通过在窗口中单击鼠标左键进行增加小球个数
3.玩家可以通过在窗口中单击鼠标右键进行删减小球个数
4.玩家可以通过键盘的方向键:上,右键进行对小球加速
5.玩家可以通过键盘的方向键:下,左键进行对小球减速
6.玩家可以按键盘:f键实现全屏显示
7.玩家可以按键盘:Esc键实现退出全屏操作
8.窗口左下角显示小球个数,右下角显示作者邮箱 v1.1修改如下:
1.增加了背景音乐
2.增加小球的时候,会伴随音乐产生
3.窗口左下角显示小球个数,速度,以及最后一个小球的位置 v1.2修改如下:
1.修改键盘方向键:左,右键为调节音量(0, 10)
2.在状态栏添加音量状态信息:数字和图形显示 '''
__author__ = {'name' : 'Hongten',
'mail' : 'hongtenzone@foxmail.com',
'blog' : 'http://www.cnblogs.com/hongten',
'version' : '1.2'} if not pygame.font: print('Warning, fonts disabled')
if not pygame.mixer: print('Warning, sound disabled') pygame.init()
pygame.display.set_caption('Ball Game') SCREEN_WIDTH = 600
SCREEN_HEIGHT = 500
SPEED = 1
VOLUME = 5
SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)
SCREEN_DEFAULT_COLOR = (255, 255 ,255)
READY = 0 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
screen.fill(SCREEN_DEFAULT_COLOR)
bg = pygame.image.load('data\\image\\bg.jpg').convert()
font = pygame.font.Font('data\\font\\TORK____.ttf', 14)
new_sound = pygame.mixer.Sound('data\\sound\\new.wav')
bg_sound = pygame.mixer.Sound('data\\sound\\bg.ogg')
bg_sound.set_volume(0.1 * VOLUME)
bg_sound.play(-1)
new_sound.set_volume(0.1 * VOLUME) balls = []
BALL_R = 30
BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]
BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]
BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]] VOLUME_POINTS = []
VOLUME_POINTS_START = []
VOLUME_RECT_COLORS = []
for p in range(170, 250, 7):
VOLUME_POINTS.append([SCREEN_WIDTH - p,SCREEN_HEIGHT + 20])
for ps in range(175, 250, 7):
VOLUME_POINTS_START.append([SCREEN_WIDTH - ps, SCREEN_HEIGHT])
VOLUME_RECT_COLORS.append((randint(0, 255), randint(0, 255), randint(0, 255))) print(VOLUME_POINTS[-10])
print(VOLUME_POINTS_START[-10]) for i in range(len(BALL_COLORS)):
screen.fill(SCREEN_DEFAULT_COLOR)
b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)
balls.append(b) while 1:
for event in pygame.event.get():
if event.type == QUIT:
bg_sound.stop()
exit()
elif event.type == KEYDOWN:
if event.key == K_UP:
SPEED += 0.1
elif event.key == K_DOWN:
SPEED -= 0.1
elif event.key == K_LEFT:
if VOLUME > 0:
VOLUME -= 1
elif event.key == K_RIGHT:
if VOLUME <= 9:
VOLUME += 1
elif event.key == K_f:
pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)
elif event.key == 27:
pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
elif event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
for index in range(len(pressed_array)):
if pressed_array[index]:
if index == 0:
new_sound.play(-1)
c_color = (randint(0, 255), randint(0, 255), randint(0, 255))
x, y = (BALL_R+1, BALL_R+1)
c_r = randint(10, 100)
c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]
c = pygame.draw.circle(screen, c_color, (x, y), BALL_R)
BALL_COLORS.append(c_color)
BALL_POINTS.append([x, y])
BALL_VELOCITY.append(c_v)
balls.append(c)
elif index == 2:
if len(balls) > 1:
balls.pop(0)
BALL_COLORS.pop(0)
BALL_POINTS.pop(0)
BALL_VELOCITY.pop(0)
elif event.type == MOUSEBUTTONUP:
new_sound.stop() #print(balls)
for i in range(len(balls)):
screen.blit(bg, (-300, -100))
#screen.fill(SCREEN_DEFAULT_COLOR)
for n in range(len(balls)):
'''
if ((BALL_POINTS[i][0] - BALL_R) > 0 and (BALL_POINTS[i][0] - BALL_R) < BALL_R):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] + BALL_R),int(BALL_POINTS[n][1])), BALL_R)
elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_WIDTH and (BALL_POINTS[i][1] + BALL_R) > SCREEN_WIDTH - BALL_R):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] - BALL_R),int(BALL_POINTS[n][1])), BALL_R)
elif ((BALL_POINTS[i][1] - BALL_R) > 0 and (BALL_POINTS[i][1] - BALL_R) < BALL_R):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] + BALL_R)), BALL_R)
elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_HEIGHT and (BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT - BALL_R):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] - BALL_R)), BALL_R)
'''
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)
if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) > SCREEN_WIDTH))):
BALL_VELOCITY[i][0] = -1 * BALL_VELOCITY[i][0]
if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT))):
BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1] for j in range(len(balls)):
for k in range(len(balls)):
b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2
b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2
b_r =(BALL_R*2)**2
if (round((b_x + b_y), 2) <= round(b_r, 2)):
temp_x = BALL_VELOCITY[j][0]
temp_y = BALL_VELOCITY[j][1]
BALL_VELOCITY[j][0] = BALL_VELOCITY[k][0]
BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]
BALL_VELOCITY[k][0] = temp_x
BALL_VELOCITY[k][1] = temp_y BALL_POINTS[j][0] += round(SPEED, 1) * BALL_VELOCITY[j][0]
BALL_POINTS[j][1] += round(SPEED, 1) * BALL_VELOCITY[j][1] pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))
bg_sound.set_volume(0.1 * VOLUME)
new_sound.set_volume(0.1 * VOLUME)
pygame.draw.rect(screen,
(255, 255, 255),
Rect((VOLUME_POINTS_START[-1][0],
VOLUME_POINTS_START[-1][1]),
(VOLUME_POINTS[-10][0] - VOLUME_POINTS_START[-1][0],
20)))
for v in range(VOLUME+1):
if v > 0:
pygame.draw.rect(screen,
VOLUME_RECT_COLORS[v],
Rect((VOLUME_POINTS_START[-v][0],
VOLUME_POINTS_START[-v][1]),
(VOLUME_POINTS[-v][0] - VOLUME_POINTS_START[-v][0],
20))) game_info = 'Balls: ' + str(len(balls)) + ' Speed: ' + str(round(SPEED, 2)) + ' LastBall: ' + str(round(BALL_POINTS[-1][0])) + ',' + str(round(BALL_POINTS[-1][1]))
text = font.render(game_info, True, (255,255,255))
author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))
volume_text = font.render('Volume: ' + str(VOLUME), True, (255, 255, 255))
screen.blit(text, (0, SCREEN_HEIGHT+5))
screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5))
screen.blit(volume_text, (SCREEN_WIDTH - 310, SCREEN_HEIGHT+5))
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:

 #pygame draw

 import pygame
from pygame.locals import *
from sys import exit
from random import * '''
Game Name:
Ball Perfectly Elastic Collision(BPEC)
Rules or Description:
1.There are five balls whth different color have elastic collision
after the game loaded.
2.The player can click the window with the LEFT mouse button
and create the new ball,which with the different color,but
sometimes maybe like other balls.
3.The player can click teh window with RIGHT mouse button
and minus a few balls.
4.You can change all ball speed by pressing the UP and DOWN
the keyboard direction key.
5.Also you can change the background music by pressing the LEFT
and RIGHT the keyboard direction key(volume:0-10).
6.Maybe you want to full screen view,By pressing the F key
and ESC key switch.
7.Ball number,the speed,the volume,author E-mail is written
in the status bar at the bottom.
'''
__version__ = '1.3'
__author__ = {'name' : 'Hongten',
'mail' : 'hongtenzone@foxmail.com',
'blog' : 'http://www.cnblogs.com/hongten',
'version' : __version__} if not pygame.font: print('Warning, fonts disabled')
if not pygame.mixer: print('Warning, sound disabled') pygame.init()
pygame.display.set_caption('Ball Game') SCREEN_WIDTH = 600
SCREEN_HEIGHT = 500
SPEED = 1
VOLUME = 5
SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)
SCREEN_DEFAULT_COLOR = (255, 255 ,255)
READY = 0 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
screen.fill(SCREEN_DEFAULT_COLOR)
bg = pygame.image.load('data\\image\\bg.jpg').convert()
font = pygame.font.Font('data\\font\\TORK____.ttf', 14)
new_sound = pygame.mixer.Sound('data\\sound\\new.wav')
bg_sound = pygame.mixer.Sound('data\\sound\\bg.ogg')
bg_sound.set_volume(0.1 * VOLUME)
bg_sound.play(-1)
new_sound.set_volume(0.1 * VOLUME) balls = []
BALL_R = 30
BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]
BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]
BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]] VOLUME_POINTS = []
VOLUME_POINTS_START = []
VOLUME_RECT_COLORS = []
for p in range(170, 250, 7):
VOLUME_POINTS.append([SCREEN_WIDTH - p,SCREEN_HEIGHT + 20])
for ps in range(175, 250, 7):
VOLUME_POINTS_START.append([SCREEN_WIDTH - ps, SCREEN_HEIGHT])
VOLUME_RECT_COLORS.append((randint(0, 255), randint(0, 255), randint(0, 255))) for i in range(len(BALL_COLORS)):
screen.fill(SCREEN_DEFAULT_COLOR)
b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)
balls.append(b) while 1:
for event in pygame.event.get():
if event.type == QUIT:
bg_sound.stop()
exit()
elif event.type == KEYDOWN:
if event.key == K_UP:
SPEED += 0.1
elif event.key == K_DOWN:
SPEED -= 0.1
elif event.key == K_LEFT:
if VOLUME > 0:
VOLUME -= 1
elif event.key == K_RIGHT:
if VOLUME <= 9:
VOLUME += 1
elif event.key == K_f:
pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)
elif event.key == 27:
pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
elif event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
for index in range(len(pressed_array)):
if pressed_array[index]:
if index == 0:
new_sound.play(-1)
c_color = (randint(0, 255), randint(0, 255), randint(0, 255))
x, y = (BALL_R+1, BALL_R+1)
c_r = randint(10, 100)
c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]
c = pygame.draw.circle(screen, c_color, (x, y), BALL_R)
BALL_COLORS.append(c_color)
BALL_POINTS.append([x, y])
BALL_VELOCITY.append(c_v)
balls.append(c)
elif index == 2:
if len(balls) > 1:
balls.pop(0)
BALL_COLORS.pop(0)
BALL_POINTS.pop(0)
BALL_VELOCITY.pop(0)
elif event.type == MOUSEBUTTONUP:
new_sound.stop() #print(balls)
for i in range(len(balls)):
screen.blit(bg, (-300, -100))
#screen.fill(SCREEN_DEFAULT_COLOR)
for n in range(len(balls)):
pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)
if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) > SCREEN_WIDTH))):
BALL_VELOCITY[i][0] = -1 * BALL_VELOCITY[i][0]
if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT))):
BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1] for j in range(len(balls)):
for k in range(len(balls)):
b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2
b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2
b_r =(BALL_R*2)**2
if (round((b_x + b_y), 2) <= round(b_r, 2)):
temp_x = BALL_VELOCITY[j][0]
temp_y = BALL_VELOCITY[j][1]
BALL_VELOCITY[j][0] = BALL_VELOCITY[k][0]
BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]
BALL_VELOCITY[k][0] = temp_x
BALL_VELOCITY[k][1] = temp_y BALL_POINTS[j][0] += round(SPEED, 1) * BALL_VELOCITY[j][0]
BALL_POINTS[j][1] += round(SPEED, 1) * BALL_VELOCITY[j][1] pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))
bg_sound.set_volume(0.1 * VOLUME)
new_sound.set_volume(0.1 * VOLUME)
pygame.draw.rect(screen,
(255, 255, 255),
Rect((VOLUME_POINTS_START[-1][0],
VOLUME_POINTS_START[-1][1]),
(VOLUME_POINTS[-10][0] - VOLUME_POINTS_START[-1][0],
20)))
for v in range(VOLUME+1):
if v > 0:
pygame.draw.rect(screen,
VOLUME_RECT_COLORS[v],
Rect((VOLUME_POINTS_START[-v][0],
VOLUME_POINTS_START[-v][1]),
(VOLUME_POINTS[-v][0] - VOLUME_POINTS_START[-v][0],
20))) game_info = 'Balls: ' + str(len(balls)) + ' Speed: ' + str(round(SPEED, 2)) + ' LastBall: ' + str(round(BALL_POINTS[-1][0])) + ',' + str(round(BALL_POINTS[-1][1]))
text = font.render(game_info, True, (255,255,255))
author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))
volume_text = font.render('Volume: ' + str(VOLUME), True, (255, 255, 255))
screen.blit(text, (0, SCREEN_HEIGHT+5))
screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5))
screen.blit(volume_text, (SCREEN_WIDTH - 310, SCREEN_HEIGHT+5))
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

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

pygame系列_小球完全弹性碰撞游戏_源码下载的更多相关文章

  1. pygame系列_小球完全弹性碰撞游戏

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

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

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

  3. openlayers4 入门开发系列结合 echarts4 实现散点图(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  4. leaflet-webpack 入门开发系列三地图分屏对比(附源码下载)

    前言 leaflet-webpack 入门开发系列环境知识点了解: node 安装包下载webpack 打包管理工具需要依赖 node 环境,所以 node 安装包必须安装,上面链接是官网下载地址 w ...

  5. cesium 入门开发系列矢量瓦片加载展示(附源码下载)

    前言 cesium 入门开发系列环境知识点了解:cesium api文档介绍,详细介绍 cesium 每个类的函数以及属性等等cesium 在线例子 内容概览 cesium 实现矢量瓦片加载效果 源代 ...

  6. [Java] SSH框架笔记_框架分析+环境搭建+实例源码下载

    首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...

  7. openlayers4 入门开发系列之聚合图篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  8. openlayers4 入门开发系列之迁徙图篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  9. openlayers4 入门开发系列之地图工具栏篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

随机推荐

  1. html总结(一)

    一.了解 HTML文档也被称为网页,包含html标签和纯文本,浏览器读取HTML文档,以网页的形式显示出来,而标签决定了所显示网页的格式. 二.要点 常用的HTML文档声明 HTML5 <!DO ...

  2. [BZOJ 2299][HAOI 2011]向量 题解(裴蜀定理)

    [BZOJ 2299][HAOI 2011]向量 Description 给你一对数a,b,你可以任意使用(a,b), (a,-b), (-a,b), (-a,-b), (b,a), (b,-a), ...

  3. [转]Linux联网问题

    一.Kali联网问题 首先ifconfig,可以看到没有正在工作的网卡,只有localhost 然后ifconfig -a,可以看到eth0这块网卡并没有离家出走,只是罢工了而已 打开/etc/net ...

  4. 分享一个C#创建Barcode的DLL

    用于工作需要产生Barcode,随手从网上找了一个DLL(原文地址忘了) http://files.cnblogs.com/panchunting/barcode_bin.zip 使用非常简单,只需添 ...

  5. RabbitMQ Queue一些常见模式

    懒队列:lazy Queue,即用到的时候才会加载,3.6.0及之后新添加的.当新添加数据后,不会将其放入到内存中,而是将其放入到磁盘中. 普通队列:1).in-memory,数据直接放入到内存中. ...

  6. Python学习四|变量、对象、引用的介绍

    变量 变量创建:一个变量也就是变量名,就像a,当代码第一次赋值时就创建了它.之后的赋值将会改变已创建的变量名的值,从技术上讲,Python在代码运行之前先检测变量名,可以当成是最初的赋值创建了变量. ...

  7. vue.js 解决空格报错!!!

    当我们初入vue.js的时候.使用cli脚手架快速创建项目的时候: 如果语法格式错误(这里主要指的是:空格多少引起的问题)!! 找到  webpack.base.config.js文件注释掉下面的东西 ...

  8. Online DDL工具的安装与使用

    最近经常在线上经常遇到有性能问题的SQL,有些表没有合理添加索引,有些表添加的索引不合理,各种各样的问题,导致SQL的执行效率不高.这时DBA们不得不重构SQL,使其达到最好的性能,这时我们往往要在线 ...

  9. 利用pt-table-checksum校验数据一致性

    相信很多人的线上都搭建了MySQL主从这样的框架,很多人只监控MySQL的从服务器Slave_IO和Slave_SQL这两个线程是否为YES,还有 Seconds_Behind_Master延迟大不大 ...

  10. MySQL 数据库性能优化之SQL优化【转】

    优化目标 减少 IO 次数IO永远是数据库最容易瓶颈的地方,这是由数据库的职责所决定的,大部分数据库操作中超过90%的时间都是 IO 操作所占用的,减少 IO 次数是 SQL 优化中需要第一优先考虑, ...