Shooting bullets in pygame - python

I'm making my own space invaders game and so far I've been able to move my ship around using the mouse. However, I still can't shoot. Here's my game loop.
def game_loop():
x=0
y=0
xlist=[]
ylist=[]
while True:
mouseclk=pygame.mouse.get_pressed()
game_display.fill(white)
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
x, y = pygame.mouse.get_pos()
xlist.append(x)
ylist.append(y)
if x>=display_width-40:
x=display_width-40
if y>=display_height-48:
y=display_height-48
if pygame.mouse.get_focused()==0:
game_display.blit(spaceship, (x, y))
elif pygame.mouse.get_focused()==1:
game_display.blit(spaceshipflames, (x, y))
pygame.display.update()
if pygame.mouse.get_focused()==0:
pause()
clock.tick(500)
I've tried using the following code inside my game loop:
if mouseclk[0]==1:
shoot.play()
while True:
pygame.draw.circle(game_display, white, (x+20, y-2), 5)
pygame.draw.circle(game_display, red, (x+20, y-7), 5)
y-=5
if y<=0:
break
pygame.display.update()
clock.tick(400)
But the end result is very glitchy and doesn't allow me to shoot multiple bullets without making the game choppy.
Is there a way to run both loops at the same time, or a completely different way to implement shooting?

I'd recommend making use of classes (especially for games) and splitting up your code into smaller functions.
When making a game, each class should represent some type of object in the game, for example a ship or a bullet here. Using classes should help with this problem of multiple bullets causes glitches.
Breaking into smaller functions will make your code much easier to read and update as it grows. Try to stick to the Single Responsibility Principle as much as possible.
How you might implement shooting with these things in mind:
bullets = []
class Bullet:
def __init__(self, position, speed):
self.position = position
self.speed = speed
def move(self):
self.position[1] += self.speed
class Ship:
def __init__(self, position, bullet_speed):
self.position = position
self.bullet_speed = bullet_speed
def shoot(self):
new_bullet = Bullet(self.position, self.bullet_speed)
bullets.append(new_bullet)
Where the position variables have the form [x,y]. Then to move your bullets forward, put this line somewhere in your main game loop:
for bullet in bullets:
bullet.move()
And loop over all the bullets and draw each to the screen to render them.
This isn't the most detailed example, but hopefully it's enough to get you going in the right direction.

Related

Need some assistance needed with detecting multiple collisions

I created a simple program consisting of bouncy balls that bounce off the walls and are supposed to turn green upon collision with another ball. However when I add more than 2 balls to the screen, the collision detections falls apart: some balls that collide with one another don't turn green or of the balls that do collide, only one of them turns green. id appreciate it if anybody can look through my code and tell me what the issue is
I detect collisions between balls in the update method of the Ball class.
Here is the Ball class
class Ball:
def __init__(self,x,y,radius,speed,color=RED):
self.x=x
self.y=y
self.color=color
self.speed=speed
self.radius=radius
def update(self):
for i in range(0,len(ball)):
for j in range(0,len(ball)):
if i==j:
continue
elif is_collision(ball[i],ball[j]):
ball[i].color=GREEN
ball[j].color=GREEN
else:
ball[i].color=RED
ball[j].color=RED
def draw(self):
pygame.draw.circle(screen,self.color(self.x,self.y),radius)
This is the rest of the code outside of the class
def is_collision(ball1, ball2):
distance = math.hypot(ball2.x - ball1.x, ball2.y - ball1.y)
if distance<=ball1.radius+ball2.radius:
return True
else:
return False
for i in range(number_balls): # adds balls to list
rX=random.randint(radius,WIDTH-radius)
rY=random.randint(radius,HEIGHT-radius)
dx=random.randint(1,10)
dy=random.randint(1,10)
rC1=random.randint(0,255)
rC2=random.randint(0,255)
rC3=random.randint(0,255)
rColor=(rC1,rC2,rC3)
ball.append(Ball(rX,rY,25,[dx,dy]))
while True: # main loop
for events in pygame.event.get():
if events.type==pygame.QUIT:
sys.exit()
for x in ball:
x.x+=x.speed[0]
x.y+=x.speed[1]
if x.x-x.radius<0 or x.x+x.radius>WIDTH:
x.speed[0]=-x.speed[0]
if x.y-x.radius<0 or x.y+x.radius>HEIGHT:
x.speed[1]=-x.speed[1]
x.draw()
x.update()
pygame.display.update()
screen.fill(BLACK)
Assistance would be greatly appreciated

Trying to display multiple projectiles on screen

Essentially, I am trying to display multiple projectiles on the screen for the player to avoid.
At this point, I'm not worried about hit boxes or anything like that, just simply trying to get multiple projectiles to display. It displays one, then the projectile disappears off the screen and never shows back up.
I believe this may be a scope issue, but I'm not entirely sure. I have moved around pretty much every piece of code I can think of to no avail.
class Game:
clock = pygame.time.Clock()
def __init__(self):
self.enemy = Projectile()
self.enemies = []
def loop(self):
self.clock.tick(30)
for enemy in self.enemies:
if self.enemy.y < 925 and self.enemy.x < self.screen_width:
self.enemy.x += self.enemy.velocity
self.enemy.frame_count += 1
else:
self.enemies.pop(self.enemies.index(self.enemy))
if len(self.enemies) < 5:
self.enemies.append(self.enemy)
def render(self):
# other stuff
for enemy in self.enemies:
self.enemy.draw_projectile(self._display_surf)
And then in my projectile file I have:
class Projectile():
# projectile images
def __init__(self):
# stuff
def draw(self, gameDisplay):
if self.frame_count > 118:
self.frame_count = 0
display_window.blit(self.projectile_sprites[self.frame_count],(self.x, self.y))
I'm trying to get multiple projectile-type enemies on the screen. I looked over a few tutorials on how to do this type of thing and I can't get the expected results.
Few things need to be fixed. Not sure these fixs will be enough, but at least they will help.
Class Projectile seems ok sintactically. I cannot speak for the omitted parts, though.
However, you never use the gameDisplay argument in the draw function, but you have a display_window. Are they supposed to be the same thing? If so, use the same name.
I guess display_window / gameDisplay is the display, the surface initialized with pygame.display.set_mode(). If so, is fine.
For the Game class, see the comments I added.
class Game:
clock = pygame.time.Clock()
def __init__(self):
#self.enemy = Projectile() remove this line, is useless.
self.enemies = []
def loop(self):
self.clock.tick(30)
for enemy in self.enemies:
if enemy.y < 925 and enemy.x < self.screen_width:
#no need of self before enemy in this section. Otherwise you
#do not refer to the "enemy" on which you are looping but to
#the attribute "enemy" (which I removed). They are different things.
enemy.x += enemy.velocity
enemy.frame_count += 1
else:
self.enemies.pop(self.enemies.index(enemy))
#I leave this here for now, but remember that editing
#a list on which you are looping is a bad idea in general.
if len(self.enemies) < 5:
self.enemies.append(Projectile())
#each iteration you need to create a new instance of the class.
def render(self):
# other stuff
for enemy in self.enemies:
enemy.draw(self._display_surf)
#again, no self before enemy, same reason of before.
I do not know what is draw_projectile(). I suppose you meant to use the draw() method of Projectile class, it makes sense thos way.
Again self._display_surf is not defined in your code, I supposed is the display which you pass to the draw() function.

Trouble with classes in pygame

I've been working on a game using pygame. So far I have got the shooting to work.
Here is the code:
def game_loop():
global x
global y
x=0
y=0
second_x=0
second_y=0
change_x=0
change_y=0
xlist=[]
ylist=[]
# paused=True
while True:
game_display.blit(background, (0, 0))
mouseclk=pygame.mouse.get_pressed()
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
x, y = pygame.mouse.get_pos()
xlist.append(x)
ylist.append(y)
if x>=display_width-40:
x=display_width-40
if y>=display_height-48:
y=display_height-48
if event.type == pygame.MOUSEBUTTONUP :
if event.button==1:
bullets.append([x+16,y])
shoot.play()
for bullet in bullets:
game_display.blit(bulletpic, (bullet[0], bullet[1]+10))
for b in range(len(bullets)):
bullets[b][-1] -= 10
for bullet in bullets:
if bullet[1]>display_height:
bullets.remove(bullet)
mainship=Ship(spaceship, x, y, pygame.mouse.get_focused())
mainship.MakeShip()
if pygame.mouse.get_focused()==0:
pause()
pygame.display.update()
clock.tick(60)
The shooting works fine but i really want to use classes in my game. I tried making a class like this:
class Bullet:
def __init__(self, xpos, ypos, sprite, speed, llist):
self.xpos=xpos
self.ypos=ypos
self.sprite=sprite
self.speed=speed
self.list=llist
self.list.append([self.xpos+16,self.ypos])
for bullet in self.list:
game_display.blit(self.sprite, (bullet[0], bullet[1]+10))
for b in range(len(self.list)):
self.list[b][-1] -= self.speed
for bullet in self.list:
if bullet[1]>display_height:
self.list.remove(bullet)
Now, even though this looks like it would work, it just makes the bullets glitchy and slow and sometimes the bullets won't even spawn properly.
Is there any way i can make this work? I'm a noob programmer and new to classes in general so any help will be appreciated.
I think your Bullet does far too much. You could model bullets like so:
class Bullet:
"""This is a bullet. It knows about its position, direction and speed.
It can move. It knows when it crosses the classvariables bounds."""
# bounds as class variable for _all_ bullets - set it to dimensions of your game field
bounds = (0,0,10,10) # if position is lower or higher that these it is out of bounds
# if you got only 1 spite for all bullets (no acid-, fire-, waterbullets) you could
# set it here. I would not, I would leave bullets simply to do the movement and tell
# me if they get out of bounds
def __init__(self, xpos, ypos, xdir, ydir, speed):
# where does the bullet start (f.e. muzzle position)
self.xpos=xpos
self.ypos=ypos
# direction that the bullet flies
self.xdir=xdir
self.ydir=ydir
# initial speed of the bullet
self.speed=speed
def moveBullet(self):
self.xpos += int(self.xdir * self.speed) # speed may be 0.8 or so
self.ypos += int(self.ydir * self.speed)
def outOfBounds(self):
minX,minY,maxX,maxY = Bullet.bounds
return self.xpos < minX or self.ypos < minY or self.xpos > maxX or self.ypos > maxY
def __repr__(self):
"""Pretty print stuff"""
return "({},{}){}".format(self.xpos,self.ypos,"" if
not self.outOfBounds()
else " : Out Of Bounds {}".format(Bullet.bounds))
Your game then sets the bounds of all bullets and keeps track of a list of bullets. If you need a new bullet, simply place it into the list
Bullet.bounds = (0,0,90,90)
bullets = [ Bullet(10,10,12,0,3), Bullet(10,10,11,9,3), Bullet(10,10,0,5,1)]
while bullets:
# you should move this into some kind of `def moveBullets(bulletlist): ...`
# that you then call inside your game loop
for b in bullets:
b.moveBullet()
print(b) # instead draw a spirte at b.xpos, b.ypos
print("")
# return from moveBullets(..)
# remove all out of bounds bullets from list
bullets = filter(lambda b: not b.outOfBounds(),bullets)
Output:
(46,10)
(43,37)
(10,15)
(82,10)
(76,64)
(10,20)
(118,10) : Out Of Bounds (0, 0, 90, 90)
(109,91) : Out Of Bounds (0, 0, 90, 90)
(10,25)
(10,30)
(10,35)
(10,40)
(10,45)
(10,50)
(10,55)
(10,60)
(10,65)
(10,70)
(10,75)
(10,80)
(10,85)
(10,90)
(10,95) : Out Of Bounds (0, 0, 90, 90)

Pygame Sprite flickers when moving

I am new to pygame and have been having some problems with a sprite flickering/dragging it's image when it moves across the screen. I have read other answers on here about getting a certain pix/s, where you should change the framerate and lower pix/frame, but that solution hasn't helped so I was wondering if I'm just not implementing it correctly and that's the only solution or if I am missing something. Below is some the code for the game that runs at a constant framerate. Thank you!
pygame.init()
display=pygame.display.set_mode((screen_width,screen_height), pygame.RESIZABLE)
background=pygame.Surface((screen_width, screen_height))
background.fill(background_color)
class Ship(pygame.sprite.Sprite):
def __init__(self, image_file):
pygame.sprite.Sprite.__init__(self)
self.original_image_unscaled=pygame.image.load(image_file)
self.original_image=pygame.transform.smoothscale(self.original_image_unscaled, (56,70))
self.image=self.original_image
self.rect=self.image.get_rect()
self.xpos=0
self.ypos=0
self.rect.centerx=self.xpos
self.rect.centery=self.ypos
self.vel=0
self.acc=.05
self.brake=.2
self.turn_brake=1.5
self.angle=0
self.max_speed=5
def getShipPos(self):
return (self.xpos, self.ypos)
def getShipVel(self):
return self.vel
def getShipAcc(self):
return self.acc
def getShipRect(self):
return self.rect
def update(self):
self.rect.centerx=self.xpos
self.rect.centery=self.ypos
def rotateShip(self, angle):
#this rotates the ship by a specified angle
self.image, self.rect = pygameRotateImage2(self.original_image, self.rect, angle-self.angle)
self.angle+=angle
def simpleMove(self, x_final, y_final):
self.xpos=x_final
self.ypos=y_final
def moveShip(self):
keys=pygame.key.get_pressed()
xpos_i=self.xpos
ypos_i=self.ypos
if keys[K_w]:
if self.vel>self.max_speed:
self.vel=self.max_speed
else:
self.vel+=self.acc
elif keys[K_s]:
self.vel-=self.brake
if self.vel<0:
self.vel=0
if keys[K_a]:
self.rotateShip(-self.turn_brake)
elif keys[K_d]:
self.rotateShip(self.turn_brake)
self.xpos+=self.vel*math.sin(math.radians(self.angle))
self.ypos+= -self.vel*math.cos(math.radians(self.angle))
display.blit(background, (0,0))
pygame.display.update()
player_ship=Ship('image.png')
player_ship.simpleMove(screen_width/2, screen_height/2)
movement_group=pygame.sprite.Group()
movement_group.add(player_ship)
clock=pygame.time.Clock()
while True:
clock.tick(60)
(mousex,mousey)=pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
player_ship.moveShip()
movement_group.update()
display.blit(background, (0,0))
movement_group.draw(display)
pygame.display.update()
Double buffering will probably help at least some. It is a technique used in computer graphics to reduce the number of times a pixel changes. It can be implemented as:
pygame.display.set_mode(dimensions, pygame.RESIZEABLE|pygame.DOUBLEBUF)
Another option is to move the clock.tick from the beginning of the loop to the end of the loop. That is where I usually see it, and it could alter the outcome of the graphics.
Try changing the line pygame.display.update() to pygame.display.flip().
I had the same problem in the past.
I now usually create a method in the classes that need to be displayed on screen which blit the picture (of the class itself) to the screen.
I than call that method before the screen update.
For example, this is a method of the class "Tank", which actually build the tank used by the player:
def master_player(self): #calculate and draw the player sprite
self.posix() # call of a method which calculate the position
self.image_tank.set_colorkey(PURPLE)
screen.blit(player.image_tank, player.position_tuple)
As I said before, I call this method at the end of the game loop and before the screen update.
Try moving the player_ship.moveShip() right under the pygame.display.update()
Like this:
player_ship.moveShip()
movement_group.update()
display.blit(background, (0,0))
movement_group.draw(display)
pygame.display.update()

Pygame Inquiry- Generating Multiple Sprites in Same Class

Unimportant Preamble:
Hello, I'm using Python and Pygame to create a game. This is for the purpose of improving my programming skills rather than a serious attempt at game creation. I've taken a break from Python lately for Objective C, but I'm now returning to it. This is a problem that I was having before I took a brief break, and I've returned to a question that was originally puzzling me. I've spent quite a while researching it, and I have a suspicion I've come across the solution and merely failed to understand it. I apologize for some of the bad naming conventions/lack of comments. I'm working on improving that.
Substance of Question:
Anyway, I've attached the four images I'm using. The program uses a simple function to position various Tiles on the screen. The mouse cursor is a sword. It is the entire image, but I'll be changing that later. I've made the program type "blue" in the shell whenever the cursor collides with a Tile. My goal is to have this happen when it collides with "ANY" tile of that color.
Long-term, I want to be able to modify the properties of these tile sprites. Various game-pieces would interact, and I would need to save the state of each sprite. I'd also be setting interactions for the other sprites.
Right now the sprites are all generating images, but my collision rectangle for the Tile is simply moving after each image is generated. I suppose that makes sense given the code, but I need a way to multiple sprites, each with a rectangle for collision.
Thanks
EDIT: I was unable to add images due to a new-user restriction. They are available enter link description here I think I read somewhere that people can (and do) edit posts here. So if anyone who the ability to move the images into this thread is welcome to do so.
import random,math,sys,os
import pygame
from pygame.locals import *
pygame.init() #Initializing Pygame
#Colors
black=(0,0,0)
#Screen
screen=pygame.display.set_mode((1200,800),0,0)
pygame.display.set_caption("Nero's Sandbox")
pygame.mouse.set_visible(False)
clock=pygame.time.Clock()
fps=40
#Game Functions:
def terminate():
pygame.quit()
sys.exit()
def numgen(x,y):
return random.randint(x,y)
#Loop Variables
tri=2
#Groups:
allsprites = pygame.sprite.Group()
alltiles = pygame.sprite.Group()
allmice = pygame.sprite.Group()
#Mouse Classes
class Pointy(pygame.sprite.DirtySprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('redsword.png').convert() #31x32 image
self.image.set_colorkey(black)
self.rect=self.image.get_rect()
self.set=pygame.sprite.Group()
self.add(allmice, allsprites, self.set)
pygame.sprite.RenderPlain((self.set,allmice,allsprites))
def update(self):
screen.fill(black)
alltiles.draw(screen)
if event.type == pygame.MOUSEMOTION:
pos = pygame.mouse.get_pos()
self.rect.topright = pos
self.set.draw(screen)
#Tile Sprites - only one rect is being recognized.
class Tile(pygame.sprite.Sprite):
def __init__(self, graphic):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(graphic).convert()
self.image = pygame.transform.scale((self.image),(50,50))
self.rect=self.image.get_rect()
self.add(alltiles, allsprites)
self.set=pygame.sprite.RenderPlain((self))
def update(self, x, y):
pos = (x,y)
self.rect.topleft = pos
#Micers
pointy1=Pointy()
#Game Loops
while True: #Ensures all loops within program are constantly called when conditions are met.
screen.fill(black)
while tri==2:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
pygame.display.flip()
x = 0
y = 50
w = 0
while x!=600:
x=x+50
w = w+1
if w%2==0:
purpletile1=Tile('purplesquare.png')
purpletile1.set.update(x,y)
purpletile1.set.draw(screen)
else:
c=numgen(1,2)
if c==1:
bluetile1=Tile('lightbluesquare.png')
bluetile1.set.update(x,y)
bluetile1.set.draw(screen)
if c==2:
redtile1=Tile('redsquare.png')
redtile1.set.update(x,y)
redtile1.set.draw(screen)
if x>=600 and y!=450:
if y<450:
x = 0
y = y+50
w=w-1
if y>=450:
tri=3
while tri==3:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
alltiles.draw(screen)
pointy1.set.update()
pointy1.set.draw(screen)
pygame.display.flip()
clock.tick(fps)
if pygame.sprite.collide_rect(pointy1,bluetile1):
print('blue')
I had this same problem myself! I did some debugging, and it appeared that all instances of my class shared the same instance of pygame.Rect()
You may want to change the line:
pygame.sprite.Sprite.__init__(self)
to
super.__init__(self)
This way, pygame.sprite.Sprite's init will set attributes to your tile. I could be wrong, I'm not entirely familiar with python's inheritance syntax, but that is the way I do it.
I could also be the get_rect that is causing the same rectangle to be used for all classes, but that doesn't seem to be likely.
I hope that I was some help, and just remember that pygame.Rect is an object, so somehow you are instantiating only once.

Categories

Resources