• Advertisement
Sign in to follow this  

Bullet issues in Python/Pygame

This topic is 2348 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

pygame.init()

#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):
change_x=0
change_y=0

def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)

self.image = killer

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

def changespeed(self, x, y):
self.change_x+=x
self.change_y+=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):
pygame.sprite.Sprite.__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()
all_things_list.add(shot)


player = Player(0, 0)
all_things_list.add(player)

#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:
player.changespeed(3,0)
if event.key == pygame.K_UP:
player.changespeed(0,-3)
if event.key == pygame.K_DOWN:
player.changespeed(0,3)

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

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)




screen.fill(white)
screen.blit(background,(0,0))

all_things_list.draw((screen))

player.update()


clock.tick(50)

pygame.display.flip()


pygame.quit()




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
Advertisement
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.

http://codepad.org/Ckzv6ufc

Edit:
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)
update
screen fill
drawing/blitting
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?

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

Edit:
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.

http://www.pygame.org

Share this post


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

  • Advertisement