[英]Pygame screen won't blit
我有一個類似Agar.io的測試游戲,方形玩家觸及“位”並且增長。 問題是比特生成。 該位每秒生成並具有自己的表面。 但是,該程序不會將一點點(沒有雙關語)進入初始表面。
這是代碼:
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沒有錯誤消息。
提前致謝。
要使每個Bit
對象具有不同的位置和實例變量,您需要在您的位類中使用__init__
方法。 通過添加__init__
,並添加self.
對於變量,blitting工作。
但是一句警告。 當你為每個對象制作一個單獨的cscreen
,然后將cscreen
blit到你的實際屏幕時,你就會擺脫所有其他被cscreen
,一次只能看到1 Bit
。 就算你cscreen
外__init__
,你現在已經刪除舊的一個問題,所以我建議你采取不同的方法。
我的建議是找到一個點的小圖片,並在Bit
的__init__
方法中寫下這些行。
self.image = pygame.image.load("reddot.png")
self.rect = self.image.get_rect()
創建位后,將其添加到pygame.sprite.Group()
。 這些組具有非常方便的內置方法。 其中一個是.draw()
。 它將通過你的Bits
組並使用每個self.image
和self.rect
每次都在正確的位置繪制它們,而不必擔心它。 當吃掉一個Bit
,您可以從該組中刪除該Bit
,現在它將被刪除。
隨機附注:我建議您將事件循環更改為在event.type == pygame.QUIT
時event.type == pygame.QUIT
。 這樣,Python不需要在每一幀上檢查if d == True
語句,同時使代碼更具可讀性。 如果它只是跳出for循環(對我來說它退出正常),你可以在導入sys
后使用sys.exit()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.