变色弹跳球 [英] Colour Changing Bouncing Ball
问题描述
我正在研究一个在pygame中制作动画的项目.分配是这样的:
在屏幕上添加另外2个球(总共3个),这些球的颜色,大小和方向都将不同.他们都会不断反弹.
提示:您将需要为每个球创建所有新变量
即x2,y2,dx2,dy2
并且您将需要单独检查每个球是否撞到墙壁,并分别更新每个球的垂直和水平位置.第4级挑战:当球碰到墙壁并且颜色改变时,每种颜色都会随机改变.
到目前为止,这是我的代码,我在Mac上使用的是Python 3.7.到目前为止,该代码使第一个球弹起并保持相同的颜色.我不知道如何再制造两个球,并让它们每次撞墙时都会改变颜色.如果有人可以帮助我,我将无法解决这个问题.
import pygame导入系统pygame.init()屏幕尺寸=(800,600)屏幕= pygame.display.set_mode(screensize,0)pygame.display.set_caption(动画测试")白色=(255,255,255)绿色=(0,255,0)蓝色=(0,0,255)红色=(255,0,0)screen.fill(白色)pygame.display.update()x = 100y = 200dx = 2dy = 2去=真边走边:对于pygame.event.get()中的事件:如果event.type == pygame.QUIT:去=错误screen.fill(白色)x = x + dxy = y + dy颜色=蓝色如果(x> = 775):dx = -dxelif(x> = 775):dx = -dx颜色=红色
创建一个可以创建随机球的函数( createball
).球是x和y位置,运动矢量dx,dy和颜色((x,y,dx,dy,颜色)
)的元组.使用随机位置(
import pygame导入系统随机导入pygame.init()屏幕尺寸=(800,600)屏幕= pygame.display.set_mode(screensize,0)pygame.display.set_caption(动画测试")时钟= pygame.time.Clock()白色=(255,255,255)绿色=(0,255,0)蓝色=(0,0,255)红色=(255,0,0)半径= 25color_list = [绿色,蓝色,红色]def createball():x = random.randint(半径,screensize [0]-半径)y = random.randint(半径,screensize [1]-半径)颜色= random.choice(color_list)返回x,y,2,2,colordef moveball(x,y,dx,dy,颜色):x,y = x + dx,y + dy如果不是,半径<x <screensize [0]-半径:dx = -dx颜色= random.choice(color_list)如果不是,半径<&screensize [1] -radius:dy = -dy颜色= random.choice(color_list)返回x,y,dx,dy,颜色x,y,dx,dy,颜色= createball()x2,y2,dx2,dy2,color2 = createball()x3,y3,dx3,dy3,color3 = createball()去=真边走边:clock.tick(60)对于pygame.event.get()中的事件:如果event.type == pygame.QUIT:去=错误x,y,dx,dy,颜色= moveball(x,y,dx,dy,颜色)x2,y2,dx2,dy2,color2 = moveball(x2,y2,dx2,dy2,color2)x3,y3,dx3,dy3,color3 = moveball(x3,y3,dx3,dy3,color3)screen.fill(白色)pygame.draw.circle(屏幕,颜色,(x,y),半径)pygame.draw.circle(屏幕,color2,(x2,y2),半径)pygame.draw.circle(屏幕,color3,(x3,y3),半径)pygame.display.flip()
可以在在pygame中使用vector2找到更复杂的球类方法.
I'm working on a project to make an animation in pygame. The assignment is this:
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.
Here is my code so far, I am using Python 3.7 on a Mac. The code so far has the first ball bouncing and staying the same color. I can't figure out how to make two more balls and have them change color each time they hit a wall. If someone could please help I literally cannot figure this out.
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
Create a function (createball
) which can crate a random ball. A ball is a tuple of x and y position, the movement vector dx, dy and the color ((x, y, dx, dy, color)
). Create a certain number of ball (max_balls
) with random positions (random.randint(a, b)
) and random colors (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
Crate the 3 balls and store them to the variables as suggested in the assignment (x2, y2, dx2, dy2
):
x, y, dx, dy, color = createball()
x2, y2, dx2, dy2, color2 = createball()
x3, y3, dx3, dy3, color3 = createball()
Create a function (moveball
) which can move a ball, change there direction when the hit the bounds and change the color. Move the balls in the application loop:
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)
See the example:
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()
A bit more sophisticated approach with a ball class can be found at Use vector2 in pygame.
这篇关于变色弹跳球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!