'Shoot sprite more then once and not reset it?
I have looked over a lot of sites but I have only found stuff that was either much over my head or not aplicable to my situation. I want to be able to shoot a sprite out of my tank, I've got that working, but it fires and then I have to wait till it hits something and resets before I can shoot again without messing up my shot.
I've seen stuff with list and things but I couldn't undestand it or how they 'fixed' their problems.
My code is alittle messy but I'll try and make it understandable
Oh, and I'm new here:>
import pgzrun
import pygame
import random
"""setting up sprites"""
ptank = Actor('tank_a', anchor=('center', 'middle'))
ptmissile = Actor('shot_c', anchor=('center', 'middle'))
etank = Actor('tank_bbb', anchor=('center', 'top'))
ptshot = Actor('shot_cc', anchor=('center', 'middle'))
ptank.topright = 550, 800
etank.topright = 550, 200
ptmissile.topright = ptank.x, ptank.y
ptshot.topright = ptank.x, ptank.y
WIDTH = 1000
HEIGHT = 1000
"""global variables for gamplay"""
global pscore
pscore = 0
global escore
escore = 0
global ehealth
ehealth = 10
global phealth
phealth = 10
global shotdirect
shotdirect = 1
global shoot
shoot = 0
global missile
missile = 0
global gcheck
gcheck = 1
global graphics
graphics = 2
"""functions for gameplay"""
def follow():
if ptmissile.y < etank.y:
ptmissile.y += 1
elif ptmissile.y > etank.y:
ptmissile.y -= 1
if ptmissile.x < etank.x:
ptmissile.x += 1
elif ptmissile.x > etank.x:
ptmissile.x -= 1
def missile_reset():
global missile
missile = 0
ptmissile.topright = ptank.x, ptank.y
def shot_reset():
global shoot
shoot = 0
ptshot.topright = ptank.x, ptank.y
def shot_right():
ptshot.x += 10
def shot_left():
ptshot.x -= 10
def shot_up():
ptshot.y -= 10
def shot_down():
ptshot.y += 10
def missile_to():
i = 0
while i < 20:
follow()
i += 1
def on_mouse_down(pos):
global graphics
if ptank.collidepoint(pos) or etank.collidepoint(pos):
gcheck = 0
if ptank.collidepoint(pos):
graphics = 0
gcheck = 0
elif etank.collidepoint(pos):
graphics = 1
gcheck = 0
"""drawing sprites"""
def draw():
global missile
global gcheck
global graphics
global shoot
screen.clear()
if gcheck == 1:
screen.draw.text("Graphics level: click on low: you, or high: enemy", [500, 500])
if graphics == 1:
screen.blit('background_a', (0, 0))
if graphics == 0:
gcheck = 0
ptank.draw()
etank.draw()
if missile == 1:
ptmissile.draw()
if shoot == 1:
ptshot.draw()
"""main code"""
def update():
global missile
global shoot
global graphics
global gcheck
global shotdirect
if shoot == 1:
if shotdirect == 1:
shot_up()
if shotdirect == 2:
shot_right()
if shotdirect == 3:
shot_down()
if shotdirect == 4:
shot_left()
if ptshot.x < 0:
shot_reset()
shoot = 0
if ptshot.x > WIDTH:
shot_reset()
shoot = 0
if ptshot.y < 0:
shot_reset()
shoot = 0
if ptshot.y > HEIGHT:
shot_reset()
shoot = 0
if ptmissile.collidepoint(etank.x, etank.y):
missile_reset()
missile = 0
sounds.explosion_b.play()
if missile == 1:
missile_to()
if keyboard.tab:
missile_reset()
missile = 1
print(str(missile))
if ptshot.collidepoint(etank.x, etank.y):
shot_reset()
shoot = 0
sounds.explosion_b.play()
if keyboard.space:
shoot = 0
shot_reset()
shoot = 1
print(str(shoot))
if keyboard.left:
ptank.angle = 90
ptank.x -=10
if shoot == 0:
shotdirect = 4
if keyboard.right:
ptank.angle = 270
ptank.x +=10
if shoot == 0:
shotdirect = 2
if keyboard.up:
ptank.angle = 360
ptank.y -=10
if shoot == 0:
shotdirect = 1
if keyboard.down:
ptank.angle = 180
ptank.y +=10
if shoot == 0:
shotdirect = 3
"""this isn't the best way to do it but I've started fixing it (see: above; using angle)"""
if keyboard.a:
etank.image = 'tank_bbbb'
etank.x -=10
if keyboard.d:
etank.image = 'tank_bb'
etank.x +=10
if keyboard.w:
etank.image = 'tank_b'
etank.y -=10
if keyboard.s:
etank.image = 'tank_bbb'
etank.y +=10
pgzrun.go()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
