我刚刚开始使用PyGame。在这里,我试图绘制一个矩形,但它不是渲染。不能在Python 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' – 2013-04-30 01:54:44
不,它不会。我, – rafitufi 2013-04-30 02:07:01
嘿,也不适合我。 :)原来'rect'不接受关键字参数,所以可选的最后一个参数必须没有关键字。但无论如何,这不是你的问题。 – 2013-04-30 02:29:05