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" />
</content>
<orderEntry type="jdk" jdkName="Python 3.11 (CNSA-266-FP)" jdkType="Python SDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -68,7 +68,7 @@ class Alien(arcade.Sprite):
if movedown:
self.center_y -= 8 * self.scScale
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_y = self.center_y

116
Main.py
View File

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

View File

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