首页 > 解决方案 > Pygame 问题 - 功能正常,但视觉效果不行

问题描述

我正在尝试使用 Pygame 制作游戏,并认为我可以通过重新创建现有项目来了解它的工作原理。所以我跟着这个教程:https ://www.youtube.com/watch?v=UYgyRArKDEs

尽管当我尝试运行视频描述中提供的源代码时,即使终端中的文本是正确的,我也会在游戏应该出现的地方出现一个空白屏幕。Connect-4 在终端中正常工作,但 Pygame 窗口什么也不做,只是保持空白。(虽然我可以重新调整那个窗口的大小。

请参阅教程中的代码,但这是我正在尝试的工作:



# Game: Flower Shop // Flower Factory
from __future__ import print_function
import sys
import os
import pandas as pd
import numpy as np
import operator as op
import numpy as np
import matplotlib.pyplot as plt
import pygame


GREEN = (0, 128, 0)

print('----------------------------')

print(os.getcwd())

print('')
print('----------------------------')
print('Welcome to your Flower Shop!')
print('----------------------------')

w, h = 5, 5
garden = [['x' for x in range(w)] for y in range(h)]


#print(garden)
#

# print(np.matrix(garden))

# for y in range(w):
#     for x in range(h):
#         print garden[y][x]


for y in range(h):
    print()
    for x in range(w):
        print (garden[y][x], end = ' ')

print()
print()


# w=10
# h=10
# fig=plt.figure(figsize=(8, 8))
# columns = 4
# rows = 5
# for i in range(1, columns*rows +1):
#     img = np.random.randint(10, size=(h,w))
#     fig.add_subplot(rows, columns, i)
#     plt.imshow(img)
# plt.show()


pygame.init()

squaresize = 100
width = w * squaresize
height = h * squaresize

size = (width, height)



def create_board():
    board = np.zeros((h,w))
    return board

def draw_garden(board):
    for c in range(w):
        for r in range(w):
            pygame.draw.rect(screen, GREEN, (c*squaresize, r*squaresize, squaresize, squaresize))





pygame.display.set_caption("Flower Breeder")
game_over = False
board = create_board()


screen = pygame.display.set_mode(size)
draw_garden(board)
pygame.display.update()


while not game_over:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    pygame.event.get()

    pygame.draw.rect(screen, (255,0,0), (w, h, width, height))  #This takes: window/surface, color, rect
    pygame.display.update() # This updates the screen so we can see our rectangle

    screen.fill(GREEN)
    pygame.display.update()



# Flowers:
# easy - lilies, cosmos


# TODO: see notes

# TODO: use python to amimate the breeding using pictures to represent the flower pathes

谢谢!我真的需要通过这个空白屏幕:/

标签: pythonimagepygame

解决方案


请注意,颜色值具有 alpha 通道,因此来自 pygame 文档的 (r,g,b,255) :

pygame 1.8.1 中有一个错误,默认 alpha 为 0,而不是像以前那样的 255。

这可能是填充你的显示器时的问题。而且我不太确定抽奖顺序和双重更新

尝试

screen.fill()
pygame.draw.rect()
screen.update()

推荐阅读