过去一周左右我一直在研究太空入侵者类型的游戏,而且它的进展非常顺利,但出于某种原因,当我在我的Windows计算机上运行游戏时(我更愿意在其上编码) ),大多数精灵都没有出现,或只是在他们在屏幕上朝某个方向移动时出现。当我在我的Mac上运行完全相同的代码时,所有内容都会完美地更新。这是一个已知的问题,pygame只是与Windows不相处?一切都在我的Mac上运行的事实使我认为这不是我的代码的问题,但以防万一,这是我的主循环:
dirty = []
score_label = font.render("Score: " + str(score), 1, (255,215,0))
level_label = font.render("Level: " + str(level), 1, (255,215,0))
screen.blit(background,(10,10),(10,10,330,30))
screen.blit(score_label,(10,10))
screen.blit(level_label,(200,10))
dirty.append((10,10,330,30))
# New level
if isEmpty(enemies):
dirty.append((400,300,150,30))
for i in bullets:
dirty.append(i.pos)
screen.blit(background,(i.pos[0],i.pos[1]),i.pos)
bullets.remove(i)
if pauseCounter == 40:
screen.blit(background,(400,300),(400,300,150,30))
pauseCounter = 0
jump = 30
level+=1
for x in range(1,6):
for y in range(11):
enemies[x-1].append(Sprites.Enemy(enemy_values[6-x],(60*y,40*x),6-x))
for x in range(len(enemies)):
for e in enemies[x]:
dirty.append(e.pos)
else:
upLevelLabel = levelFont.render("Next Level!",1,(255,215,0))
screen.blit(upLevelLabel,(400,300))
pauseCounter += 1
# Player input
dirty.append(o.pos)
screen.blit(background,(o.pos[0],o.pos[1]),o.pos)
keys = pygame.key.get_pressed()
if keys[K_a] and o.pos[0]-10 >= 0:
o.speed = -10
o.move()
if keys[K_d] and o.pos[0] <= 907-o.pos[3]-10:
o.speed = 10
o.move()
dirty.append(o.pos)
screen.blit(o.image,o.pos)
# Add bullets
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == K_SPACE:
bullets.append(Sprites.Bullet(bullet,(o.pos[0]+28,o.pos[1])))
# Updates bunkers
for b in bunkers:
screen.blit(b.image,b.pos)
if b.health == 0:
screen.blit(background,(b.pos[0],b.pos[1]),b.pos)
dirty.append(b.pos)
bunkers.remove(b)
# Check if bullets are offscreen/collide, move accordingly
for b in bullets:
dirty.append(b.pos)
if b.pos[1] < 0:
screen.blit(background,(b.pos[0],b.pos[1]),b.pos)
dirty.append(b.pos)
bullets.remove(b)
if isEmpty(enemies) == False:
for x in range(len(enemies)):
if len(enemies[x]) > 0:
temp = enemies[x][0]
if b.pos[1] in range(temp.pos[1],temp.pos[1]+temp.pos[3]):
for e in enemies[x]:
if b.pos.colliderect(e.pos):
score += e.val*10
screen.blit(background,(b.pos[0],b.pos[1]),b.pos)
screen.blit(background,(e.pos[0],e.pos[1]),e.pos)
dirty.append(e.pos)
dirty.append(b.pos)
bullets.remove(b)
enemies[x].remove(e)
for i in bunkers:
if b.pos.colliderect(i.pos):
screen.blit(background,(b.pos[0],b.pos[1]),b.pos)
dirty.append(b.pos)
i.health -= 1
bullets.remove(b)
for b in bullets:
dirty.append(b.pos)
screen.blit(background,(b.pos[0],b.pos[1]),b.pos)
b.move()
screen.blit(b.image,b.pos)
dirty.append(b.pos)
# Moves enemy at appropriate pace, drops bombs
if enemyMoves == jump:
moveCounter += 1
if moveCounter == 20:
jump = jump/2
moveCounter = 0
enemyMoves = 0
for x in range(len(enemies)):
for e in enemies[x]:
dirty.append(e.pos)
screen.blit(background,(e.pos[0],e.pos[1]),e.pos)
e.move()
screen.blit(e.image,e.pos)
dirty.append(e.pos)
else:
for x in range(len(enemies)):
for e in enemies[x]:
screen.blit(e.image,e.pos)
enemyMoves+=1
pygame.display.update(dirty)
clock.tick(30)
print clock.get_fps()
提前致谢!
答案 0 :(得分:0)
也许你应该尝试:
dirty.append(Rect(x,y,width,height))
或
dirty.append(pygame.Rect(x,y,width,height))
请记住,您正在将一个矩形列表传递给pygame.display.update(dirty)
,因此请尝试坚持使用pygame矩形
而不是:
dirty.append((x,y,width,height))
答案 1 :(得分:0)
感谢furas,这是导致错误的部分更新!