2012-07-22 16 views
11

मैं इस ऐप के आकार बदलने की अनुमति देने की कोशिश कर रहा हूं, मैंने RESIZABLE ध्वज रखा है, लेकिन जब मैं आकार बदलने का प्रयास करता हूं, तो यह गड़बड़ हो जाता है! मेरा कोड आज़माएंविंडो का आकार बदलने की अनुमति देना pyGame

यह एक ग्रिड प्रोग्राम है, जब खिड़की का आकार बदलता है, तो मैं ग्रिड को आकार बदलना/हटाना चाहता हूं।

import pygame,math 
from pygame.locals import * 
# Define some colors 
black = ( 0, 0, 0) 
white = (255, 255, 255) 
green = ( 0, 255, 0) 
red  = (255, 0, 0) 

# This sets the width and height of each grid location 
width=50 
height=20 
size=[500,500] 
# This sets the margin between each cell 
margin=1 


# Initialize pygame 
pygame.init() 

# Set the height and width of the screen 

screen=pygame.display.set_mode(size,RESIZABLE) 

# Set title of screen 
pygame.display.set_caption("My Game") 

#Loop until the user clicks the close button. 
done=False 

# Used to manage how fast the screen updates 
clock=pygame.time.Clock() 

# -------- Main Program Loop ----------- 
while done==False: 
    for event in pygame.event.get(): # User did something 
     if event.type == pygame.QUIT: # If user clicked close 
      done=True # Flag that we are done so we exit this loop 
     if event.type == pygame.MOUSEBUTTONDOWN: 
      height+=10 

    # Set the screen background 
    screen.fill(black) 

    # Draw the grid 
    for row in range(int(math.ceil(size[1]/height))+1): 
     for column in range(int(math.ceil(size[0]/width))+1): 
      color = white 
      pygame.draw.rect(screen,color,[(margin+width)*column+margin,(margin+height)*row+margin,width,height]) 

    # Limit to 20 frames per second 
    clock.tick(20) 

    # Go ahead and update the screen with what we've drawn. 
    pygame.display.flip() 
# Be IDLE friendly. If you forget this line, the program will 'hang' 
# on exit. 
pygame.quit() 

कृपया मुझे बताएं कि क्या गलत है, धन्यवाद।

उत्तर

9

जब आप विंडो बदलते हैं तो आप अपनी चौड़ाई, ऊंचाई या आकार को अपडेट नहीं कर रहे हैं।

डॉक्स से: http://www.pygame.org/docs/ref/display.html

प्रदर्शन pygame.RESIZABLE ध्वज के साथ सेट है, तो pygame.VIDEORESIZE घटनाओं उपयोगकर्ता खिड़की आयाम समायोजित कर देता है जब भेजा जाएगा।

आप नए size, w, h घटना VIDEORESIZEhttp://www.pygame.org/docs/ref/event.html

0

एक साधारण नमस्ते विश्व विंडो आकार बदलने योग्य है से प्राप्त कर सकते हैं, के साथ साथ मैं चारों ओर वर्गों के साथ खेल रहा था।
रंगीन स्थिरांक को परिभाषित करने के लिए दो फाइलों में टूटा हुआ।

import pygame, sys 
from pygame.locals import * 
from colors import * 


# Data Definition 
class helloWorld: 
    '''Create a resizable hello world window''' 
    def __init__(self): 
     pygame.init() 
     self.width = 300 
     self.height = 300 
     DISPLAYSURF = pygame.display.set_mode((self.width,self.height), RESIZABLE) 
     DISPLAYSURF.fill(WHITE) 

    def run(self): 
     while True: 
      for event in pygame.event.get(): 
       if event.type == QUIT: 
        pygame.quit() 
        sys.exit() 
       elif event.type == VIDEORESIZE: 
        self.CreateWindow(event.w,event.h) 
      pygame.display.update() 

    def CreateWindow(self,width,height): 
     '''Updates the window width and height ''' 
     pygame.display.set_caption("Press ESC to quit") 
     DISPLAYSURF = pygame.display.set_mode((width,height),RESIZABLE) 
     DISPLAYSURF.fill(WHITE) 


if __name__ == '__main__': 
    helloWorld().run() 

