不能停止程序随机化。 Pygame的

时间:2016-12-15 07:59:59

标签: python-3.x pygame

我知道这是一个非常古怪的问题,但问题是这个。 我正在做一个游戏,我想将星星随机化到一个巨大的空间区域

但我得到的结果是:

  1. 这个空间不断地重复着星星的位置,所以一切都在移动。
  2. 它只显示窗口左上角的1​​颗星。
  3. 以下是完整代码:

    import os
    import pygame
    from pygame.locals import *
    import random
    
    gstars = 500
    gstarmax = 0
    red = (255,0,0)
    yellow = (240,250,60)
    timer = 0
    size = [1600, 1000]
    screen = pygame.display.set_mode((1600, 1000), HWSURFACE | DOUBLEBUF | RESIZABLE)
    pygame.display.set_caption('planet system test')
    clock = pygame.time.Clock()
    
    area = pygame.image.load("screenMonitor.png")
    
    
    done = False
    while done == False:
        pygame.event.pump()
        mouse_x, mouse_y = pygame.mouse.get_pos()
    
        timer += 1
        if timer > 99:
            timer = 0
        screen.fill((0,0,0))
        pygame.draw.rect(screen, red, (20, 20, 1350, 480), 2)
        pygame.draw.rect(screen,yellow,(2,2,2,2),2)
        go = True
        if go == True:
            for i in range(gstars):
                s1 = random.randrange(1,1600)
                s2 = random.randrange(1,1000)
                colorSelect = random.randrange(0,5)
    
    
                if colorSelect == 1:
                    yellow = (240,250,60)
    
                if colorSelect == 2:
                    yellow = (249,173,60)
    
                if colorSelect == 3:
                    yellow = (226,43,68)
    
                if colorSelect == 4:
                    yellow = (52,173,84)
    
                go = False
                pygame.draw.rect(screen,yellow,(2+s1,2+s2,2,2),2)
    
    
    
    
    
        pygame.display.flip()
    
        clock.tick(30)
        pygame.quit()
    

1 个答案:

答案 0 :(得分:1)

麻烦在于主/事件循环。

你把代码放在主循环中。因为它在每次迭代时都是随机的,所以它会移动。

我删除可能导致错误只显示一颗星的go = False

在下面的代码中,我创建了一个星形列表,其位置在索引0和1中,而颜色的元组在2中。 使用random.choice(Sequence)函数随机选择颜色元组。 https://docs.python.org/2/library/random.html

import pygame
from pygame import *
import random

gstars = 500
gstarmax = 0
red = (255,0,0)
yellow = (240,250,60)
timer = 0
size = [1600, 1000]

    pygame.init()    
    pygame.display.set_caption('planet system test')
    screen = pygame.display.set_mode((size[0], size[1]))
    clock = pygame.time.Clock()

    area = pygame.image.load("screenMonitor.png")

    color = ((240,250,60),(249,173,60),(226,43,68),(52,173,84))
    stars = []
    for i in range(gstars):
        s1 = random.randrange(1,1600)
        s2 = random.randrange(1,1000)
        starcolor = random.choice(color)
        stars.append([s1, s2, starcolor])

    done = False
    while done == False:

        for event in pygame.event.get():
            if event.type == pygame.QUIT: #Quit the game if X 
                done = False

        screen.fill(0)
        pygame.draw.rect(screen, red, (20, 20, 1350, 480), 2)
        pygame.draw.rect(screen,yellow,(2,2,2,2),2)
        for star in stars:
            pygame.draw.rect(screen,star[2],(2+star[0],2+star[1],2,2),2)


        pygame.display.flip()

        clock.tick(30)

编辑:根据Ted Klein Bergman的建议更正代码。