簡體   English   中英

變色彈跳球

[英]Colour Changing Bouncing Ball

我正在做一個在 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) )的元組。 用隨機位置( 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()

在 pygame中的Use vector2 中可以找到更復雜的球類方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM