我刚开始玩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
和{
这更多的是一个延伸的评论和问题,而不是一个答案。在
下面画一个红色正方形。对你有用吗?在
检查以下链接:
http://www.pygame.org/docs/ref/draw.html#pygame.draw.rect
这里有一些例子:
http://nullege.com/codes/search?cq=pygame.draw.rect
让我们看看主循环的一部分:
在Hero类的draw函数中,您正在绘制rect。在主循环中,您将调用
^{pr2}$hero.draw()
,然后在处理完输入之后,您将调用screen.fill()
。这是在你刚刚画的矩形图上画的。试试这个:这将使整个屏幕变绿,然后在绿色屏幕上绘制矩形。在
相关问题 更多 >
编程相关推荐