为什么我的物体冻结了?

时间:2017-10-19 21:45:22

标签: python pygame

我正在使用Python制作太空入侵者的版本,但出于某种原因,我的外星人或玩家都没有弹出。如果我按空间它们会出现,但在那里,它们只是被冻结了。它发生在我添加我的碰撞代码后,但我不知道它为什么会发生。帮助将不胜感激。这是我的代码:`

import random
import pygame
import sys
import time
#Setting up pygame
pygame.init()
shooting = False

goingright = 1
keys = [False,False,False,False]
clock = pygame.time.Clock()
screen = pygame.display.set_mode([500,500])
font = pygame.font.Font(None,50)
#Creating class for player
class Player:
    def __init__(self,x,y,width,height):
        self.x = x 
        self.y = y
        self.width = width
        self.height = height
    def draw(self):
        pygame.draw.rect(screen,[0,255,0],[int(self.x),int(self.y),int(self.width),int(self.height)],0)
    def move(self):
            if keys[1] == True:
                self.x -= 1
            elif keys[3] == True:
                self.x += 1

            if self.x < 0:
                print(self.x)
                self.x = 0
            if self.x > 500 - self.width:
                print(self.x)
                self.x = 500 - self.width
    def shoot(self):
        return
class Bullet():
    def __init__(self,x,y):
        self.x = x 
        self.y = y
    def update(self,y_amount = 5):
         self.y -= y_amount
         return
    def draw(self):
        self.rect = pygame.draw.rect(screen,[0,255,0],[int(self.x),int(self.y),10,50],0)


class Alien():
    def __init__(self,x,y,goingright,dead):
        self.x = x
        self.y = y

        self.goingright = goingright
        self.dead = dead
    def draw(self):
         self.rect = pygame.draw.rect(screen,[0,255,0],[int(self.x),int(self.y),25,25],0)

    def move(self):

        if self.goingright == True:
            self.x += 0.1
            if self.x > 500 - 25:
                self.y+=25
                self.x = 475
                self.goingright = False
        elif self.goingright == False:
            self.x -= 0.1
            if self.x < 0:
                self.y+=25
                self.x = 0
                self.goingright = True




bullets = []        
aliens = []  


#Creating a player

player = Player(200,450,40,20)
for i in range(100,800,75):
    aliens.append(Alien(i,100,True,False))


#Main Loop
while True:

    #Background
    screen.fill([0,0,0])
    #Letting Player move
    player.move()
    #Drawing Player
    player.draw()
    #Updating screen

    #Checking for events
    for event in pygame.event.get():

        #Checking for quit   
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:

            #Checking for keys
            if event.key == pygame.K_w:
                keys[0] = True
            elif event.key == pygame.K_a:
                keys[1]=True
            elif event.key == pygame.K_s:
                keys[2]=True
            elif event.key == pygame.K_d:
                keys[3]=True
            elif event.key == pygame.K_SPACE:
                bullets.append(Bullet(player.x, player.y))



        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                keys[0]=False
            elif event.key == pygame.K_a:
                keys[1]=False
            elif event.key == pygame.K_s:
                keys[2]=False
            elif event.key == pygame.K_d:
                keys[3]=False
            elif event.key == pygame.K_SPACE:
                shooting = True
                shooting = False
                c = 0














    for bullet in bullets:
        bullet.draw()
        bullet.update()
    for alien in aliens:
        alien.draw()
        if alien.dead == False:
           alien.move()
    for alien in aliens:

        for bullet in bullets:    

           if alien.x < bullet.x + 10 and alien.x + 25 > bullet.x and alien.y < bullet.y + 50 and 25 + alien.y > bullet.y:
               pygame.display.flip()

`

1 个答案:

答案 0 :(得分:2)

必须调用

pygame.display.flip()来刷新场景。由于只有在满足一系列条件时才会这样做,即使外星人,子弹和玩家位置/状态正在更新,屏幕也不会刷新。

如果我没有弄错,那么狭窄的条件是碰撞检测,应该将外星人的状态更新为死亡。您还需要将flip调用移动到主while True:循环中(python范围对选项卡敏感)