0

私は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現在は 0とself.y0 です。これは完成したプログラムではないことに注意してください。WASD キーで制御できる緑の背景に赤い四角を描くだけです。

4

3 に答える 3

5

メイン ループの一部を見てみましょう。

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))

Hero クラスの draw 関数内で、rect を描画しています。メイン ループでは、 を呼び出しhero.draw()、入力を処理した後、 を呼び出していscreen.fill()ます。これは、今描いた四角形の上に描いています。これを試して:

while True:

    screen.fill((0, 255, 0))
    hero.draw()

    keysPressed = pygame.key.get_pressed()
    ....

これにより、画面全体が緑色になり、緑色の画面上に四角形が描画されます。

于 2013-04-30T17:33:43.713 に答える
0

次のリンクを確認してください。

http://www.pygame.org/docs/ref/draw.html#pygame.draw.rect

そして、ここにいくつかの例があります:

http://nullege.com/codes/search?cq=pygame.draw.rect

pygame.draw.rect(screen, color, (x,y,width,height), thickness)

pygame.draw.rect(screen, (255, 0, 0), (self.x, self.y, 50, 50), 1)
于 2013-04-30T01:51:52.383 に答える