gpt4 book ai didi

python - 变色弹跳球

转载 作者:行者123 更新时间:2023-12-01 06:22:04 24 4
gpt4 key购买 nike

我正在开发一个在 pygame 中制作动画的项目。作业是这样的:

Add 2 other balls to the screen (3 in total) that will all be different colors, different sizes and start in different directions. They will all bounce around continuously.

Hint: you will need to create all new variables for each ball
i.e. x2,y2, dx2, dy2
and you will need to check each ball individually for hitting a wall and update the vertical and horizontal position of each ball individually.

Level 4 Challenge: when the balls hit the wall and the colors change to make each color randomly change.

这是到目前为止我的代码,我在 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)) 的元组。创建一定数量的具有随机位置 ( random.randint(a, b) ) 和随机颜色 ( random.choice(seq) ) 的球 (max_balls):

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

按照作业中的建议创建 3 个球并将它们存储到变量中(x2、y2、dx2、dy2):

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()
<小时/>

可以在Use vector2 in pygame找到更复杂的球类方法。 .

关于python - 变色弹跳球,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60312365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com