我试图使用Pygame在python 3.5中编写一个简单的推箱子游戏,但是当我运行代码时,我得到的是黑色背景。我确定我只是做了一些错误的事情,但仍然学习不太确定。 任何人都可以给予任何帮助将不胜感激:)
import random, pygame, sys
from pygame.locals import *
from Classes import Maze, Move, Player, Box
maze = Maze()
WHITE = (255, 255, 255)
FPS = 30
WINWIDTH = 750
WINHEIGHT = 750
HALFHEIGHT = int(WINWIDTH/2)
HALFWIDTH = int(WINWIDTH/2)
#BASICFONT = pygame.font.Font('freesansbold.tff',100)
#Tile height and width in pixels
TILEWIDTH = 30
TILEHEIGHT = 30
TILEFLOORHEIGHT = 30
IMAGESDICT = {"WALL": pygame.image.load("C:/Users/Trail/Documents/College 2/Object Oriented Programming/Sokoban Implement/images/wall.bmp"),
"FLOOR": pygame.image.load(""C:/Users/Trail/Documents/College 2/Object Oriented Programming/Sokoban Implement/images/floor.bmp"),
"CRATE": pygame.image.load(""C:/Users/Trail/Documents/College 2/Object Oriented Programming/Sokoban Implement/images/object.bmp"),
"TARGET": pygame.image.load("C:/Users/Trail/Documents/College 2/Object Oriented Programming/Sokoban Implement/images/wall.bmp"object_store),
"PLAYER": pygame.image.load("C:/Users/Trail/Documents/College 2/Object Oriented Programming/Sokoban Implement/images/mover_left.bmp")}
TILEMAPPING = {"#":IMAGESDICT["WALL"],
" ":IMAGESDICT["FLOOR"],
"$":IMAGESDICT["CRATE"],
"@":IMAGESDICT["TARGET"],
"P":IMAGESDICT["PLAYER"]}
#Display surface
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAY = pygame.display.set_mode((WINWIDTH,WINHEIGHT))
pygame.display.set_caption("Sokoban")
def Build(maze):
maze.walls(10)
maze.targets(5)
maze.startPos()
mapSurfWidth = maze.getWidth() * TILEWIDTH
mapSurfHeight = maze.getHeight() * TILEHEIGHT
mapSurf = pygame.Surface((mapSurfWidth,mapSurfHeight))
mapSurf.fill(WHITE)
for h in range(maze.getHeight()):
for w in range(maze.getWidth()):
tile = pygame.Rect((w * TILEWIDTH, h * TILEFLOORHEIGHT, TILEWIDTH, TILEHEIGHT))
if maze.getPos(h,w) in TILEMAPPING:
baseTile = TILEMAPPING[maze.getPos(h,w)]
#Draw
mapSurf.blit(baseTile,tile)
return mapSurf
def main():
Build(maze)
main()
我的Maze类(这是在我的代码中缩进但不是格式化):
class Maze:
def __init__(self):
self.width = 20
self.height = 10
#init
width = 20
height = 10
#Create 2D array for maze
self.maze = [[0 for i in range(width)] for j in range(height)]
#Create square grid
for i in range(width):
for j in range(height):
if j == 0 or j == height - 1:
self.maze[j][i] = "#"
elif i == 0 or i == width - 1:
self.maze[j][i] = "#"
else:
self.maze[j][i] = " "
def crates(self,level):
self.level = level
for i in range(0,level):
#Don't spawn here if wall is already generated
if self.maze[i+2][i+5] == "#":
self.maze[i+3][i+6] = "$"
else:
self.maze[i+2][i+5] = "$"
return self.maze
def walls(self,level):
self.level = level
#Semi working, needs randomised for play
for i in range(0,level):
for j in range(0,level):
#Shitty random gen
y = level / 2
x = randint(0,level)
y = randint(0,y)
#Shitty test random wall gen
if x % 3 == 0:
self.maze[y][x] = "#"
"""if (j % 3 == 0) and (i < 4):
self.maze[j][i] = "#"
elif (j % 2 == 0) and (i > 8):
self.maze[i][j] = "#"""
return self.maze
def targets(self,level):
self.level = level
#Test
for i in range(0,level):
self.maze[i+2][i+10] = "@"
return self.maze
def startPos(self):
x = randint(1,5)
y = randint(1,5)
self.maze[x][y] = "P"
return self.maze
def getWidth(self):
return self.width
def getHeight(self):
return self.height
def getPos(self,row,col):
return self.maze[row][col]