2

私は初心者の Python プログラマーで、テトリス ゲームを作ろうとしています。ゲームを実行すると、ブロックが毎秒 10 ピクセルずつ下に移動します。問題は、ブロックがランダムに停止し、印刷ステートメントも印刷を停止することです。タスクマネージャーによると、エラーは発生していません。メモリは問題ありません。私は celeron プロセッサを搭載した vista 32 ビットを使用していますが、i5 プロセッサを搭載した Windows 7 でも試してみましたが、問題が解決しないため、迷っています。

これはコードです:

import pygame
from pygame.locals import *
from datetime import datetime
import Blocks

def Gravity(Box, rate):

    # Gravity effect
    Box.set_yCoor(Box.get_yCoor()+ rate)
    Box.Edge.set_yCoor(Box.Edge.get_yCoor()+ rate)
    return Box


def main():
    # initailize all pygame modules
    pygame.init()

    windowWidth,windowHeight= 640,700
    screen_color= (255,255,255)

    # Determines how many pixels per time the box will fall
    gravity= 10

    # Create screen/window and change color to white
    screen= pygame.display.set_mode((windowWidth,windowHeight),0,32)
    screen.fill(screen_color)

    # Used to determine whether to draw box and apply gravity
    index =0

    # Put box and border info in list
    # Args: (x,y) (len, width) (color)
    boxInfo= [10,10, 20,50, (0,0,0)]
    borderInfo= [5,5, 30,60, (154,24,214)]
    # Create instance of box and pass info
    Box= Blocks.Box(boxInfo, borderInfo)

    #Create an endless loop that the game will run inside of
    while True:
        for event in pygame.event.get():
            if event.type== QUIT:
                pygame.quit()
                return

        # Get second and millisecond time and convert to string
        startTime= str(datetime.now())
        # Splice time string and convert to float
        startTime= float(startTime[17:23])

        if index== 0:
            # Pull the box down by gravity
            Box= Gravity(Box, gravity)

            # Move box back to the top of window
            if Box.Edge.get_yCoor() > windowHeight - Box.Edge.get_boxWid():
                Box.Edge.set_yCoor(5)
                Box.set_yCoor(10)

            # Clear screen
            screen.fill(screen_color)
            # Display Screen
            Box.display(screen)
            # Set the stop time used to determine when to call gravity and display
            stopTime= startTime+ 1
            index= 1

            # Get second and millisecond time and convert to string
            bug= str(datetime.now())
            # Splice time string and convert to float
            bug= bug[17:23]

            print("\nDisplayed at:")
            print('Start:', startTime)
            print('Stop:', stopTime)
            print('Bug:', bug)
        elif startTime >= stopTime:
            index= 0
            # Get second and millisecond time and convert to string
            bug= str(datetime.now())
            # Splice time string and convert to float
            bug= bug[17:26]

            print("\nNot displaying at:")
            print('Start:', startTime)
            print('Stop:', stopTime)
            print('Bug:', bug)


# Call main
main()

これはブロック クラスです: import pygame from pygame.locals import *

class Box():

    def __init__(self, boxInfo, edgeInfo):
        # Pixel location of the box
        self.__xCoor= boxInfo[0]
        self.__yCoor= boxInfo[1]

        # Pixel length and width of the box
        self.__boxLen= boxInfo[2]
        self.__boxWid= boxInfo[3]

        # Pixel color is white by default
        self.__color= boxInfo[4]

        self.Edge= Border(edgeInfo)

    # ---- ACCESSORS -- and -- MUTATORS ----

    def set_xCoor(self,Coordx):
        self.__xCoor= Coordx

    def get_xCoor(self):
        return self.__xCoor

    def set_yCoor(self,Coordy):
        self.__yCoor= Coordy

    def get_yCoor(self):
        return self.__yCoor

    def set_boxLen(self,Length):
        self.__boxLen= Length

    def get_boxLen(self):
        return self.__boxLen

    def set_boxWid(self,Width):
        self.__boxWid= Width

    def get_boxWid(self):
        return self.__boxWid

    def set_color(self,color):
        self.__color= color

    def get_color(self):
        return self.__color

    # ---- METHODS ----

    def boxStatCheck(self):
        # Prints all the attributes in the shell for debug
        print("x-Coordinate=", self.get_xCoor())
        print("y-Coordinate=", self.get_yCoor())
        print("Box Length=", self.get_boxLen())
        print("Box Width=", self.get_boxWid())
        print("Color Value=", self.get_color())


    def display(self, screen):
        screen.lock()
        pygame.draw.rect(screen, self.Edge.get_color(), Rect((self.Edge.get_xCoor(),self.Edge.get_yCoor()),
                                                        (self.Edge.get_boxLen(),self.Edge.get_boxWid())))

        pygame.draw.rect(screen, self.get_color(), Rect((self.get_xCoor(),self.get_yCoor()),
                                                        (self.get_boxLen(),self.get_boxWid())))
        screen.unlock()
        pygame.display.update()


class Border():

    def __init__(self, listInfo):
        # Pixel location of the box
        self.__xCoor= listInfo[0]
        self.__yCoor= listInfo[1]

        # Pixel length and width of the box
        self.__boxLen= listInfo[2]
        self.__boxWid= listInfo[3]

        # Pixel color is white by default
        self.__color= listInfo[4]

    # ---- ACCESSORS -- and -- MUTATORS ----

    def set_xCoor(self,Coordx):
        self.__xCoor= Coordx

    def get_xCoor(self):
        return self.__xCoor

    def set_yCoor(self,Coordy):
        self.__yCoor= Coordy

    def get_yCoor(self):
        return self.__yCoor

    def set_boxLen(self,Length):
        self.__boxLen= Length

    def get_boxLen(self):
        return self.__boxLen

    def set_boxWid(self,Width):
        self.__boxWid= Width

    def get_boxWid(self):
        return self.__boxWid

    def set_color(self,color):
        self.__color= color

    def get_color(self):
        return self.__color

    # ---- METHODS ----

    def boxStatCheck(self):
        # Prints all the attributes in the shell for debug
        print("x-Coordinate=", self.get_xCoor())
        print("y-Coordinate=", self.get_yCoor())
        print("Box Length=", self.get_boxLen())
        print("Box Width=", self.get_boxWid())
        print("Color Value=", self.get_color())


    def display(self, screen):
        pygame.draw.rect(screen, self.get_color(), Rect((self.get_xCoor(),self.get_yCoor()),
                                                        (self.get_boxLen(),self.get_boxWid())))
4

2 に答える 2

0

startTime に 1 を追加して stopTime を設定しているため、startTime が 59.xx になると、stopTime は 60 より大きい値になります。startTime は 0 に戻ります。これは新しい分であり、行き詰まっているためです。index = 1 で、startTime が stopTime より大きくなることはありません。if または elif ステートメントはどれも True と評価されません。ボックスを再度描画する前に遅延を組み込むことが目標である場合は、次のようなことを検討してください...

pauseLength = 120

そしてゲームループで...

pygame.time.delay(pauseLength)

これにより、後で難易度を追加するためにゲームをスピードアップすることもできます.

于 2013-05-20T18:51:55.990 に答える