2016-12-15 73 views
0

叶我知道这是相当低调的问题,但问题是这样的。 我正在做一个游戏,我想随机明星来了巨大的空间区域无法停止程序随机化。 Pygame

但结果是我得到的是:

  1. 空间保持recaculating明星的地方,这样的一切动作。
  2. 它仅显示窗口左上角的一颗星。

下面是完整的代码:

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

麻烦的是在主/事件循环。

你把你的星星定位在主循环内的代码。由于它在每次迭代中都是随机的,它会移动。

我删除了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) 

编辑:更正以下特德·克莱恩伯格曼的建议代码。

+0

代码似乎正常工作,它会正确生成星形,但程序卡住了。它不会给出任何崩溃消息,它只是停止响应。试图找出它,但看到没有什么是错的。 – cmdtvt

+0

对不起,我的代码中有个小错误:'pygame.event.pump()'。忘记()。 如果event.type是pygame.QUIT,我倾向于使用以下代码退出pygame的主循环:for event in pygame.event.get(): :#如果X done = False,则退出游戏 –

+0

我添加了'pygame.quit()',但它仍然可以。这是否仅仅运行一代恒星?编辑:哦,对不起,我的坏运行明星知道,不会崩溃似乎工作知道 – cmdtvt