Reputation: 309
When I run this program I want the game to spawn one larger movable dot and hundreds of smaller non-movable dots at random locations. However, when I run the program the smaller dots keep despawning and respawning. I think it has something to do with the pygame.display.update() function but I am not entirely sure. How do I fix this problem?
from pygame import *
import random as rd
p_1_x = 200
p_1_y = 200
p_1_change_x = 0
init()
screen = display.set_mode((800, 600))
def p_1(x, y):
player_1 = draw.circle(screen, (0, 0, 0), (x, y), 15)
def drawwing():
for i in range(0, 250):
x = rd.randint(100, 700)
y = rd.randint(100, 500)
dot = draw.circle(screen, (55, 255, 155), (x, y), 5)
while True:
for events in event.get():
if events.type == QUIT:
quit()
if events.type == KEYDOWN:
if events.key == K_RIGHT:
p_1_change_x = 1
if events.key == K_LEFT:
p_1_change_x = -1
if events.type == KEYUP:
if events.key == K_RIGHT or K_LEFT:
p_1_change_x = 0
p_1_change_y = 0
screen.fill((255, 255, 255))
p_1_x += p_1_change_x
p_1(p_1_x, p_1_y)
display.update()
drawwing()
display.update()
Upvotes: 2
Views: 97
Reputation: 1985
I made a class for dots:
class Dot():
SIZE = 5
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self):
draw.circle(screen, self.color, (self.x, self.y), Dot.SIZE)
Then I made an array and generate NUMBER_OF_DOTS
like this:
dots = []
for i in range(NUMBER_OF_DOTS):
x = rd.randint(100, 700)
y = rd.randint(100, 500)
dots.append(Dot(x,y))
and in the while
loop, after filling the whole scene by white color, redraw all dots like this:
while True:
screen.fill((255, 255, 255))
...
for dot in dots:
dot.draw()
Happy Coding https://github.com/peymanmajidi/Ball-And-Dots-Game__Pygame
Upvotes: 0
Reputation: 4137
If you want stationary dots, use something like this instead:
dots = list((rd.randint(100,700),rd.randint(100,500)) for i in range(0,250))
def drawwing():
for x,y in dots:
draw.circle(screen, (55, 255, 155), (x, y), 5)
Upvotes: 3