【发布时间】:2013-04-30 01:46:56
【问题描述】:
我刚刚开始使用 PyGame。在这里,我正在尝试绘制一个矩形,但它没有渲染。
这是整个程序。
import pygame
from pygame.locals import *
import sys
import random
pygame.init()
pygame.display.set_caption("Rafi's Game")
clock = pygame.time.Clock()
screen = pygame.display.set_mode((700, 500))
class Entity():
def __init__(self, x, y):
self.x = x
self.y = y
class Hero(Entity):
def __init__(self):
Entity.__init__
self.x = 0
self.y = 0
def draw(self):
pygame.draw.rect(screen, (255, 0, 0), ((self.x, self.y), (50, 50)), 1)
hero = Hero()
#--------------Main Loop-----------------
while True:
hero.draw()
keysPressed = pygame.key.get_pressed()
if keysPressed[K_a]:
hero.x = hero.x - 3
if keysPressed[K_d]:
hero.x = hero.x + 3
if keysPressed[K_w]:
hero.y = hero.y - 3
if keysPressed[K_s]:
hero.y = hero.y + 3
screen.fill((0, 255, 0))
#Event Procesing
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#Event Processing End
pygame.display.flip()
clock.tick(20)
self.x 和 self.y 目前是 0 和 0。
请注意,这不是一个完成的程序,它应该做的只是在绿色背景上绘制一个可以通过 WASD 键控制的红色方块。
【问题讨论】:
-
如果在末尾包含
width参数,它会呈现吗?例如。width=1 -
嘿,对我也不起作用。 :) 结果
rect不接受关键字参数,因此必须给出可选的最后一个参数而没有关键字。但无论哪种方式,这都不是你的问题。 -
如果您确定
self.x和self.y为 0,并且screen已正确初始化,那么我认为您必须发布一个完整的、独立的最小示例说明问题。没有这个,我们只是在猜测。 -
你能发布更多你的代码吗?我想看看显示在哪里更新。
-
发布了整个节目。
标签: python pygame render draw rect