colors.py:

BLACK = (0, 0,0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
YELLOW = (255, 255, 0) 
BLUE = (0,0,255) 

GREEN = (0,255,0) 
+7

कोड काम करता है, लेकिन तुम सच में पीईपी 8 शैली दिशा निर्देशों को पढ़ना चाहिए। आप 'CreateWindow' जैसे नामों के साथ कई सम्मेलनों का निर्माण कर रहे हैं जो कि कक्षा नहीं है,' हैलो वर्ल्ड 'यह * है, और' DISPLAYSURF' जो निरंतर नहीं है। साथ ही, हर जगह से 'आयात *' स्पैमिंग से बचें, खासकर जब से आप उनका उपयोग नहीं कर रहे हैं (आप वैसे भी सभी 'pygame' कॉल उपसर्ग कर रहे हैं) – MestreLion

5

ninMonkey से वर्णन सही था (https://stackoverflow.com/a/11604661/3787376)।

जब आप विंडो परिवर्तन करते हैं तो आप अपनी चौड़ाई, ऊंचाई या आकार को अपडेट नहीं कर रहे हैं।

तो जवाब pygame खिड़की से बनाना, इसके आकार
(यह वर्तमान विंडो का आकार बदलता है और इसकी सतह पर सभी पिछले सामग्री को हटाता है) को अद्यतन करने के लिए बस है।

>>> import pygame 
>>> 
>>> pygame.display.set_mode.__doc__ 
'set_mode(resolution=(0,0), flags=0, depth=0) -> Surface\nInitialize a window or screen for display' 
>>> 

यह pygame.VIDEORESIZE घटनाओं, उपयोगकर्ता आकार बदलने योग्य खिड़की के आयाम बदल जाता है जब जो भेजा जाता है पर किया जाना चाहिए। साथ ही, वर्तमान विंडो सामग्री को रखने के लिए नीचे दी गई विधि का उपयोग करने की आवश्यकता हो सकती है।

कुछ उदाहरण कोड:

import pygame, sys 
# from pygame.locals import * # This would make Pygame constants (in capitals) not need the prefix "pygame." 

pygame.init() 

# Create the window, saving it to a variable. 
surface = pygame.display.set_mode((350, 250), pygame.RESIZABLE) 
pygame.display.set_caption("Example resizable window") 

while True: 
    surface.fill((255,255,255)) 

    # Draw a red rectangle that resizes with the window as a test. 
    pygame.draw.rect(surface, (200,0,0), (surface.get_width()/3, 
              surface.get_height()/3, 
              surface.get_width()/3, 
              surface.get_height()/3)) 

    pygame.display.update() 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_ESCAPE: 
       pygame.quit() 
       sys.exit() 
     if event.type == pygame.VIDEORESIZE: 
      # The main code that resizes the window: 
      # (recreate the window with the new size) 
      surface = pygame.display.set_mode((event.w, event.h), 
               pygame.RESIZABLE) 

पिछली सामग्री को संकट से बचाने के लिए एक विधि:

  1. :
    यहां कुछ युक्तियां अपने जीयूआई के कुछ हिस्सों को छोड़ने के लिए अपरिवर्तित है एक दूसरी चर बनाओ, पुरानी विंडो सतह परिवर्तक के मान पर सेट करें।
  2. नई विंडो बनाएं, इसे पुराने चर के रूप में संग्रहीत करें।
  3. दूसरी सतह को पहले (पुराने चर) पर खींचें - blit फ़ंक्शन का उपयोग करें।
  4. इस चर का उपयोग करें और नए चर को हटाएं (वैकल्पिक, del का उपयोग करें) यदि आप स्मृति बर्बाद नहीं करना चाहते हैं।

ऊपर समाधान के लिए कुछ नमूना कोड (, pygame.VIDEORESIZE घटना if बयान में चला जाता है शुरू में):

  old_surface_saved = surface 
      surface = pygame.display.set_mode((event.w, event.h), 
               pygame.RESIZABLE) 
      # On the next line, if only part of the window needs to be copied, there's some other options. 
      surface.blit(old_surface_saved, (0,0)) 
      del old_surface_saved # This line may not be needed. 
संबंधित मुद्दे