单击它如何使圆圈绿色

发布于 2025-01-28 10:58:42 字数 724 浏览 3 评论 0原文

我正在尝试制作一个具有一千个小圆圈的窗户,当我单击一个圆圈时,圆圈将变绿。我是Pygame的新手。

import pygame
import random as r
pygame.init()

screen = pygame.display.set_mode([1700, 900])
pygame.display.set_caption('So many buttons')
from pygame import mixer
mixer.init()
mixer.music.load('looperman-l-2592210-0277509-vein-95bpm-blend-beats.ogg')
mixer.music.play(-1)
run = True
num = 1

def draw_circle(self):
    x = r.randint(1, 1700)
    y = r.randint(1, 900)
    pygame.draw.circle(screen, (255, 0, 0),[x, y], 10, 0)
    pygame.display.update()

while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    if num <= 1000:
        draw_circle()
        num += 1
pygame.quit()

I'm trying to make a Pygame window that has one thousand small circles and when I click one that circle will turn green. I am new to Pygame.

import pygame
import random as r
pygame.init()

screen = pygame.display.set_mode([1700, 900])
pygame.display.set_caption('So many buttons')
from pygame import mixer
mixer.init()
mixer.music.load('looperman-l-2592210-0277509-vein-95bpm-blend-beats.ogg')
mixer.music.play(-1)
run = True
num = 1

def draw_circle(self):
    x = r.randint(1, 1700)
    y = r.randint(1, 900)
    pygame.draw.circle(screen, (255, 0, 0),[x, y], 10, 0)
    pygame.display.update()

while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    if num <= 1000:
        draw_circle()
        num += 1
pygame.quit()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无戏配角 2025-02-04 10:58:42

创建代表一个圆的类。该类应具有可以绘制圆圈的方法,检测点是否在圆内的方法以及可以更改颜色的方法:

class Circle:
    def __init__(self):
        self.x = r.randint(1, 1700)
        self.y = r.randint(1, 1700)
        self.color = (255, 0, 0)
        self.r = 10
        self.clicked = False
    def is_point_in(self, p):
        dx = p[0] - self.x
        dy = p[1] - self.y
        return math.hypot(dx, dy) <= self.r * self.r
    def set_color(self, color):
        self.color = color
    def draw(self):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.r, 0)

在应用程序循环之前创建圆圈并将其放在列表中:

list_of_circles = []
for i in range(10):
    list_of_circles.append(Circle())

当单击鼠标时,检测鼠标是否为圆圈并更改颜色:

for event in pygame.event.get():
    # [...]
    if event.type == pygame.MOUSEBUTTONDOWN:
        for circle in list_of_circles:
            if circle.is_point_in(event.pos):
                circle.set_color((0, 255, 0))

在应用程序循环中绘制圆圈:

while run:
    # [...]

    for circle in list_of_circles:
        circle.draw()

请参见ALOS 我如何在pygame中检测到碰撞? pygame鼠标单击检测


最小示例:

import pygame
import random as r
import math

class Circle:
    def __init__(self):
        self.x = r.randint(1, 1700)
        self.y = r.randint(1, 1700)
        self.color = (255, 0, 0)
        self.r = 10
        self.clicked = False
    def is_point_in(self, p):
        dx = p[0] - self.x
        dy = p[1] - self.y
        return math.hypot(dx, dy) <= self.r * self.r
    def set_color(self, color):
        self.color = color
    def draw(self):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.r, 0)

pygame.init()
screen = pygame.display.set_mode([1700, 900])

list_of_circles = []
for i in range(10):
    list_of_circles.append(Circle())

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            for circle in list_of_circles:
                if circle.is_point_in(event.pos):
                    circle.set_color((0, 255, 0))

    screen.fill(0)
    for circle in list_of_circles:
        circle.draw()
    pygame.display.update()

pygame.quit()

Create a class that represents a circle. The class should have a method that can draw the circle, method that detects if a point is inside the circle and a method that can change the color:

class Circle:
    def __init__(self):
        self.x = r.randint(1, 1700)
        self.y = r.randint(1, 1700)
        self.color = (255, 0, 0)
        self.r = 10
        self.clicked = False
    def is_point_in(self, p):
        dx = p[0] - self.x
        dy = p[1] - self.y
        return math.hypot(dx, dy) <= self.r * self.r
    def set_color(self, color):
        self.color = color
    def draw(self):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.r, 0)

Create circles before the application loop and put them in a list:

list_of_circles = []
for i in range(10):
    list_of_circles.append(Circle())

When the mouse is clicked, detect if the mouse is in a circle and change color:

for event in pygame.event.get():
    # [...]
    if event.type == pygame.MOUSEBUTTONDOWN:
        for circle in list_of_circles:
            if circle.is_point_in(event.pos):
                circle.set_color((0, 255, 0))

Draw the circle in a loop in the application loop:

while run:
    # [...]

    for circle in list_of_circles:
        circle.draw()

See alos How do I detect collision in pygame? and Pygame mouse clicking detection.


Minimal example:

import pygame
import random as r
import math

class Circle:
    def __init__(self):
        self.x = r.randint(1, 1700)
        self.y = r.randint(1, 1700)
        self.color = (255, 0, 0)
        self.r = 10
        self.clicked = False
    def is_point_in(self, p):
        dx = p[0] - self.x
        dy = p[1] - self.y
        return math.hypot(dx, dy) <= self.r * self.r
    def set_color(self, color):
        self.color = color
    def draw(self):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.r, 0)

pygame.init()
screen = pygame.display.set_mode([1700, 900])

list_of_circles = []
for i in range(10):
    list_of_circles.append(Circle())

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            for circle in list_of_circles:
                if circle.is_point_in(event.pos):
                    circle.set_color((0, 255, 0))

    screen.fill(0)
    for circle in list_of_circles:
        circle.draw()
    pygame.display.update()

pygame.quit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文