AlienInvasion | Demo Game for Mobile HTML5 Game Development | Game Engine library
kandi X-RAY | AlienInvasion Summary
kandi X-RAY | AlienInvasion Summary
It is released under both the GPL and MIT license to do with what you will. Bit.ly link for mobile: If you make an interesting fork or enhancement of the game, let me know and it’ll get linked to here. This original repo will stay matching the code in the book.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of AlienInvasion
AlienInvasion Key Features
AlienInvasion Examples and Code Snippets
Community Discussions
Trending Discussions on AlienInvasion
QUESTION
I am starting to learn Python and I'm not sure what the solution to this would be.This problem happens when i'm doing project ALien Invasion: there just 1 column of aliens moving when I run the program.
alien_invasion.py:
...ANSWER
Answered 2022-Feb-25 at 15:58Problem is self.x
In Alien.__init__
you set self.x = float(self.rect.x)
but when you create aliens then you use aliens.rect.x = ...
but you don't use alien.x = float(alien.rect.x)
and finally all aliens have different value in self.rect.x
but the same value in self.x
.
And later in Alien.update()
you set self.rect.x = self.x
and it moves all aliens to the same column.
You have to use alien.x = float(alien.rect.x)
when you create alien.
QUESTION
I am working on the Alien Invasion project at the end of Python Crash Course 2nd Edition and I am having a problem with my game ending after the user runs out of lives. The game is supposed to end after all 3 lives are lost but when that happens for mine the aliens continue to move off the bottom of the screen. There are no error messages and from my many checks through the code I have entered it word for word from the book. I have worked up to the section where the scoreboard is added but now I am resetting the scoreboard which requires the game to end properly. Below is a copy of all the necessary code files that would apply to this issue. Each separate file is indicated by #filename.py and the code for said file follows. If there is anything else needed to check through this please let me know.
...ANSWER
Answered 2021-Nov-26 at 15:46**Converting my comment to an answer
original comment:
"This is just a guess, but in your _ship_hit function you have self.stats.game_active_= False
Should that be
self.stats.game_active = False
?
it looks like you might have an extra underscore at the end"
QUESTION
I'm currently looking to display the time remaining for power ups on my scoreboard. I'm using pygame.time.get_ticks() to determine the remaining time left for a power up and it seems to be functioning as expected. When a power up is received it lasts for 5 seconds (5000 ticks), unless the same power up is picked up again, then the time is extended by another 5 seconds. This is determined by my power level; picking up a power up increases power up level by 1, once 5 seconds pass, decrease power level by 1.
I'm having trouble working out how to get this to properly display on my scoreboard, as currently it just displays -5000, which would be my starting power up time (0) minus my power up time allowed per level (5000). When a power up is picked up the time does not change at all and just stays at -5000.
main.py
...ANSWER
Answered 2021-Nov-19 at 01:44You are only doing the following two things once since prep_remaining_pow_time
is called in init.
QUESTION
I'm currently attempting to add walls to my game that can be destroyed by both the players laser and the aliens bombs. Currently when drawing my wall to screen, it is just a single broken up line, instead of the expected shape.
This is my first time using enumerate, and I followed along with Clear Codes Youtube video and from what I can tell my code should in theory be producing the desired shape.
What my wall currently looks like
walls.py
...ANSWER
Answered 2021-Nov-15 at 23:25Without deeply reading the program, the core issue is likely that your shape
is a single string of lines catenated together, which happens when strings are simply collected together in an argument without any separator.. while you probably intended to put commas or newlines inbetween them to form the rows of the sprite!
current:
QUESTION
I'm currently trying to get my aliens to have a random chance to drop powerups when they are destroyed. I seem to have the basic structure and logic figure out, but I'm running into an error.
When an alien is hit and it DOES spawn a power up, my game crashes and I'm greeted with an error.
...ANSWER
Answered 2021-Nov-14 at 22:42pygame.sprite.groupcollide
returns a dictionary, where the elements are a list. Therefore aliens
is a list:
pow = Pow(aliens.rect.center)
QUESTION
I'm currently attempting to get my aliens to drop 'bombs' back at the player during game play. My current goal is to just get all aliens to drop bombs, then to eventually have random aliens drop bombs after a certain level.
Currently is seems that all bombs are coming from the starting location of my first alien.
alien_invasion.py
...ANSWER
Answered 2021-Nov-14 at 16:05you need to include the x y position of the bomb in the __init__
of bombs:
QUESTION
import sys
import pygame
from pygame.sprite import Sprite
class Settings:
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
self.ship_speed = 1.5
self.bullet_speed = 1.0
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = (60,60,60)
self.bullets_allowed = 3
class Ship:
def __init__(self, ai_game):
self.screen = ai_game.screen
self.settings = ai_game.settings
self.screen_rect = ai_game.screen.get_rect()
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.rect.midbottom = self.screen_rect.midbottom
self.x = float(self.rect.x)
self.moving_right = False
self.moving_left = False
def update(self):
if self.moving_right and self.rect.right < self.screen_rect.right:
self.x += self.settings.ship_speed
if self.moving_left and self.rect.left > 0:
self.x -= self.settings.ship_speed
self.rect.x = self.x
def blitme(self):
self.screen.blit(self.image, self.rect)
class Bullet(Sprite):
def __init__(self, ai_game):
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.settings
self.color = self.settings.bullet_color
self.rect = pygame.Rect(0, 0, self.settings.bullet_width, self.settings.bullet_height)
self.rect.midtop = ai_game.ship.rect.midtop
self.y = float(self.rect.y)
def update(self):
self.y -= self.settings.bullet_speed
self.rect.y = self.y
def draw_bullet(self):
pygame.draw.rect(self.screen, self.color, self.rect)
class Alien(Sprite):
def __init__(self, ai_game):
super().__init__()
self.screen = ai_game.screen
self.image = pygame.image.load('images/alien.bmp')
self.rect = self.image.get_rect()
self.rect.x = self.rect.width
self.rect.y = self.rect.height
self.x = float(self.rect.x)
class AlienInvasion:
def __init__(self):
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
self.aliens = pygame.sprite.Group()
def run_game(self):
while True:
self._check_events()
self._update_screen()
self._update_bullets()
self._create_fleet()
def _check_events(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
elif event.type == pygame.KEYUP:
self._check_keyup_events(event)
elif event.type == pygame.QUIT:
sys.exit()
def _check_keydown_events(self, event):
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.key == pygame.K_SPACE:
self._fire_bullet()
elif event.key == pygame.K_q:
sys.exit()
def _check_keyup_events(self, event):
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
def _fire_bullet(self):
if len(self.bullets) <= self.settings.bullets_allowed:
bullet = Bullet(self)
self.bullets.add(bullet)
def _update_bullets(self):
self.bullets.update()
for bullet in self.bullets.copy():
if bullet.rect.bottom <= 0:
self.bullets.remove(bullet)
def _create_fleet(self):
alien = Alien(self)
self.aliens.add(alien)
def _update_screen(self):
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
self.aliens.draw(self.screen)
pygame.display.flip()
if __name__ == '__main__':
ai = AlienInvasion()
ai.run_game()
...ANSWER
Answered 2021-Sep-04 at 09:04Ship.update()
is never called. You can call it in AlienInvasion._update_screen
:
QUESTION
I'm making a simple python game and there are 3 py files: alien_invasion, settings, ship. I would like the image position to be at the middle bottom of screen every time the game starts. It works when code are like the following:
...
ANSWER
Answered 2021-Jul-10 at 16:33You need to set self.x
after setting the location of self.rect
:
QUESTION
I am currently trying to study python using the Python Crash Course book but I have halted right now because I can't go forward with the first project. Whenever I run my program in pycharm or IDLE, all I get is a message saying "pygame 2.0.1 (SDL 2.0.14, Python 3.9.6) Hello from the pygame community. https://www.pygame.org/contribute.html" with no window for the pygame popping up, my code is below, thank you for whoever can help me: EDIT: My OS is Windows 10 21H1, Currently using pycharm 2021.1 with python 3.9.6 and pygame 2.0.1
...ANSWER
Answered 2021-Jul-10 at 09:26Already fixed, I tried doing another project first (one the utilizes matplot library and it was magically fixed when I tried it again.)
QUESTION
I've tried everything to fix this, my code runs and a screen is shown but the image is not on the screen. so far I've tried:
- putting an absolute path when entering the image in pygame.image.load
- trying os.join.path method (file cannot be found error)
- And loads of other things -How after trying everything, and closely following the book, i cant get it to work?
ANSWER
Answered 2021-May-26 at 10:26The image is not shown, because the display is not updated after drawing the image. Update the display after drawing the ship:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install AlienInvasion
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page