python创建两个圆对象,在python中同时生成同一对象的多个实例

Hey I'm a beginner programmer who is starting with python and am starting out by making a game in pygames.

The game basically spawns in circles at random positions, and when clicked give you points.

Recently i've hit a roadblock when I want to spawn in multiple instances of the same object(in this case circles) at the same time.

I've tried stuff like sleep() and some other code related to counters, but it always results in the next circle spawned overriding the previous one(i.e the program spawns in circle 1, but when circle 2 comes in, circle 1 disappears).

Anyone know a solution to this? Would really appreciate your help!

import pygame

import random

import time

pygame.init()

window = pygame.display.set_mode((800,600))

class circle():

def __init__(self, color, x, y, radius, width,):

self.color = color

self.x = x

self.y = y

self.radius = radius

self.width = width

def draw(self, win, outline=None):

pygame.draw.circle(win, self.color, (self.x, self.y, self.radius, self.width), 0)

run=True

while run:

window.fill((0, 0, 0))

pygame.draw.circle(window, (255, 255, 255), (random.randint(0, 800),random.randint(0, 600)), 20, 20)

time.sleep(1)

pygame.display.update()

for event in pygame.event.get():

if event.type == pygame.QUIT:

run=False

pygame.quit()

quit()

解决方案

The previous drawn circle disappear, because the display is cleared in every frame by

window.fill((0, 0, 0))

Add a list for the circle objects:

circles = []

Use pygame.time.Clock / tick() to control the frames per second. tick() returns the number of milliseconds since the previous call:

clock = pygame.time.Clock()

FPS = 60

run=True

while run:

delta_ms = clock.tick()

# [...]

Count the past time (current_time += delta_ms) and create new Circle object when one second was elapsed and set the next time threshold (next_circle_time = current_time + 1000). Add the circle to the list.

current_time = 0

next_circle_time = 0

run=True

while run:

delta_ms = clock.tick()

current_time += delta_ms

if current_time > next_circle_time:

next_circle_time = current_time + 1000 # 1000 milliseconds (1 second)

new_circle = Circle((255, 255, 255), random.randint(0, 800), random.randint(0, 600), 20, 20)

circles.append(new_circle)

Draw the circles in a loop:

for c in circles:

c.draw(window)

See the example:

1X0kk.gif

import pygame

import random

import time

pygame.init()

window = pygame.display.set_mode((800,600))

class Circle():

def __init__(self, color, x, y, radius, width):

self.color = color

self.x = x

self.y = y

self.radius = radius

self.width = width

def draw(self, win, outline=None):

pygame.draw.circle(win, self.color, (self.x, self.y), self.radius, self.width)

circles = []

clock = pygame.time.Clock()

FPS = 60

current_time = 0

next_circle_time = 0

run=True

while run:

delta_ms = clock.tick()

current_time += delta_ms

if current_time > next_circle_time:

next_circle_time = current_time + 1000 # 1000 milliseconds (1 second)

r = 20

new_circle = Circle((255, 255, 255), random.randint(r, width-r), random.randint(r, height-r), r, r)

circles.append(new_circle)

window.fill((0, 0, 0))

for c in circles:

c.draw(window)

pygame.display.update()

for event in pygame.event.get():

if event.type == pygame.QUIT:

run=False

pygame.quit()

quit()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值