使用函数关闭pygame窗口 [英] Using a function closes pygame window

查看:72
本文介绍了使用函数关闭pygame窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是 String 类.在我的主循环中使用此类的draw函数可以立即关闭游戏,而不会出现任何错误,并且只要我不添加它,游戏就可以正常运行.它确实给了我以下警告.

The following is the String class. Using the draw function from this class in my main loop immediately closes the game without any errors and as long i dont include it, the game runs fine. It does give me the following warning.

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

起初我在differnet函数的 draw 函数中拥有所有内容,但在调试时有点混乱.特别是,这是导致 draw()中的最后两行崩溃的窗口,即

I initially had everything in draw function in differnet functions but it got a bit messy while debugging.Specifically, it is the last two lines in draw() that's causing the window to crash i.e

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

推荐答案

The position (dest) argument to pygame.Surface.blit() is ought to be a tuple of 2 integral numbers.
In your case self.pos[0] and/or self.pos[1] seems to be a floating point number.

您可以通过将浮点坐标四舍五入为整数坐标来消除警告(

You can get rid of the warning by rounding the floating point coordinates to integral coordinates (round):

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

为了完整起见,必须提到该参数也可以是矩形.具有四个组成部分(左,顶部,宽度,高度)的元组.

For the sake of completeness it has to be mentioned that the argument can also be a rectangle. A tuple with the 4 components (left, top, width, height).

此外,您还创建了一个具有整数长度的Surface,并且必须在(重新)创建它之后旋转该表面:

Furthermore you have create a Surface with an integral length and you have to rotate the surface after it is (re)created:

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

这篇关于使用函数关闭pygame窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