Jump to content
  • Advertisement
Sign in to follow this  

Bullet issues in Python/Pygame

This topic is 2958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright when i say "Bullet issues" i mean issues with my ability to understand the concept of getting a bullet sprite to shoot in my players direction at a certain speed when spacebar is pressed. Here is the code. For seasoned programmers there are probably obvious issues, but bare with me because i am still learning.

import pygame
import random


#Get those colors
red = (255, 0, 0)
black = ( 0, 0, 0)
white = (255,255,255)

#load player image
shot = pygame.image.load("shot.BMP")
background = pygame.image.load("background.BMP")
zombie = pygame.image.load("zombie.BMP")
killer = pygame.image.load("killer.BMP")

#load player class, because he is a sprite
class Player(pygame.sprite.Sprite):

def __init__(self, x, y):

self.image = killer

self.rect = self.image.get_rect()
self.rect.topleft = [x,y]

def changespeed(self, x, y):

def update(self):
self.rect.top += self.change_y
self.rect.left += self.change_x

def pos(self):
self.pos = self.rect.topright

class Shot(pygame.sprite.Sprite):
def __init__(self):

self.image = shot

self.rect = self.image.get_rect()

def speed(self):
self.speed = -3

def pos(self):
self.pos = player.pos

all_things_list = pygame.sprite.RenderPlain()

shot = Shot()

player = Player(0, 0)

#Make the display
screen = pygame.display.set_mode((800, 600))

pygame.display.set_caption("My Game")

score = 0

done = False

clock = pygame.time.Clock()

#--------Main Game loop!--------
while done is False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3, 0)
if event.key == pygame.K_RIGHT:
if event.key == pygame.K_UP:
if event.key == pygame.K_DOWN:

if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
if event.key == pygame.K_RIGHT:
if event.key == pygame.K_UP:
if event.key == pygame.K_DOWN:

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player.image = pygame.transform.rotate(killer, 0)
if event.key == pygame.K_a:
player.image = pygame.transform.rotate(killer, 90)
if event.key == pygame.K_s:
player.image = pygame.transform.rotate(killer, 180)
if event.key == pygame.K_d:
player.image = pygame.transform.rotate(killer, -90)

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
shot.direction = (player.position)







My question is how do i go about doing this because I am obviously lost.

Share this post

Link to post
Share on other sites
Your bullet has no velocity calculations or starting position, you're just setting the bullet's position to the player's position, which isn't calculating the movement attributes for it.

Same with your player.

You should not be subclassing your game objects themselves into sprites. You should make a Sprite class that inherits from pygame.sprite.Sprite, and assign the sprite's image to a variable such as 'image' in the sprite class, then assign this sprite to a variable in Player. Same with almost everything else in your game that isn't strictly just a sprite image. (I.e; bullets have functionality other than just being an image, same with players)

The way you're doing it right now, you're mixing data code and functionality code which I can understand doing seeing as how you're new to programming, but that's a habit that you DON'T want to get into. Your code will be a mess and you'll lose track of lots of things later on if you keep doing this.

I re-wrote some of your code. Take a look and see what changes I made that may help you on your programming adventures.


My fixes are only a small amount compared to what I would do if it were my game, but I understand you're very new. I didn't want to re-write it to the point where you wouldn't understand it.

Share this post

Link to post
Share on other sites
I changed this part of your code, not sure if you had noticed it by now or not... but, you should almost always order your main loop like this:

process events (key presses, mouse buttons, etc)
screen fill
screen flip

You were updating after you were drawing. The game will still work this way, but you'll notice quirky behavior if the update method is called after the draw method, especially once your game is more involved than this.

Also a good note to remember:
You can mix event processing code with update code and vice versa if the result warrants an update-type event, such as a change in a character attribute (like Strength or HP) with the pressing of a mouse button (this would be handled in process_events).

Share this post

Link to post
Share on other sites

What is the from pygame.locals import* part below the importing pygame thing?

it basically allows you to use pygame's constant variables without saying "pygame.locals.variable = x", you can just say "variable = x" or in your code, it allows you to say "K_LEFT" instead of having to type pygame before all of your constant key variables. It's pretty useful, and makes more more legible and easier to follow code

You should check out the pygame documentation on their website. It has tons of helpful tips and tricks and tons of relevant information for what you're doing. You'd benefit from it quite a bit.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!