- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个在 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/
我需要我的 Sprite 从一种颜色过渡到另一种颜色,等等...比如蓝色,然后绿色,然后紫色,但我找不到任何好的 Action ,我想知道,我应该使用动画吗?或者是否有为此采取的联合行动? 最佳答案
我必须做某事。像这样,我无法弄清楚问题是什么。这是我的代码: size(200,200); background(255); float w = 200 ; float radius = 20; wh
我需要更改 Lollipop (API 21) 应用程序中 ProgressBar 元素的颜色。更清楚地说,我的意思是,例如,Youtube 移动应用程序循环进度条。例如,我需要将进度条的颜色设为红色
如何让网站像下面的例子那样改变 Activity 栏的颜色: 最佳答案 在您的网页中添加: 阅读更多 here . 关于android - 如何在 Material Design 中让 Activi
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是一名优秀的程序员,十分优秀!