Merge remote-tracking branch 'origin/master'

# Conflicts:
#	Main.py
This commit is contained in:
EggMan 2024-05-06 08:18:44 -04:00
commit 2a0301aefb
4 changed files with 106 additions and 65 deletions

View File

@ -5,7 +5,7 @@
<excludeFolder url="file://$MODULE_DIR$/.venv" /> <excludeFolder url="file://$MODULE_DIR$/.venv" />
<excludeFolder url="file://$MODULE_DIR$/venv" /> <excludeFolder url="file://$MODULE_DIR$/venv" />
</content> </content>
<orderEntry type="jdk" jdkName="Python 3.11 (CNSA-266-FP)" jdkType="Python SDK" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@ -68,7 +68,7 @@ class Alien(arcade.Sprite):
if movedown: if movedown:
self.center_y -= 8 * self.scScale self.center_y -= 8 * self.scScale
if x: if x:
self.center_x += 2*self.scScale*sign self.center_x += 2 * self.scScale * sign
self.alien_sprite.center_x = self.center_x self.alien_sprite.center_x = self.center_x
self.alien_sprite.center_y = self.center_y self.alien_sprite.center_y = self.center_y

116
Main.py
View File

@ -7,7 +7,7 @@ from Score import Score
from Bullet import Bullet from Bullet import Bullet
from Alien import Alien from Alien import Alien
DEBUG = True DEBUG = False
SLOW = False SLOW = False
SLOW_SPEED = 0.01 SLOW_SPEED = 0.01
@ -21,7 +21,7 @@ SCREEN_TITLE = "Space Invaders"
PLAYER_SPEED = 1 PLAYER_SPEED = 1
PLAYER_BOUNDS = 32 PLAYER_BOUNDS = 32
ALIEN_BOUNDS = 16 ALIEN_BOUNDS = 32
@ -47,14 +47,16 @@ class MyGame(arcade.Window):
[Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien], [Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien],
[Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien]] [Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien]]
self.lives = 3
self.alienCounterY = 4 self.alienCounterY = 4
self.alienCounterX = 0 self.alienCounterX = 0
self.alienChangeDir = 0 self.alienChangeDir = 0
self.alienCurrentDir = -1 self.alienCurrentDir = -1
self.movedown = False self.movedown = False
self.current_player_texture_index = 0 self.current_texture_index = 0
self.player_sprite = None self.player_sprite = None
self.level = 0
self.movePlayer = 0 self.movePlayer = 0
if DEBUG: if DEBUG:
@ -65,6 +67,12 @@ class MyGame(arcade.Window):
self.i = 0 self.i = 0
def setup(self): def setup(self):
self.aliens = [[Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien],
[Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien],
[Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien],
[Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien],
[Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien]]
"""start / restart game""" """start / restart game"""
self.scene = arcade.Scene() self.scene = arcade.Scene()
@ -75,8 +83,6 @@ class MyGame(arcade.Window):
self.player_list = arcade.SpriteList() self.player_list = arcade.SpriteList()
self.wall_list = arcade.SpriteList(use_spatial_hash=True) self.wall_list = arcade.SpriteList(use_spatial_hash=True)
self.rend = 0 self.rend = 0
self.lives = 3
self.level = 0
self.score = None self.score = None
# self.overlay_color = (0,255,0,128) # self.overlay_color = (0,255,0,128)
self.player_sprite_death = None self.player_sprite_death = None
@ -90,6 +96,7 @@ class MyGame(arcade.Window):
self.player_sprite.center_y = (32 + 4) * SCREEN_SCALE self.player_sprite.center_y = (32 + 4) * SCREEN_SCALE
self.player_list.append(self.player_sprite) self.player_list.append(self.player_sprite)
self.level += 1
# Create Left Boundry # Create Left Boundry
if DEBUG == True: if DEBUG == True:
@ -162,19 +169,19 @@ class MyGame(arcade.Window):
elif key == arcade.key.RIGHT and self.movePlayer == PLAYER_SPEED * SCREEN_SCALE: elif key == arcade.key.RIGHT and self.movePlayer == PLAYER_SPEED * SCREEN_SCALE:
self.movePlayer = 0 self.movePlayer = 0
def load_death_animation(self): # def load_death_animation(self):
self.death_textures = [] # self.death_textures = []
for i in range(0,5): # for i in range(0,5):
#
texture = arcade.load_texture("assets/Sprites/Player/PlayerDeath1.png") # texture = arcade.load_texture("assets/Sprites/Player/PlayerDeath1.png")
self.death_textures.append(texture) # self.death_textures.append(texture)
texture = arcade.load_texture("assets/Sprites/Player/PlayerDeath2.png") # texture = arcade.load_texture("assets/Sprites/Player/PlayerDeath2.png")
self.death_textures.append(texture) # self.death_textures.append(texture)
#
def play_death_animation(self): # def play_death_animation(self):
if self.current_player_texture_index < len(self.death_textures): # if self.current_texture_index < len(self.death_textures):
self.player_sprite.set_texture(self.current_player_texture_index) # self.player_sprite.set_texture(self.current_texture_index)
self.current_player_texture_index += 1 # self.current_texture_index += 1
def on_draw(self): def on_draw(self):
self.clear() self.clear()
@ -184,15 +191,7 @@ class MyGame(arcade.Window):
# except Exception as e: # except Exception as e:
# pass # pass
# Render player bullet if it exists
try:
self.bullet.bullet_sprite.draw(pixelated=True)
except Exception as e:
if DEBUG:
self.rend += 1
if self.rend >= 20:
print(e)
self.rend = 0
# Render alien bullets if they exist and render aliens # Render alien bullets if they exist and render aliens
try: try:
@ -206,7 +205,15 @@ class MyGame(arcade.Window):
except Exception as e: except Exception as e:
pass pass
# Render player bullet if it exists
try:
self.bullet.bullet_sprite.draw(pixelated=True)
except Exception as e:
if DEBUG:
self.rend += 1
if self.rend >= 20:
print(e)
self.rend = 0
self.player_list.draw(pixelated=True) self.player_list.draw(pixelated=True)
self.wall_list.draw(pixelated=True) self.wall_list.draw(pixelated=True)
@ -217,30 +224,41 @@ class MyGame(arcade.Window):
self.activeBullet = False self.activeBullet = False
def respawn(self): def respawn(self):
# for alienRow in self.aliens: for alienRow in self.aliens:
# for alien in alienRow: for alien in alienRow:
# alien.bullet = None alien.bullet = None
self.player_sprite.center_x = 32*SCREEN_SCALE self.player_sprite.center_x = 32*SCREEN_SCALE
time.sleep(2) time.sleep(2)
def death(self): def death(self):
if self.lives > 0: if self.lives > 0:
self.somethingalsi = False self.somethingalsi = False
self.load_death_animation() # self.load_death_animation()
self.play_death_animation() # self.play_death_animation()
self.respawn() self.respawn()
else: else:
if DEBUG: if DEBUG:
print(str(self.Score)) print("\n" * 100)
print("You scored: " + str(self.Score))
print("You died on level: " + str(self.level))
sys.exit() sys.exit()
def update(self, delta_time): def update(self, delta_time):
if self.somethingalsi:
# Picking numbers to possibly spawn an alien bullet
testx = random.randint(0, 5 + 100 - self.level)
testy = random.randint(0, 11 + 100 - self.level)
if True:
# Picking numbers to possibly spawn an alien bullet
testx = random.randint(0, 5 + 100 - self.level * 5)
testy = random.randint(0, 11 + 100 - self.level * 5)
testx = 500
testy = 500
if self.alienCounterY < 0:
# Reset the X and Y counter variables
self.alienCounterY = len(self.aliens) - 1
self.alienCounterX = 0
# Checking if player bullet is off-screen, if so, set bullet to None, else, update bullet # Checking if player bullet is off-screen, if so, set bullet to None, else, update bullet
try: try:
@ -258,6 +276,9 @@ class MyGame(arcade.Window):
for alienx in self.aliens: for alienx in self.aliens:
for alien in alienx: for alien in alienx:
try: try:
if alien.getY() - 8 * SCREEN_SCALE < self.player_sprite.center_y:
self.lives = 0
self.death()
alien.bullet.update() alien.bullet.update()
except Exception as e: except Exception as e:
pass pass
@ -271,21 +292,21 @@ class MyGame(arcade.Window):
for index, alien in enumerate(alienx): for index, alien in enumerate(alienx):
try: try:
alien.checkBullet() alien.checkBullet()
if indexx == testx and index == testy: if indexx == testx and index == testy and alien.bullet == None:
alien.spawnBullet() alien.spawnBullet()
except Exception as e: except Exception as e:
pass pass
except Exception as e: except Exception as e:
pass pass
if self.alienCounterY < 0:
# Reset the X and Y counter variables
self.alienCounterY = len(self.aliens) - 1
self.alienCounterX = 0
# Alien Movement # Alien Movement
if self.Won == False: if self.Won == False:
#Left - Right Movement #Left - Right Movement
try:
if self.alienCounterX <= len(self.aliens[self.alienCounterY]) - 1:
pass
except Exception as e:
self.setup()
if self.alienCounterX <= len(self.aliens[self.alienCounterY]) - 1: if self.alienCounterX <= len(self.aliens[self.alienCounterY]) - 1:
x = self.aliens[self.alienCounterY][self.alienCounterX].getX() x = self.aliens[self.alienCounterY][self.alienCounterX].getX()
@ -344,7 +365,7 @@ class MyGame(arcade.Window):
self.alienChangeDir = 0 self.alienChangeDir = 0
# if DEBUG: # if DEBUG:
# print(e) # print(e)
if len(self.aliens) == 0: if len(self.aliens) < 1 :
self.Won = True self.Won = True
# except Exception as e: # except Exception as e:
# if DEBUG: # if DEBUG:
@ -353,8 +374,9 @@ class MyGame(arcade.Window):
print("You Won!") print("You Won!")
sys.exit() sys.exit()
# Check to see if any aliens are colliding with a player's bullet # Check to see if any aliens are colliding with a player's bullet
for alienRow in self.aliens: for index, alienRow in enumerate(self.aliens):
if alienRow == []: length = len(self.aliens)
if alienRow == [] and index == length - 1 and length > 0:
self.aliens.remove(alienRow) self.aliens.remove(alienRow)
for alien in alienRow: for alien in alienRow:
try: try:

