也许这就是您正在寻找的。从这里下载图像。
import pygame
import sys
WIDTH, HEIGHT = 600,600
ROWS, COLS = 8,8
SQUARE_SIZE = WIDTH//COLS
WIDTH, HEIGHT = SQUARE_SIZE*COLS, SQUARE_SIZE*ROWS
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
CLOCK = pygame.time.Clock()
class Ball:
GRAVITY = 0.5
def __init__(self, row, col):
self.row = row
self.col = col
self.target_x = (SQUARE_SIZE*col)+SQUARE_SIZE/2
self.target_y = (SQUARE_SIZE*row)+SQUARE_SIZE/2
self.x = self.target_x
self.y = 0
self.vel = 10
self.img = pygame.transform.scale(pygame.image.load("ball2.png"), (SQUARE_SIZE-20, SQUARE_SIZE-20))
self.landed = False
def draw(self):
self.update()
pygame.draw.circle(WIN, (0,255,0), (self.x, self.y), 15)
def update(self):
if not self.landed:
self.vel += self.GRAVITY
self.y += self.vel
if self.y > self.target_y:
self.vel = -(self.vel*0.70)
if (abs(self.vel)<1):
self.landed = True
self.y += self.vel
def draw_board(ball_list):
WIN.fill((0,0,0))
for row in range(ROWS):
for col in range(row%2, COLS, 2):
pygame.draw.rect(WIN, (255,0,0), (row*SQUARE_SIZE, col*SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))
def create_balls(ball_list):
x = []
for row in range(ROWS):
for col in range(COLS):
if ball_list[row][col] ==1: x.append(Ball(row, col))
return x
def main():
ball_list = [
[0,0,0,1,0,0,0,1],
[1,0,1,0,0,0,0,1],
[0,0,0,1,0,1,0,0],
[0,0,1,1,0,0,0,1],
[1,0,0,0,0,1,0,0],
[0,0,0,0,1,0,0,1],
[0,1,0,0,0,1,0,0],
[0,0,1,0,0,0,1,0],
]
balls = create_balls(ball_list)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
draw_board(ball_list)
for ball in balls:
ball.draw()
pygame.display.update()
CLOCK.tick(60)
main()