In the "def Update" section.
According to the place where the four blocks,which are tagged, are placed. The bullet just goes past the screen or stays inside the screen blinking (because it changes its x,y to -x,-y all the time)
Bullet doesn't bounce unlike in other programs I've made. I think the problem is in the position of those four blocks. Can you tell me where to fix?
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
my full code here - for you to try
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()
You are changing bullet speed in two places in your Bullet update function, one workaround could be:
# 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.