为什么我在 pygame 上的圈子不断消失和重新产生?
Why do my circles on pygame keep despawning and re-spawning?
当我 运行 这个程序时,我希望游戏在随机位置产生一个较大的可移动点和数百个较小的不可移动点。但是,当我 运行 程序时,较小的点会不断消失和重生。我认为它与 pygame.display.update() 函数有关,但我不完全确定。我该如何解决这个问题?
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()
如果你想要静止的点,请改用这样的东西:
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)
我为点做了一个class:
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)
然后我做了一个数组并生成 NUMBER_OF_DOTS
像这样:
dots = []
for i in range(NUMBER_OF_DOTS):
x = rd.randint(100, 700)
y = rd.randint(100, 500)
dots.append(Dot(x,y))
并且在while
循环中,用白色填充整个场景后,像这样重绘所有点:
while True:
screen.fill((255, 255, 255))
...
for dot in dots:
dot.draw()
快乐编码
https://github.com/peymanmajidi/Ball-And-Dots-Game__Pygame
当我 运行 这个程序时,我希望游戏在随机位置产生一个较大的可移动点和数百个较小的不可移动点。但是,当我 运行 程序时,较小的点会不断消失和重生。我认为它与 pygame.display.update() 函数有关,但我不完全确定。我该如何解决这个问题?
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()
如果你想要静止的点,请改用这样的东西:
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)
我为点做了一个class:
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)
然后我做了一个数组并生成 NUMBER_OF_DOTS
像这样:
dots = []
for i in range(NUMBER_OF_DOTS):
x = rd.randint(100, 700)
y = rd.randint(100, 500)
dots.append(Dot(x,y))
并且在while
循环中,用白色填充整个场景后,像这样重绘所有点:
while True:
screen.fill((255, 255, 255))
...
for dot in dots:
dot.draw()
快乐编码 https://github.com/peymanmajidi/Ball-And-Dots-Game__Pygame