python - top down shooter zombie survival how to get the player to shoot? -
would please tell me how bullet class/variable working cause error "'bullet' object not callable".
heres bullet class
class bullet(pygame.sprite.sprite): def __init__(self): super().__init__() self.image = pygame.surface([3,3]) self.image.fill(brown) self.rect = self.image.get_rect() self.rect.center = body.rect.center self.tick = true self.ticknum = 0 self.ticktarget = 10 def update(self): if self.ticknum == self.ticktarget: self.tick = true else: self.ticknum += 1 self.velx = mousex/20 self.vely = mousey/20 self.rect.x += self.velx self.rect.y += self.vely
heres bit calls , makes shoot
while true: if buttondown: bullet = bullet() if bullet.tick == true: bullet_list.add(bullet) all_sprites_list.add(bullet) bullet.tick = false
heres entire code:
import pygame, sys, time, math pygame.locals import * pygame.init() pygame.display.set_caption("###zombie survival###") fps = 60 clock = pygame.time.clock() dpwidth = 1500 dpheight = 800 middlex = int(dpwidth/2) middley = int(dpheight/2) middle = middlex,middley dp = pygame.display.set_mode((dpwidth,dpheight)) mousex = 0 mousey = 0 mouse = mousex,mousey buttondown = false black = 0,0,0 white = 255,255,255 purple = 255,0,255 lightblue = 170,190,255 blue = 0,0,255 red = 255,0,0 brown = 85,65,0 green = 0,100,0 all_sprites_list = pygame.sprite.group() zombie_list = pygame.sprite.group() bullet_list = pygame.sprite.group() block_list = pygame.sprite.group() player_list = pygame.sprite.group() load = pygame.image.load class legs(pygame.sprite.sprite): def __init__(self): super().__init__() self.image = load("player/player_legs_1.png") self.rect = self.image.get_rect() self.rect.center = middle self.animate = false self.timenum = 0 self.newimg = 1 self.timetarget = 8 self.rootimg = "player/player_legs_" def update(self): updateplayer(self) class body(pygame.sprite.sprite): def __init__(self): super().__init__() self.image = load("player/player_gun_1.png") self.rect = self.image.get_rect() self.rect.center = middle self.animate = false self.timenum = 0 self.newimg = 1 self.timetarget = 5 self.rootimg = "player/player_gun_" def update(self): updateplayer(self) class bullet(pygame.sprite.sprite): def __init__(self): super().__init__() self.image = pygame.surface([3,3]) self.image.fill(brown) self.rect = self.image.get_rect() self.rect.center = body.rect.center self.tick = true self.ticknum = 0 self.ticktarget = 10 def update(self): if self.ticknum == self.ticktarget: self.tick = true else: self.ticknum += 1 self.velx = mousex/20 self.vely = mousey/20 self.rect.x += self.velx self.rect.y += self.vely ##class zombie(pygame.sprite.sprite): ## ##class block(pygame.sprite.sprite): def basic(): global mousex global mousey global mouse global buttondown event in pygame.event.get(): if event.type == mousemotion: mousey = event.pos[1] mousex = event.pos[0] mouse = mousex,mousey if event.type == mousebuttondown: buttondown = true elif event.type == mousebuttonup: buttondown = false elif event.type == quit: pygame.quit() exit curser() def curser(): pygame.draw.circle(dp,black,mouse,10,2) pygame.draw.circle(dp,black,mouse,3) def rotateplayer(class): playerxdif = mousex-middlex playerydif = mousey-middley dif = (playerxdif),(playerydif) rotdeg1 = math.atan2(dif[0],dif[1]) rotdeg2 = math.degrees(rotdeg1) img1 = rotateimage(class.image,rotdeg2) return img1 def rotateimage(img,angle): orig_rect = img.get_rect() rot_img = pygame.transform.rotate(img,angle) rot_rect = orig_rect.copy() rot_rect.center = rot_img.get_rect().center rot_img = rot_img.subsurface(rot_rect).copy() return rot_img def updateplayer(class): class.newtimetarget = int(fps/class.timetarget) if class.animate == true: class.timenum += 1 if class.timenum == self.newtimetarget: if class.newimg != self.maximg: class.newimg += 1 else: class.newimg = 1 class.animate = false class.timenum = 0 else: class.newimg = 1 class.image = load(str(class.rootimg+str(class.newimg)+".png")) class.image = rotateplayer(class) legs = legs() body = body() player_list.add([body,legs]) all_sprites_list.add([body,legs]) while true: if buttondown: bullet = bullet() if bullet.tick == true: bullet_list.add(bullet) all_sprites_list.add(bullet) bullet.tick = false dp.fill(green) basic() all_sprites_list.update() all_sprites_list.draw(dp) clock.tick(fps) #print(mouse) pygame.display.update()
bullet = bullet()
problem. you're doing nasty things namespace. instead, bullet = bullet()
or b = bullet()
Comments
Post a Comment