简体   繁体   English

Pygame屏幕不会blit

[英]Pygame screen won't blit

I have an Agar.io like test game where the square player touches the "bit"s and grows. 我有一个类似Agar.io的测试游戏,方形玩家触及“位”并且增长。 The problem is the bit generation. 问题是比特生成。 The bit generates every second and has its own surface. 该位每秒生成并具有自己的表面。 But, the program will not blit the bit (no pun intended) into the initial surface. 但是,该程序不会将一点点(没有双关语)进入初始表面。

Here is the code: 这是代码:

import pygame
import random
pygame.init()
clock = pygame.time.Clock()
display = pygame.display
screen = display.set_mode([640,480])
rect_x = 295
rect_y = 215
display.set_caption("Agar")
d = False
bits = []
size = 50
steps = 0
class Bit:
    cscreen = display.set_mode([640,480])
    circ = pygame.draw.circle(cscreen, (65, 76, 150), (random.randrange(40, 600), random.randrange(40, 440)), 5)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            d = True
    if d == True:
        break

    # Fill background
    screen.fill((5, 58, 0))

    # Create player object
    player = pygame.draw.rect(screen, (250, 197, 255), [rect_x, rect_y, size, size])

    # Moving
    if pygame.key.get_pressed()[pygame.K_UP]:
        rect_y -= 2
    if pygame.key.get_pressed()[pygame.K_DOWN]:
        rect_y += 2
    if pygame.key.get_pressed()[pygame.K_RIGHT]:
        rect_x += 2
    if pygame.key.get_pressed()[pygame.K_LEFT]:
        rect_x -= 2

    # Bit generation
    if steps == 60:
        new_bit = Bit()
        bits.append(new_bit.circ)
        screen.blit(new_bit.cscreen, (0,0))
        steps = 0
    steps += 1

    # Collision detection
    collision = player.collidelist(bits)
    if collision != -1:
        bits[collision].cscreen.fill((0,0,0,0))
        # Make player larger
        size += 10

    # FPS limit
    clock.tick(60)

    # Refresh
    display.flip()

PS There is no error message. PS没有错误消息。

Thanks in advance. 提前致谢。

To have each Bit object have different locations and instance variables, you need to have an __init__ method in your bit class. 要使每个Bit对象具有不同的位置和实例变量,您需要在您的位类中使用__init__方法。 By adding an __init__ , and adding self. 通过添加__init__ ,并添加self. to the variables, the blitting works. 对于变量,blitting工作。

But a word of warning. 但是一句警告。 When you make a separate cscreen for each object, and then blit that cscreen to your actual screen, you get rid of every other cscreen thats been blitted, making only 1 Bit viewable at a time. 当你为每个对象制作一个单独的cscreen ,然后将cscreen blit到你的实际屏幕时,你就会摆脱所有其他被cscreen ,一次只能看到1 Bit Even if you had cscreen outside of __init__ , you would have an issue erasing older ones now, so I advise you take a different approach. 就算你cscreen__init__ ,你现在已经删除旧的一个问题,所以我建议你采取不同的方法。

My recommendation is to find a small picture of a dot, and write these lines in the Bit 's __init__ method. 我的建议是找到一个点的小图片,并在Bit__init__方法中写下这些行。

self.image = pygame.image.load("reddot.png")
self.rect = self.image.get_rect()

Once you create your bit, add it to a pygame.sprite.Group() . 创建位后,将其添加到pygame.sprite.Group() These groups have very handy built in methods. 这些组具有非常方便的内置方法。 One of them being .draw() . 其中一个是.draw() It will go through your Group of Bits and use each's self.image and self.rect to draw them in the correct place every time, without you having to worry about it. 它将通过你的Bits组并使用每个self.imageself.rect每次都在正确的位置绘制它们,而不必担心它。 When a Bit is eaten, you can delete the Bit from the Group and it is now erased for you. 当吃掉一个Bit ,您可以从该组中删除该Bit ,现在它将被删除。

A random side note: I recommend you change your event loop to just break when it event.type == pygame.QUIT . 随机附注:我建议您将事件循环更改为在event.type == pygame.QUITevent.type == pygame.QUIT That way, Python does not need to check the if d == True statement on every frame, while making the code slightly more readable. 这样,Python不需要在每一帧上检查if d == True语句,同时使代码更具可读性。 If it only breaks out of the for loop (for me it exited fine), you can use sys.exit() after importing sys 如果它只是跳出for循环(对我来说它退出正常),你可以在导入sys后使用sys.exit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM