程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Make a simple ball ball battle with Python

編輯:Python

Hello everyone , I'm Charlie . Today, I'll teach you to make a simplified version of the ball battle \

Don't say much , Code up

# -*- coding: utf-8 -*-
# @Time : 2018/7/30 16:19
# @Author : G.Hope
# @Email : [email protected]
# @File : Eat ball .py
# @Software: PyCharm
import pygame
import random
import math
# Generate random colors
def random_color():
return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
# Determine if there is a collision , And make the big ball eat the small ball ( The ball disappears , The big ball gets bigger )
def eat(ball1, ball2):
x1, y1 = ball1['pos']
x2, y2 = ball2['pos']
x_distance = x1 - x2
y_distance = y1 - y2
distance = math.sqrt(x_distance ** 2 + y_distance ** 2)
if distance < ball1['r'] + ball2['r']:
if ball1['r'] > ball2['r']:
ball1['r'] = ball2['r'] + ball1['r']
all_balls.remove(ball2)
else:
ball2['r'] = ball2['r'] + ball1['r']
all_balls.remove(ball1)
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((255, 255, 255))
pygame.display.flip()
# all_balls Save multiple balls in
# Keep each ball : radius 、 The coordinates of the center of the circle 、 Color 、x Speed 、y Speed
all_balls = [
{
'r': random.randint(10, 20),
'pos': (100, 100),
'color': random_color(),
'x_speed': random.randint(-1, 1),
'y_speed': random.randint(-1, 1)
},
{
'r': random.randint(10, 20),
'pos': (200, 200),
'color': random_color(),
'x_speed': random.randint(-1, 1),
'y_speed': random.randint(-1, 1)
},
{
'r': random.randint(10, 20),
'pos': (300, 300),
'color': random_color(),
'x_speed': random.randint(-1, 1),
'y_speed': random.randint(-1, 1)
}
]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
# Click the mouse to create a ball
ball = {
'r': random.randint(10, 20), # The random size
'pos': event.pos, # Set the center of the circle to the coordinates of the current mouse click
'color': random_color(),
'x_speed': random.randint(-1, 1), # Random direction
'y_speed': random.randint(-1, 1)
}
# Save ball
all_balls.append(ball)
# Refresh the interface
screen.fill((255, 255, 255))
for ball_dict in all_balls:
# Take out the principle of x,y Coordinates and their speed
x, y = ball_dict['pos']
x_speed = ball_dict['x_speed']
y_speed = ball_dict['y_speed']
if x >= 800: # Set the boundary and change the direction of movement
x = 800
x_speed = -1
ball_dict['x_speed'] = x_speed
if x < 0:
x = 0
x_speed = 1
ball_dict['x_speed'] = x_speed
if y >= 600:
y = 600
y_speed = -1
ball_dict['y_speed'] = y_speed
if y < 0:
y = 0
y_speed = 1
ball_dict['y_speed'] = y_speed
x += x_speed
y += y_speed
pygame.draw.circle(screen, ball_dict['color'], (x, y), ball_dict['r'])
# Update the coordinates corresponding to the ball
ball_dict['pos'] = x, y
pygame.display.update()
# Collision
for ball1 in all_balls:
for ball2 in all_balls:
if ball1 == ball2:
continue
eat(ball1, ball2)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved