Traceback Help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: Traceback Help (/thread-2712.html) |
Traceback Help - Low_Ki_ - Apr-04-2017 I have 5 files that tie together to create a simple alien invaders type game. While adding the bullet function I've made a mistake somewhere and cannot find it. The game starts up just fine and the ship is able to move_left and move_right. When the K_SPACE is depressed the game crashes and leaves a traceback which is hard to follow. This is all the code from each file and the traceback: alien_invasion.py: import pygame from settings import Settings from ship import Ship import game_functions as gf from pygame.sprite import Group def run_game(): # Initialize pygame, settings, and create a screen obj. pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # Make ship. ship = Ship(ai_settings, screen) # Make bullet group to store bullets in. bullets = Group() # Start the main loop for game. while True: gf.check_events(ai_settings, screen, ship, bullets) ship.update() bullets.update() gf.update_screen(ai_settings, screen, ship, bullets) run_game()game_functions.py: import sys import pygame from bullet import Bullet def check_keydown_events(event, ai_settings, screen, ship, bullets): if event.key == pygame.K_RIGHT: ship.moving_right = True elif event.key == pygame.K_SPACE: new_bullet = Bullet(ai_settings, screen, ship) bullets.add(new_bullet) elif event.key == pygame.K_LEFT: ship.moving_left = True def check_keyup_events(event, ship): if event.key == pygame.K_RIGHT: ship.moving_right = False elif event.key == pygame.K_LEFT: ship.moving_left = False def check_events(ai_settings, screen, ship, bullets): """ Respond to keypresses and moust events. """ # Watch for keyboard and mouse events. for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: check_keydown_events(event, ai_settings, screen, ship, bullets) elif event.type == pygame.KEYUP: check_keyup_events(event, ship) def update_screen(ai_settings, screen, ship, bullets): """ Update images on the screen. """ # Redraw all bullets behind ship and aliens. for bullet in bullets.sprites(): bullet.draw_bullet() # Redraw the screen during each pass through the loop. screen.fill(ai_settings.bg_color) ship.blitme() # Make most recently drawn screen visible. pygame.display.flip()settings.py: class Settings(): """ A class to store all settings for Alien Invasion. """ def __init__(self): """ Initialize the game's settings. """ # Screen settings self.screen_width = 1200 self.screen_height = 700 self.bg_color = (230, 230, 230) # Ship settings self.ship_speed_factor = 1.5 # Bullet settings self.bullet_speed_factor = 1 self.bullet_width = 3 self.bullet_height = 15 self.bullet_color = 60, 60, 60bullet.py: import pygame from pygame.sprite import Sprite class Bullet(Sprite): def __init(self, ai_settings, screen, ship): """ Create a bullet object at the ships current position. """ super(Bullet, self).__init__() self.screen = screen # Create a bullet rect at (0, 0) and then set correct position. self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height) self.rect.centerx = ship.rect.centerx self.rect.top = ship.rect.top # Store bullets position as a decimal value. self.y = float(self.rect.y) self.color = ai_settings.bullet_color self.speed_factor = ai_settings.bullet_speed_factor def update(self): """Move the bullet up the screen.""" # Update the decimal position of the bullet. self.y -= self.speed_factor # Update the rect position. self.rect.y = self.y def draw_bullet(self): """Draw the bullet to the screen.""" pygame.draw.rect(self.screen, self.color, self.rect)ship.py: import pygame class Ship(): def __init__(self, ai_settings, screen): """ Initialize the ship and set its starting position. """ self.screen = screen self.ai_settings = ai_settings # Load the ship image and get its rect. self.image = pygame.image.load('images/ship.bmp') self.rect = self.image.get_rect() self.screen_rect = screen.get_rect() # Start each new ship at the bottom center of the screen. self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom self.center = float(self.rect.centerx) # Movement flag self.moving_right = False self.moving_left = False def update(self): """ Update ships position based on the movement flag. """ if self.moving_right and self.rect.right < self.screen_rect.right: self.center += self.ai_settings.ship_speed_factor if self.moving_left and self.rect.left > 0: self.center -= self.ai_settings.ship_speed_factor self.rect.centerx = self.center def blitme(self): """ Draw the ship at its current location. """ self.screen.blit(self.image, self.rect)Traceback: I understand this is a lot of code to go through and could take some time to help find the error. Any help is very much appreciated. Thank you in advance.
RE: Traceback Help - metulburr - Apr-04-2017 your Bullet dunder init method is typo'd Quote: def __init(self, ai_settings, screen, ship):If you have this many files that also require resources, you need to upload all your code and any resources the code needs (images, etc.) to github. You can can simply follow this tutorial https://python-forum.io/Thread-Basic-Creating-a-repo-for-your-completed-scripts RE: Traceback Help - Low_Ki_ - Apr-10-2017 (Apr-04-2017, 07:28 PM)metulburr Wrote: your Bullet dunder init method is typo'd Ok, everything has been uploaded to git https://github.com/Low-Ki/alien_invasion.git RE: Traceback Help - metulburr - Apr-10-2017 like i said you typo'd your dunder init method. That is why you are getting that error https://github.com/Low-Ki/alien_invasion/pull/2/commits/6395e2ec5b62477eaaa08af95c4e8e3ba6f6c0bb RE: Traceback Help - Low_Ki_ - Apr-10-2017 (Apr-10-2017, 04:16 PM)metulburr Wrote: like i said you typo'd your dunder init method. That is why you are getting that error Ahhh lol. Now the bullet does not draw to the screen when K_SPACE is depressed. RE: Traceback Help - metulburr - Apr-10-2017 thats because you are drawing the background overtop of the bullets https://github.com/Low-Ki/alien_invasion/pull/4/commits/0f476d13aaec8bb3b19d1d9454a3b07eca1b7249 I also added a .gitignore file to stop pyc files from being uploaded. RE: Traceback Help - Low_Ki_ - Apr-11-2017 (Apr-10-2017, 11:27 PM)metulburr Wrote: thats because you are drawing the background overtop of the bullets Awesome pyc files were confusing me lol. also why do I get this error when trying to help others on their projects "remote: Permission to dariankbrown/The-5000.git denied to Low-Ki." cannot figure out why i get that error when pushing to git... also im new to get... i am using http to clone and push... i also don't know the command line too well yet lol RE: Traceback Help - metulburr - Apr-11-2017 You cant push to another's repo unless you have write access. In that case you have to make a pull request. You have to fork their project, write your changes to your fork, then make the pull request to merge your fork in. RE: Traceback Help - Low_Ki_ - Apr-11-2017 (Apr-11-2017, 12:10 PM)metulburr Wrote: You cant push to another's repo unless you have write access. In that case you have to make a pull request. You have to fork their project, write your changes to your fork, then make the pull request to merge your fork in. thanks RE: Traceback Help - timtimbot - Aug-07-2018 (Apr-10-2017, 11:27 PM)metulburr Wrote: thats because you are drawing the background overtop of the bullets Ive got the same problem with invisible bullets. Can you explain it please? |