View File

@ -2,7 +2,7 @@ import sys
import arcade import arcade
LEFT_OFFSET = 8 LEFT_OFFSET = 16
class Score(arcade.Sprite): class Score(arcade.Sprite):
def __init__(self, SCREEN_SCALE, DEBUG): def __init__(self, SCREEN_SCALE, DEBUG):
@ -28,11 +28,29 @@ class Score(arcade.Sprite):
def update(self, score): def update(self, score):
self.sprites.clear() self.sprites.clear()
self.sprites.append(arcade.Sprite("assets/Sprites/Letters/s.png", self.scScale)) # add x and y coords s = arcade.Sprite("assets/Sprites/Letters/s.png", self.scScale)
self.sprites.append(arcade.Sprite("assets/Sprites/Letters/c.png", self.scScale)) # add x and y coords c = arcade.Sprite("assets/Sprites/Letters/c.png", self.scScale)
self.sprites.append(arcade.Sprite("assets/Sprites/Letters/o.png", self.scScale)) # add x and y coords o = arcade.Sprite("assets/Sprites/Letters/o.png", self.scScale)
self.sprites.append(arcade.Sprite("assets/Sprites/Letters/r.png", self.scScale)) # add x and y coords r = arcade.Sprite("assets/Sprites/Letters/r.png", self.scScale)
self.sprites.append(arcade.Sprite("assets/Sprites/Letters/e.png", self.scScale)) # add x and y coords e = arcade.Sprite("assets/Sprites/Letters/e.png", self.scScale)
s.center_y = self.scScale * 243
c.center_y = self.scScale * 243
o.center_y = self.scScale * 243
r.center_y = self.scScale * 243
e.center_y = self.scScale * 243
s.center_x = self.scScale * 8 + LEFT_OFFSET * self.scScale
c.center_x = self.scScale * 8 * 2 + LEFT_OFFSET * self.scScale
o.center_x = self.scScale * 8 * 3 + LEFT_OFFSET * self.scScale
r.center_x = self.scScale * 8 * 4 + LEFT_OFFSET * self.scScale
e.center_x = self.scScale * 8 * 5 + LEFT_OFFSET * self.scScale
self.sprites.append(s)
self.sprites.append(c)
self.sprites.append(o)
self.sprites.append(r)
self.sprites.append(e)
self.value = score self.value = score
@ -47,71 +65,72 @@ class Score(arcade.Sprite):
for index, val in enumerate(valArr): for index, val in enumerate(valArr):
if val == "0": if val == "0":
addSprite = arcade.Sprite("assets/Sprites/Numbers/0.png", self.scScale) addSprite = arcade.Sprite("assets/Sprites/Numbers/0.png", self.scScale)
addSprite.center_y = self.scScale * 250 addSprite.center_y = self.scScale * 230
addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET) addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET)
self.sprites.append(addSprite) self.sprites.append(addSprite)
if self.deBuggin: if self.deBuggin:
print(addSprite.center_x) print(addSprite.center_x)
elif val == "1": elif val == "1":
addSprite = arcade.Sprite("assets/Sprites/Numbers/1.png", self.scScale) addSprite = arcade.Sprite("assets/Sprites/Numbers/1.png", self.scScale)
addSprite.center_y = self.scScale * 250 addSprite.center_y = self.scScale * 230
addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET) addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET)
self.sprites.append(addSprite) self.sprites.append(addSprite)
if self.deBuggin: if self.deBuggin:
print(addSprite.center_x) print(addSprite.center_x)
elif val == "2": elif val == "2":
addSprite = arcade.Sprite("assets/Sprites/Numbers/2.png", self.scScale) addSprite = arcade.Sprite("assets/Sprites/Numbers/2.png", self.scScale)
addSprite.center_y = self.scScale * 250 addSprite.center_y = self.scScale * 230
addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET) addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET)
self.sprites.append(addSprite) self.sprites.append(addSprite)
if self.deBuggin: if self.deBuggin:
print(addSprite.center_x) print(addSprite.center_x)
elif val == "3": elif val == "3":
addSprite = arcade.Sprite("assets/Sprites/Numbers/3.png", self.scScale) addSprite = arcade.Sprite("assets/Sprites/Numbers/3.png", self.scScale)
addSprite.center_y = self.scScale * 250 addSprite.center_y = self.scScale * 230
addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET) addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET)
self.sprites.append(addSprite) self.sprites.append(addSprite)
if self.deBuggin: if self.deBuggin:
print(addSprite.center_x) print(addSprite.center_x)
elif val == "4": elif val == "4":
addSprite = arcade.Sprite("assets/Sprites/Numbers/4.png", self.scScale) addSprite = arcade.Sprite("assets/Sprites/Numbers/4.png", self.scScale)
addSprite.center_y = self.scScale * 250 addSprite.center_y = self.scScale * 230
addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET) addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET)
self.sprites.append(addSprite) self.sprites.append(addSprite)
if self.deBuggin: if self.deBuggin:
print(addSprite.center_x) print(addSprite.center_x)
elif val == "5": elif val == "5":
addSprite = arcade.Sprite("assets/Sprites/Numbers/5.png", self.scScale) addSprite = arcade.Sprite("assets/Sprites/Numbers/5.png", self.scScale)
addSprite.center_y = self.scScale * 250 addSprite.center_y = self.scScale * 230
addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET) addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET)
self.sprites.append(addSprite) self.sprites.append(addSprite)
if self.deBuggin: if self.deBuggin:
print(addSprite.center_x) print(addSprite.center_x)
elif val == "6": elif val == "6":
addSprite = arcade.Sprite("assets/Sprites/Numbers/6.png", self.scScale) addSprite = arcade.Sprite("assets/Sprites/Numbers/6.png", self.scScale)
addSprite.center_y = self.scScale * 250 addSprite.center_y = self.scScale * 230
addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET) addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET)
self.sprites.append(addSprite) self.sprites.append(addSprite)
if self.deBuggin: if self.deBuggin:
print(addSprite.center_x) print(addSprite.center_x)
elif val == "7": elif val == "7":
addSprite = arcade.Sprite("assets/Sprites/Numbers/7.png", self.scScale) addSprite = arcade.Sprite("assets/Sprites/Numbers/7.png", self.scScale)
addSprite.center_y = self.scScale * 250 addSprite.center_y = self.scScale * 230
addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET) addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET)
self.sprites.append(addSprite) self.sprites.append(addSprite)
if self.deBuggin: if self.deBuggin:
print(addSprite.center_x) print(addSprite.center_x)
elif val == "8": elif val == "8":
addSprite = arcade.Sprite("assets/Sprites/Numbers/8.png", self.scScale) addSprite = arcade.Sprite("assets/Sprites/Numbers/8.png", self.scScale)
addSprite.center_y = self.scScale * 250 addSprite.center_y = self.scScale * 230
addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET) addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET)
self.sprites.append(addSprite) self.sprites.append(addSprite)
if self.deBuggin: if self.deBuggin:
print(addSprite.center_x) print(addSprite.center_x)
elif val == "9": elif val == "9":
addSprite = arcade.Sprite("assets/Sprites/Numbers/9.png", self.scScale) addSprite = arcade.Sprite("assets/Sprites/Numbers/9.png", self.scScale)
addSprite.center_y = self.scScale * 250 addSprite.center_y = self.scScale * 230
addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET) addSprite.center_x = self.scScale * (index*8) + (self.scScale * LEFT_OFFSET)
self.sprites.append(addSprite) self.sprites.append(addSprite)
if self.deBuggin: if self.deBuggin:
print(addSprite.center_x) print(addSprite.center_x)