子弹不会在pygame

时间:2018-06-16 02:12:15

标签: python pygame

在" def Update"部分。

根据标记的四个块放置的位置。子弹刚刚越过屏幕或停留在屏幕内闪烁(因为它将x,y更改为-x,-y始终为

) 与其他我已制作的节目不同,子弹没有反弹。我认为问题在于这四个街区的位置。你能告诉我在哪里修理吗?

class Bullet(pygame.sprite.Sprite):
    def __init__(self,centerx,centery,c):
        ...

    def update(self):
        a= pygame.time.get_ticks()
        self.b.append(a)

        #point and shoot
        if self.point == 1:
            self.yspeed = -10
            self.xspeed = 0
        if self.point == 2:
            self.yspeed = 10
            self.xspeed = 0
        if self.point == 3:
            self.yspeed = 0
            self.xspeed = -10
        if self.point == 4: 
            self.yspeed = 0
            self.xspeed = 10

        #bounce
        if self.rect.right>=WIDTH or self.rect.left<=0:
            self.xspeed = -self.xspeed
        if self.rect.top<=0 or self.rect.bottom>=HEIGHT:
            self.yspeed = -self.yspeed

        #kill after 5 sec
        if (a-self.b[0])>=5000:
            del self.b[:]
            self.kill()

        #move according to speed
        self.rect.centerx += self.xspeed
        self.rect.centery += self.yspeed

我的完整代码 - 供你试用

import pygame
import random
from os import path

WIDTH = 800
HEIGHT = 600
FPS = 60

BLACK = (0,0,0)
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
light_BLUE = (0,255,255)

pygame.init()
pygame.mixer.init()
screen= pygame.display.set_mode((WIDTH,HEIGHT))
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30,30))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH/2,HEIGHT/2)
        self.speed = 0
        self.y_speed = 0

    def update(self):
        self.speed = 0
        self.y_speed = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.speed = -3
        if keystate[pygame.K_RIGHT]:
            self.speed = 3
        if keystate[pygame.K_UP]:
            self.y_speed = -3
        if keystate[pygame.K_DOWN]:
            self.y_speed = 3
        self.rect.x+=self.speed
        self.rect.y+=self.y_speed

class Gun(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10,10))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH/2,HEIGHT/2)
        self.a = 0
        self.b = 0
        self.c = 0

    def update(self,player_Cposition):
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_UP]:
            self.a = player_Cposition[0]
            self.b = player_Cposition[1]-2*self.rect.height
            self.c = 1
        if keystate[pygame.K_DOWN]:
            self.a = player_Cposition[0]
            self.b = player_Cposition[1]+2*self.rect.height
            self.c = 2
        if keystate[pygame.K_LEFT]:
            self.a = player_Cposition[0]-2*self.rect.width
            self.b = player_Cposition[1]
            self.c = 3
        if keystate[pygame.K_RIGHT]:
            self.a = player_Cposition[0]+2*self.rect.width
            self.b = player_Cposition[1]
            self.c = 4
        self.rect.center = [self.a,self.b]

    def shoot(self,x,y,c):
        bullet = Bullet(x,y,c)
        all_sprites.add(bullet)

class Bullet(pygame.sprite.Sprite):
    def __init__(self,centerx,centery,c):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((8,8))
        self.image.fill(light_BLUE)
        self.rect = self.image.get_rect()
        self.rect.center = [centerx,centery]
        self.point = c
        self.xspeed = 0
        self.yspeed = 0
        self.b = []

    def update(self):
        a= pygame.time.get_ticks()
        self.b.append(a)
        #point and shoot
        if self.point == 1:
            self.yspeed = -10
            self.xspeed = 0
        if self.point == 2:
            self.yspeed = 10
            self.xspeed = 0
        if self.point == 3:
            self.yspeed = 0
            self.xspeed = -10
        if self.point == 4: 
            self.yspeed = 0
            self.xspeed = 10

        #bounce
        if self.rect.right>=WIDTH or self.rect.left<=0:
            self.xspeed = -self.xspeed
        if self.rect.top<=0 or self.rect.bottom>=HEIGHT:
            self.yspeed = -self.yspeed

        #kill after 5 sec
        if (a-self.b[0])>=5000:
            del self.b[:]
            self.kill()

        #move according to speed
        self.rect.centerx += self.xspeed
        self.rect.centery += self.yspeed

all_sprites = pygame.sprite.Group()
player = Player()
gun = Gun()
all_sprites.add(player)
gun_sprites = pygame.sprite.Group()
gun_sprites.add(gun)

running = True
while running:
    #event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                gun.shoot(gun.a,gun.b,gun.c)
    #update
    all_sprites.update()
    gun_sprites.update(player.rect.center)
    #draw
    screen.fill(BLACK)
    all_sprites.draw(screen)
    gun_sprites.draw(screen)
    pygame.display.update()
    #clock
    clock.tick(FPS)
pygame.quit()

1 个答案:

答案 0 :(得分:1)

您正在Bullet更新功能中的两个位置更改项目符号速度,一种解决方法 可能是:

# bounce
    if self.rect.right >= WIDTH or self.rect.left <= 0:
        self.xspeed = -self.xspeed
        self.point = 0
    if self.rect.top <= 0 or self.rect.bottom >= HEIGHT:
        self.yspeed = -self.yspeed
        self.point = 0