這是我爲擴展pygame
模塊而開發的一個腳本。我不想爲你寫代碼,但這應該讓你開始!我的代碼有很好的文檔記錄,應該很容易理解。祝你好運!
屏模塊
import pygame, sys, ctypes, os
from pygame.locals import *
import random
class Screen:
'''
--Screen module for handling the pygame display module...
'''
def __init__(self, resolution=(640,480), caption=None, transparent=False, icon=None, flags=0):
'''
--Grabs and sets the args
--Grabs the center of the screen based on the monitor resolution
--Creates a new instance of screen
'''
self.resolution = resolution
self.flags = flags
if caption:
self.setCaption(caption)
if icon:
self.setIcon(icon)
self.monitorCenter = self.getCenterScreen(self.getMonitorSize())
self.centerScreen()
self.transparent = transparent
self.screen = self.setScreen()
def update(self):
pygame.display.update()
def setIcon(self, icon):
'''
--sets the icon at the icon position on
the pygame window. Maintain origonal
.jpg location
Usage:
--self.setIcon(self, 'coolIcon.jpg'
'''
try:
self.icon = icon
icon = pygame.image.load(icon)
except:
self.icon = None
return
pygame.display.set_icon(icon)
def setScreen(self):
'''
--creates and returns default pygame window...
Usage:
--screen.setScreen()
'''
if self.transparent:
return pygame.display.set_mode(self.resolution, self.flags).convert_alpha()
return pygame.display.set_mode(self.resolution, self.flags)
def setCaption(self, caption):
'''
--Sets window caption as (caption)...
Usage:
--screen.setCaption("My Game!")
'''
pygame.display.set_caption(caption)
def didResize(self):
info = pygame.display.get_surface()
return info
def setMousePos(self, pos):
'''
--Sets the mouse at [pos]...
Usage:
--screen.setMousePos((250,250))
'''
pygame.mouse.set_pos(pos)
def setScreenPos(self, position):
'''
--Sets the windows window argument position to (position)
Usage:
--screen.setScreenPos((250,250))
'''
os.environ['SDL_VIDEO_WINDOW_POS'] = str(position[0])+','+str(position[1])
def getMonitorSize(self):
'''
--Returns the actual resolution of the active display
Usage:
--screen.getMonitorSize()
'''
user32 = ctypes.windll.user32
screenSize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
return screenSize
def getCenterScreen(self, res):
'''
--Returns the center of the screen based on the monitor
--resolution [res]/2 - window resolution [self.resolution]/2
Usage:
--screen.getCenterScreen((1365,782))
'''
center = ((res[0]/2)-self.resolution[0]/2), ((res[1]/2)-self.resolution[1]/2)
return center
def centerScreen(self):
'''
--sets the center of the pygame window to the
--center of the monitor
--centerScreen() needs to be called before
--setScreen() in order for it to center...
Usage:
--screen.centerScreen()
'''
centerScreen = self.getCenterScreen(self.getMonitorSize())
self.setScreenPos(centerScreen)
def getFlags(self):
'''
--Returns the flags associated the the active screen...
Usage:
--screen.getFlags()
'''
return self.getScreenSurface().get_flags()
def getScreenSurface(self):
'''
--Returns the surface object of the active screen...
Usage:
--screen.getScreenSurface()
'''
return pygame.display.get_surface()
def getCaption(self):
'''
--Returns the current caption associated with the active screen...
Usage:
--screen.getCaption()
'''
return pygame.display.get_caption()[0]
def getMousePos(self):
'''
--Returns the current mouse position [x,y] on the window...
Usage:
--screen.getMousePos()
'''
return pygame.mouse.get_pos()
def closeScreen(self):
'''
--De-initializes the screen object...
Usage:
--screen.closeScreen()
'''
pygame.display.quit()
def initScreen(self):
'''
--Re-initializes the screen object...
Usage:
--screen.initScreen()
'''
pygame.display.init()
def resize(self, size):
'''
--Creates a new screen instance with resolution of (size)...
--Overwrites the self.screen object with new size
--Grabs the New Size, current Caption, and current Flags
--Closes the current screen instance
--Creates a new screen instance
--Places the center of the screen at the center of the monitor
--Checks if flags contains FULLSCREAN if so it toggles
--Creates a new display instance with resolution[x,y], and flags(flags)
--Sets the screen caption
Usage:
--screen.resize((640,480))
'''
self.resolution = size
caption = self.getCaption()
self.flags = self.getFlags()
self.closeScreen()
self.initScreen()
self.centerScreen()
if self.flags == FULLSCREEN:
print self.flags
self.screen = pygame.display.set_mode((self.resolution),self.flags^FULLSCREEN)
else:
self.screen = pygame.display.set_mode((self.resolution),self.flags)
self.setCaption(caption)
def toggleFullscreen(self):
'''
Module for toggeling between default resolution and fullscreen...
--Grabs the current screen CAPTION, FLAGS, and MOUSE POSITION
--Grabs the current surface size
--Checks to see what resolution the screen is currently at and toggles it
--Overwrites the self.screen object with new size
--Closes the current screen instance
--Creates a new screen instance
--Places the center of the screen at the center of the monitor
--Creates a new display instance with resolution[x,y], and flags^FULLSCREEN(toggle)
--Sets the new screens caption with old caption
Usage:
--screen.toggleFullscreen()
'''
caption = self.getCaption()
self.flags = self.getFlags()
surface = self.getScreenSurface()
size = surface.get_width(), surface.get_height()
if size == self.resolution:
size = self.getMonitorSize()
else:
size = self.resolution
self.closeScreen()
self.initScreen()
self.centerScreen()
self.screen = pygame.display.set_mode((size),self.flags^FULLSCREEN)
self.setCaption(caption)
self.setIcon(self.icon)
class Grid(object):
def __init__(self, screen, size=(16,16)):
self.screen = screen
self.surface = self.screen.screen
self.size = size
self.grid = tuple(((x,y) for y in range(0, self.screen.resolution[1], size[1]) for x in range(0, self.screen.resolution[0], size[0])))
self.elements = self.create_elements()
self.active_elements = []
def create_elements(self):
l = {}
for x,y in self.grid:
if x in l:
l[x].update({y:None})
else:
l[x] = {0:None}
return l
def set_grid(self, new_size):
self.grid = ((x,y) for y in range(0, self.screen.resolution[1], new_size[1])for x in range(0, self.screen.resolution[0], new_size[0]))
def check_active(self):
self.active_elements = [i for i in [self.elements[x][y] for x,y in self.grid] if i != None]
def add_element(self, (x,y), color):
self.elements[x][y] = (color, (x*self.size[0],y*self.size[1], self.size[0], self.size[1]), 0)
self.check_active()
def remove_element(self, (x,y)):
self.elements[x][y] = None
self.check_active()
def draw_grid(self):
for x, y in self.grid:
pygame.draw.line(self.surface, (255,255,255), (x, 0), (x, self.screen.resolution[0]), 1)
pygame.draw.line(self.surface, (255,255,255), (0, y), (self.screen.resolution[0], y), 1)
for elem in self.active_elements:
pygame.draw.rect(self.surface, *elem)
主代碼
import pygame, sys
from pygame.constants import *
from PyScreen.screen import Screen, Grid
pygame.init()
if __name__ == '__main__':
my_display = Screen(caption='Conways Game Of Life', resolution=(1280, 720))
grid = Grid(my_display, (16,16)) #second parameter is the grid size
grid.add_element((0, 0), (255,255,255)) #anytime you want to add a cell
grid.add_element((16,16), (255,255,255))
grid.remove_element((0, 0)) #when you want to remove a cell
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
my_display.screen.fill((0,0,0))
grid.draw_grid()
my_display.update()
你能提供你的代碼,我或許可以提供一些基本的接口 – TheLazyScripter
問題,要求我們建議還是找一本書,工具,軟件庫,教程或其他非現場資源對於堆棧溢出而言是無關緊要的,因爲它們傾向於吸引自以爲是的答案和垃圾郵件。相反,請描述問題以及到目前爲止解決問題所做的工作。請參閱[我可以在這裏詢問什麼主題?](http://stackoverflow.com/help/on-topic)。 –
@Rory Daulton我不認爲我的問題是基於意見的,我只希望一個單一的程序能夠做到我想要的,我不問問哪個是最好的程序。當然,我寧願它很簡單,因爲我在編程方面很新,但這是一個意見問題。我只是想替代品。 – Diego