2016-12-15 77 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