python - 变色弹跳球
问题描述
我正在做一个在 pygame 中制作动画的项目。任务是这样的:
在屏幕上添加另外 2 个球(总共 3 个),它们都是不同的颜色、不同的大小并从不同的方向开始。它们都会不断地反弹。
提示:您需要为每个球创建所有新变量,
即 x2、y2、dx2、dy2
,您需要单独检查每个球是否撞到墙壁,并分别更新每个球的垂直和水平位置。4级挑战:当球撞到墙上并且颜色发生变化时,使每种颜色随机变化。
到目前为止,这是我的代码,我在 Mac 上使用 Python 3.7。到目前为止的代码有第一个球弹跳并保持相同的颜色。我不知道如何再制作两个球并让它们在每次撞到墙上时改变颜色。如果有人可以请帮助我真的无法弄清楚这一点。
import pygame
import sys
pygame.init()
screensize = (800,600)
screen = pygame.display.set_mode(screensize,0)
pygame.display.set_caption("Animation Test")
WHITE = (255,255,255)
GREEN = (0,255,0)
BLUE = (0,0,255)
RED = (255,0,0)
screen.fill(WHITE)
pygame.display.update()
x = 100
y = 200
dx = 2
dy = 2
go = True
while go:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
go = False
screen.fill(WHITE)
x = x + dx
y = y + dy
Colour = BLUE
if (x>=775):
dx = -dx
elif (x>=775):
dx = -dx
Colour = RED
解决方案
createball
创建一个可以创建随机球的函数 ( )。球是 x 和 y 位置、运动向量 dx、dy 和颜色 ( (x, y, dx, dy, color)
) 的元组。max_balls
创建具有随机位置 ( random.randint(a, b)
) 和随机颜色 ( )的一定数量的球 ( random.choice(seq)
):
radius = 25
color_list = [GREEN, BLUE, RED]
def createball():
x = random.randint(radius, screensize[0]-radius)
y = random.randint(radius, screensize[1]-radius)
color = random.choice(color_list)
return x, y, 2, 2, color
按照赋值 ( x2, y2, dx2, dy2
) 中的建议将 3 个球装箱并存储到变量中:
x, y, dx, dy, color = createball()
x2, y2, dx2, dy2, color2 = createball()
x3, y3, dx3, dy3, color3 = createball()
创建一个函数 ( moveball
),它可以移动一个球,在击球时改变方向并改变颜色。在应用程序循环中移动球:
def moveball(x, y, dx, dy, color):
x, y = x + dx, y + dy
if not radius < x < screensize[0]-radius:
dx = -dx
color = random.choice(color_list)
if not radius < y < screensize[1]-radius:
dy = -dy
color = random.choice(color_list)
return x, y, dx, dy, color
while run:
# [...]
x, y, dx, dy, color = moveball(x, y, dx, dy, color)
x2, y2, dx2, dy2, color2 = moveball(x2, y2, dx2, dy2, color2)
x3, y3, dx3, dy3, color3 = moveball(x3, y3, dx3, dy3, color3)
请参阅示例:
import pygame
import sys
import random
pygame.init()
screensize = (800,600)
screen = pygame.display.set_mode(screensize,0)
pygame.display.set_caption("Animation Test")
clock = pygame.time.Clock()
WHITE = (255,255,255)
GREEN = (0,255,0)
BLUE = (0,0,255)
RED = (255,0,0)
radius = 25
color_list = [GREEN, BLUE, RED]
def createball():
x = random.randint(radius, screensize[0]-radius)
y = random.randint(radius, screensize[1]-radius)
color = random.choice(color_list)
return x, y, 2, 2, color
def moveball(x, y, dx, dy, color):
x, y = x + dx, y + dy
if not radius < x < screensize[0]-radius:
dx = -dx
color = random.choice(color_list)
if not radius < y < screensize[1]-radius:
dy = -dy
color = random.choice(color_list)
return x, y, dx, dy, color
x, y, dx, dy, color = createball()
x2, y2, dx2, dy2, color2 = createball()
x3, y3, dx3, dy3, color3 = createball()
go = True
while go:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
go = False
x, y, dx, dy, color = moveball(x, y, dx, dy, color)
x2, y2, dx2, dy2, color2 = moveball(x2, y2, dx2, dy2, color2)
x3, y3, dx3, dy3, color3 = moveball(x3, y3, dx3, dy3, color3)
screen.fill(WHITE)
pygame.draw.circle(screen, color, (x, y), radius)
pygame.draw.circle(screen, color2, (x2, y2), radius)
pygame.draw.circle(screen, color3, (x3, y3), radius)
pygame.display.flip()
可以在 pygame 中的 Use vector2 中找到更复杂的球类方法。
推荐阅读
- python - 在第一次出现字符之前插入字符串
- google-apps-script - 如何使用 Google Scripts 访问不同的 Google Drive
- cypress - cypress 中的条件语句
- node.js - 如何在 Node.js 中读取文件内容并将数据转换为 JSON?
- django - 如何使用来自 S3 的媒体和静态文件在 AWS EC2 上部署 django
- c# - 在 ASP.NET MVC 中将 Blob 下载为文件
- jvm - SonarQube does not start in Linux (localhost)
- bash - Can I edit an image file from command line?
- c - 我如何在这里错误地使用 malloc?
- dask - 如何让 xarray.interp() 并行工作?