gpt4 book ai didi

graphics - pygame图形网格

转载 作者:行者123 更新时间:2023-12-02 04:04:55 26 4
gpt4 key购买 nike

您好,我是Python和图形编程的新手。目前,我正在实践中创建网格,并且在使pygame将对象放置在表面窗口顶部时遇到问题。

以下是我的注释代码。我认为问题可能与blit函数有关,但我不确定。任何帮助,将不胜感激。此外,Python Shell不会突出显示任何异常或错误,但是程序不会运行。

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


def main():
#settings
boardDims = (20,20) #number of cells on board

#pygame settings
cellDims = (20,20) #number of pixels on cells
framerate = 50
colours = {0:(0,0,0), 1:(255,255,255)}

#pygame
pygame.init()
#sets x and y pixels for the window
dims = (boardDims[0] * cellDims[0],
boardDims[1] * cellDims[1])
#sets window, background, and framrate
screen = pygame.display.set_mode(dims)
background = screen.convert()
clock = pygame.time.Clock()

#create new board
board = {}
#iterates over x and y axis of the window
for x in range(boardDims[0]):
for y in range(boardDims[1]):
board[(x,y)] = 0 #sets whole board to value 0
board[(1,1)] = 1 #sets one square at co-ordinates x=1,y=1 to cell
# value 1
return board


running = 1
while running:
# 1 PYGAME
#get input
for event in pygame.event.get():
if event.type == QUIT or \
(event.type == KEYDOWN and event.key == K_ESCAPE):
running = 0
return
#link pygames clock to set framerate
clock.tick(framerate)


for cell in board:
#adds cells dimensions and co-ordinates to object rectangle
rectangle = (cell[0]*cellDims[0], cell[1]* cellDims[1],
cellDims[0], cellDims[1])
#pygame draws the rectabgle on the background using the relevant
#colour and dimensions and co-ordinates outlined in 'rectangle'
square = pygame.draw.rect(background, colours[board[cell]],
rectangle)
#blits and displays object on the background
background.blit(square)
screen.blit(background, (0,0))
pygame.display.flip()


if __name__ == "__main__":
main()

最佳答案

在执行任何blit之前,“返回板”语句正在退出程序。

同样,您可以直接在屏幕变量上使正方形变灰,即:您不需要背景变量。

关于graphics - pygame图形网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8213764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com