I am currently learning python from the book 'Python Crash Course'.
In the book I am currently on a project called Alien Invasion. I am running into a problem in the game after I tried to add the ability for the ship to gain speed the longer you press the button. It says that the settings() does not have the attribute 'blit' (when it calls the blitme function to draw the ship in the game).
My code is split into multiple modules which I have included below.
All modules are .py.
alieninvasionbase:
import sys
import pygame
from settings import *
from ship import *
from gamef import *
def start_game():
pygame.init()
ai_settings = settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Space Wars")
ship = Ship(ai_settings, screen)
while True:
ship.update()
check_events(ship)
us(ai_settings,screen,ship)
start_game()
game.py:
import sys
import ship
import pygame
def check_events(ship):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
def us(self,ai_settings,screen,ship):
self.screen.fill(ai_settings.bg_color)
self.ship.blitme()
self.pygame.display.flip()
check_events(ship)
ship.py:
import pygame
import sys
class Ship():
def __init__(self,screen,ai_settings):
self.screen = screen
self.ai_settings = ai_settings
self.image = pygame.image.load('images/spaceship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = self.image.get_rect()
self.screen_rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
self.moving_right = False
self.moving_left = False
def update(self):
if self.moving_right == True:
self.rect.centerx += 1
if self.moving_left == True:
self.rect.centerx += -1
def blitme(self):
self.screen.blit(self.image,self.rect)
settings.py:
import pygame
import sys
import ship
class settings():
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (0,0,0)