機能を使用すると、pygameのウィンドウを閉じます

hippozhipos:

以下は、あるStringクラス。私のメインループの中で、このクラスから描画機能を使用すると、すぐにエラーなしでゲームを終了し、限り、私はそれを含めてはいけない、ゲームが罰金を実行します。それは私に次のような警告を与えるん。

Warning (from warnings module):
  File "C:\Users\rahul\OneDrive\Documents\A level python codes\shootingGame.py", line 44
    D.blit(self.player, (self.pos[0], self.pos[1]))
DeprecationWarning: an integer is required (got type float).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.

import math, sys, os, pygame
from pygame.locals import *

pygame.init()

win = pygame.display
D = win.set_mode((1200, 600))

class String:
    def __init__(self, x, y):
        self.pos = [x, y]
        self.dx = 0
        self.dy = 0
        self.string = pygame.Surface((1, 1)).convert_alpha()
        self.string.fill((0, 255, 0))

    def draw(self):
        angle = pygame.transform.rotate(self.string, (player.orbital_angle))
        length = math.hypot(self.dx, self.dy)
        self.string = pygame.Surface((3, length))
        D.blit(angle, (self.pos[0], self.pos[1]))

string = String(600, 300)
While True:
    string.draw()

私は最初にすべてのものを持っていたdrawdebugging.Specificallyは、それが中に最後の2行である一方、differnetの機能で機能が、それはビットの乱雑を得たdraw()ことは、すなわちをクラッシュさせる窓を引き起こしています

self.string = pygame.Surface((3, length))
D.blit(angle, (self.pos[0], self.pos[1]))
Rabbid76:

位置(DEST)に引数pygame.Surface.blit()ISは2つの整数のタプルであるべきです。
あなたのケースではself.pos[0]、および/またはself.pos[1]浮動小数点数のようです。

あなたが整数座標に浮動小数点座標を(丸めて警告を取り除くことができますround):

D.blit(self.player, (round(self.pos[0]), round(self.pos[1])))

さらに、あなたは一体型の長さで表面を作成する必要があり、あなたはそれが(再)作成された後、表面を回転する必要があります。

class String:
    # [...]

    def draw(self):

        # compute INTEGRAL length        
        length = math.hypot(self.dx, self.dy)
        length = max(1, int(length))

        # create surface
        self.string = pygame.Surface((1, length)).convert_alpha()
        self.string.fill((0, 255, 0))

        # roatet surface
        angle = pygame.transform.rotate(self.string, player.orbital_angle)

        # blit rotated surface
        D.blit(angle, (round(self.pos[0]), round(self.pos[1])))

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=18546&siteId=1