init
This commit is contained in:
EggMan 2024-04-23 20:14:24 -04:00
commit cb83deca0c
43 changed files with 661 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

10
.idea/CNSA-266-FP.iml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (CNSA-266-FP)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/CNSA-266-FP.iml" filepath="$PROJECT_DIR$/.idea/CNSA-266-FP.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

100
Alien.py Normal file
View File

@ -0,0 +1,100 @@
import arcade
import random
from AlienBullet import AlienBullet
class Alien(arcade.Sprite):
def __init__(self, x, y, SCREEN_SCALE, DEBUG, AlienType):
super().__init__()
self.center_x = x * SCREEN_SCALE
self.center_y = y * SCREEN_SCALE
self.scScale = SCREEN_SCALE
self.deBuggin = DEBUG
self.alienType = AlienType
self.alien_sprite = None
self.wait = 0
self.isAlive = True
self.bullet = None
self.sizeX = 0
self.sizeY = 0
if self.alienType == "LA":
self.sizeX = 6 * self.scScale
self.sizeY = 4 * self.scScale
self.image_source = "assets/Sprites/Enemy/LA1.png"
if self.deBuggin:
print(str(self.image_source))
if self.alienType == "MA":
self.sizeX = 5 * self.scScale
self.sizeY = 4 * self.scScale
self.image_source = "assets/Sprites/Enemy/MA1.png"
if self.deBuggin:
print(str(self.image_source))
if self.alienType == "SA":
self.sizeX = 4 * self.scScale
self.sizeY = 4 * self.scScale
self.image_source = "assets/Sprites/Enemy/SA1.png"
if self.deBuggin:
print(str(self.image_source))
self.alien_sprite = arcade.Sprite(self.image_source, self.scScale)
def update(self, x, sign, movedown):
if self.alienType == "LA" and self.wait > 0:
if self.image_source == "assets/Sprites/Enemy/LA1.png":
self.image_source = "assets/Sprites/Enemy/LA2.png"
self.alien_sprite = arcade.Sprite(self.image_source, self.scScale)
else:
self.image_source = "assets/Sprites/Enemy/LA1.png"
self.alien_sprite = arcade.Sprite(self.image_source, self.scScale)
if self.alienType == "MA" and self.wait > 0:
if self.image_source == "assets/Sprites/Enemy/MA1.png":
self.image_source = "assets/Sprites/Enemy/MA2.png"
self.alien_sprite = arcade.Sprite(self.image_source, self.scScale)
else:
self.image_source = "assets/Sprites/Enemy/MA1.png"
self.alien_sprite = arcade.Sprite(self.image_source, self.scScale)
if self.alienType == "SA" and self.wait > 0:
if self.image_source == "assets/Sprites/Enemy/SA1.png":
self.image_source = "assets/Sprites/Enemy/SA2.png"
self.alien_sprite = arcade.Sprite(self.image_source, self.scScale)
else:
self.image_source = "assets/Sprites/Enemy/SA1.png"
self.alien_sprite = arcade.Sprite(self.image_source, self.scScale)
self.wait = 1
if movedown:
self.center_y -= 8 * self.scScale
if x:
self.center_x += 2*self.scScale*sign
self.alien_sprite.center_x = self.center_x
self.alien_sprite.center_y = self.center_y
def getSizeX(self):
return self.sizeX
def getSizeY(self):
return self.sizeY
def checkBullet(self):
try:
if self.bullet != None:
if self.bullet.getY() < -50:
self.bullet = None
if self.deBuggin:
print("Bullet Killed")
except Exception as e:
pass
def spawnBullet(self):
self.bullet = AlienBullet(self.scScale, self.deBuggin, self.center_x, self.center_y)
def getX(self):
return self.center_x
def getY(self):
return self.center_y
def getType(self):
return self.alienType

68
AlienBullet.py Normal file
View File

@ -0,0 +1,68 @@
import arcade
import random
BULLET_SPEED = 2
class AlienBullet(arcade.Sprite):
def __init__(self, SCREEN_SCALE, DEBUG, x, y):
super().__init__()
self.isAlive = True
self.deBuggin = DEBUG
self.scScale = SCREEN_SCALE
self.i = 0
self.image_source = None
if self.deBuggin:
print("Alien Bullet Spawning")
self.type = random.randint(1,3)
if self.type == 1:
self.image_souce = "assets/Sprites/Bullets/B1.png"
if self.type == 2:
self.image_souce = "assets/Sprites/Bullets/C1.png"
if self.type == 3:
self.image_souce = "assets/Sprites/Bullets/D1.png"
self.bullet_sprite = arcade.Sprite(self.image_source, self.scScale)
self.center_x = x
self.center_y = y
self.count = 0
def update(self):
self.count += 1
self.center_y -= BULLET_SPEED*self.scScale
# if self.deBuggin:
# print(str(self.center_y))
if self.count == 4:
if self.type == 1:
if self.image_souce == "assets/Sprites/Bullets/B1.png":
self.image_souce = "assets/Sprites/Bullets/B2.png"
elif self.image_souce == "assets/Sprites/Bullets/B2.png":
self.image_souce = "assets/Sprites/Bullets/B3.png"
elif self.image_souce == "assets/Sprites/Bullets/B3.png":
self.image_souce = "assets/Sprites/Bullets/B4.png"
elif self.image_souce == "assets/Sprites/Bullets/B4.png":
self.image_souce = "assets/Sprites/Bullets/B1.png"
if self.type == 2:
if self.image_souce == "assets/Sprites/Bullets/C1.png":
self.image_souce = "assets/Sprites/Bullets/C2.png"
elif self.image_souce == "assets/Sprites/Bullets/C2.png":
self.image_souce = "assets/Sprites/Bullets/C3.png"
elif self.image_souce == "assets/Sprites/Bullets/C3.png":
self.image_souce = "assets/Sprites/Bullets/C4.png"
elif self.image_souce == "assets/Sprites/Bullets/C4.png":
self.image_souce = "assets/Sprites/Bullets/C1.png"
if self.type == 3:
if self.image_souce == "assets/Sprites/Bullets/D1.png":
self.image_souce = "assets/Sprites/Bullets/D2.png"
elif self.image_souce == "assets/Sprites/Bullets/D2.png":
self.image_souce = "assets/Sprites/Bullets/D3.png"
elif self.image_souce == "assets/Sprites/Bullets/D3.png":
self.image_souce = "assets/Sprites/Bullets/D4.png"
elif self.image_souce == "assets/Sprites/Bullets/D4.png":
self.image_souce = "assets/Sprites/Bullets/D1.png"
self.count = 0
self.bullet_sprite = arcade.Sprite(self.image_souce, self.scScale)
self.bullet_sprite.center_x = self.center_x
self.bullet_sprite.center_y = self.center_y
def getX(self):
return self.center_x
def getY(self):
return self.center_y

50
Bullet.py Normal file
View File

@ -0,0 +1,50 @@
import arcade
BULLET_SPEED = 4
class Bullet(arcade.Sprite):
def __init__(self, SCREEN_SCALE, DEBUG, x, y):
super().__init__()
self.isAlive = True
self.deBuggin = DEBUG
self.scScale = SCREEN_SCALE
self.i = 0
if self.deBuggin:
print("Bullet Initializing")
image_source = "assets/Sprites/Player/PlayerBullet.png"
self.bullet_sprite = arcade.Sprite(image_source, self.scScale)
self.center_x = x + (1 * self.scScale) / 2
self.center_y = y + 4 * self.scScale
def update(self):
self.center_y = self.center_y + (BULLET_SPEED * self.scScale)
self.bullet_sprite.center_y = self.center_y
self.bullet_sprite.center_x = self.center_x
# self.bullet_sprite.draw(pixelated=True)
if self.deBuggin:
self.i += 1
if self.i >= 10:
self.i = 0
print("Bullet Updating | X: " + str(self.center_x) + " | Y: " + str(self.center_y) + " ||| SX: " + str(self.bullet_sprite.center_x) + " | SY: " + str(self.bullet_sprite.center_y))
def getX(self):
return self.center_x
def getY(self):
return self.center_y
def death(self):
if self.deBuggin:
print("Bullet Deathing")
self.isAlive = False
self.data = None
self.__del__()
def __del__(self):
self.isAlive = False
if self.deBuggin:
print("Bullet Dead")

401
Main.py Normal file
View File

@ -0,0 +1,401 @@
import random
import sys
import time
import arcade
from Bullet import Bullet
from Alien import Alien
DEBUG = True
SLOW = False
SLOW_SPEED = 0.01
SCREEN_SCALE = 3
SCREEN_HEIGHT = 256 * SCREEN_SCALE
SCREEN_WIDTH = 224 * SCREEN_SCALE
SCREEN_TITLE = "Space Invaders"
PLAYER_SPEED = 1
PLAYER_BOUNDS = 32
ALIEN_BOUNDS = 16
class MyGame(arcade.Window):
""" Main application class"""
def __init__(self):
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
self.activeBullet = False
self.wall_list = None
self.player_list = None
self.Won = False
self.Score = 0
self.bullet = None
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]]
self.alienCounterY = 4
self.alienCounterX = 0
self.alienChangeDir = 0
self.alienCurrentDir = -1
self.movedown = False
self.player_sprite = None
self.movePlayer = 0
if DEBUG:
arcade.set_background_color(arcade.color.BLUE)
else:
arcade.set_background_color(arcade.csscolor.BLACK)
self.i = 0
def setup(self):
"""start / restart game"""
self.scene = arcade.Scene()
self.scene.add_sprite_list("Players")
self.scene.add_sprite_list("Walls", use_spatial_hash=True)
self.player_list = arcade.SpriteList()
self.wall_list = arcade.SpriteList(use_spatial_hash=True)
self.rend = 0
self.lives = 3
self.level = 0
# self.overlay_color = (0,255,0,128)
self.player_sprite_death = None
self.somethingalsi = True
# set up the player
image_source = "assets/Sprites/Player/Player.png"
self.player_sprite = arcade.Sprite(image_source, SCREEN_SCALE)
self.player_sprite.center_x = 32 * SCREEN_SCALE
self.player_sprite.center_y = (32 + 4) * SCREEN_SCALE
self.player_list.append(self.player_sprite)
# Create Left Boundry
if DEBUG == True:
wall = arcade.SpriteSolidColor(1, SCREEN_HEIGHT, arcade.color.RED)
else:
wall = arcade.SpriteSolidColor(0, SCREEN_HEIGHT, arcade.color.BLACK)
wall.center_x = (PLAYER_BOUNDS - 8) * SCREEN_SCALE
wall.center_y = SCREEN_HEIGHT / 2
self.wall_list.append(wall)
# Create Right Boundry
if DEBUG == True:
wall = arcade.SpriteSolidColor(1, SCREEN_HEIGHT, arcade.color.RED)
else:
wall = arcade.SpriteSolidColor(0, SCREEN_HEIGHT, arcade.color.BLACK)
wall.center_x = (224 - PLAYER_BOUNDS + 8) * SCREEN_SCALE
wall.center_y = SCREEN_HEIGHT / 2
self.wall_list.append(wall)
if DEBUG == True:
wall = arcade.SpriteSolidColor(1, SCREEN_HEIGHT, arcade.color.GREEN)
else:
wall = arcade.SpriteSolidColor(0, SCREEN_HEIGHT, arcade.color.BLACK)
wall.center_x = (ALIEN_BOUNDS - 8) * SCREEN_SCALE
wall.center_y = SCREEN_HEIGHT / 2
self.wall_list.append(wall)
# Create Right Boundry
if DEBUG == True:
wall = arcade.SpriteSolidColor(1, SCREEN_HEIGHT, arcade.color.GREEN)
else:
wall = arcade.SpriteSolidColor(0, SCREEN_HEIGHT, arcade.color.BLACK)
wall.center_x = (224 - ALIEN_BOUNDS + 8) * SCREEN_SCALE
wall.center_y = SCREEN_HEIGHT / 2
self.wall_list.append(wall)
# Aliens
for b in range (0,5,1):
for a in range(0, 11, 1):
if b == 0:
alienType = "SA"
elif b == 1 or b == 2:
alienType = "MA"
else:
alienType = "LA"
aliena = Alien(16 * (a+1) + 16, 200 - (16 * (b+1)), SCREEN_SCALE, DEBUG,alienType)
# aliena.update(None,None,None)
self.aliens[b][a] = aliena
def collisionCheck(self, x1, y1, x2, y2, rx, ry):
if abs(x1 - x2) <= rx and abs(y1 - y2) <= ry:
return True
else:
return False
def on_key_press(self, key, modifiers):
if key == arcade.key.SPACE and self.activeBullet == False:
self.bullet = Bullet(SCREEN_SCALE, DEBUG, self.player_sprite.center_x, self.player_sprite.center_y)
self.activeBullet = True
if key == arcade.key.LEFT:
self.movePlayer = -PLAYER_SPEED * SCREEN_SCALE
elif key == arcade.key.RIGHT:
self.movePlayer = PLAYER_SPEED * SCREEN_SCALE
def on_key_release(self, key, modifiers):
if key == arcade.key.LEFT and self.movePlayer == -PLAYER_SPEED * SCREEN_SCALE:
self.movePlayer = 0
elif key == arcade.key.RIGHT and self.movePlayer == PLAYER_SPEED * SCREEN_SCALE:
self.movePlayer = 0
def on_draw(self):
self.clear()
# try:
# self.player_sprite_death.draw(pixelated=True)
# 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:
for alienx in self.aliens:
for alien in alienx:
try:
alien.bullet.bullet_sprite.draw(pixelated=True)
except Exception as e:
pass
alien.alien_sprite.draw(pixelated=True)
except Exception as e:
pass
self.player_list.draw(pixelated=True)
self.wall_list.draw(pixelated=True)
def KillBullet(self):
self.bullet = None
self.activeBullet = False
def respawn(self):
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
else:
if DEBUG:
print(str(self.Score))
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)
# Checking if player bullet is off-screen, if so, set bullet to None, else, update bullet
try:
if self.bullet.getY() >= SCREEN_HEIGHT:
self.KillBullet()
if self.bullet != None:
self.bullet.update()
except Exception as e:
self.error = e
# Run the update method for each alien's bullet that exists in the self.aliens list
try:
for alienx in self.aliens:
for alien in alienx:
try:
alien.bullet.update()
except Exception as e:
pass
except Exception as e:
pass
# Checks if the alien's bullet is below -50, if so, set alien's bullet to None
# Checks to see if testx and testy == self.alien[testx],[testy]. if yes: spawn new alien bullet
try:
for indexx, alienx in enumerate(self.aliens):
for index, alien in enumerate(alienx):
try:
alien.checkBullet()
if indexx == testx and index == testy:
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
if self.alienCounterX <= len(self.aliens[self.alienCounterY]) - 1:
x = self.aliens[self.alienCounterY][self.alienCounterX].getX()
# Right Alien Bound Check
if x <= ALIEN_BOUNDS * SCREEN_SCALE + SCREEN_SCALE:
self.alienChangeDir = 1
# Left Alien Bound Check
elif x >= SCREEN_WIDTH - ALIEN_BOUNDS * SCREEN_SCALE - SCREEN_SCALE:
self.alienChangeDir = -1
# Update current alien
self.aliens[self.alienCounterY][self.alienCounterX].update(True, self.alienCurrentDir, self.movedown)
# Move the X counter variable to the next alien
self.alienCounterX += 1
# If an exeption is reached, it is likely that the X counter is out of bounds
# which means we need to go to the next row of aliens
else:
# Reset X counter
self.alienCounterX = 0
# Decrease Y counter so that we move to the next row of aliens
self.alienCounterY -= 1
# if we go outside the minimum index of our aliens list, which means we reached the
# very end of the list of aliens, raise an exception so that the except block runs
if self.alienCounterY < 0:
# Reset the X and Y counter variables
self.alienCounterY = len(self.aliens) - 1
self.alienCounterX = 0
# if the aliens are at the right alien bound
if self.alienChangeDir == -1:
if self.alienChangeDir != self.alienCurrentDir:
self.movedown = True
else:
self.movedown = False
self.alienCurrentDir = -1
# Reset alienChangeDir
self.alienChangeDir = 0
# if the aliens are at the left alien bound
elif self.alienChangeDir == 1:
if self.alienChangeDir != self.alienCurrentDir:
self.movedown = True
else:
self.movedown = False
self.alienCurrentDir = 1
self.alienChangeDir = 0
else:
self.alienChangeDir = 0
# if DEBUG:
# print(e)
if len(self.aliens) == 0:
self.Won = True
# except Exception as e:
# if DEBUG:
# print(e)
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 == []:
self.aliens.remove(alienRow)
for alien in alienRow:
try:
# Check for alien colliding with player bullet
if self.collisionCheck(alien.getX(), alien.getY(), self.bullet.getX(), self.bullet.getY(),
alien.getSizeX(), alien.getSizeY()):
# For Scoring Points
type = alien.getType()
if type == "SA":
self.Score += 40
elif type == "MA":
self.Score += 20
elif type == "LA":
self.Score += 10
alienRow.remove(alien)
if DEBUG:
print(str(self.Score))
# Kill player bullet that hit the alien
self.KillBullet()
except Exception as e:
pass
try:
# Check for player colliding with alien bullet
if self.collisionCheck(self.player_sprite.center_x, self.player_sprite.center_y, alien.bullet.getX(), alien.bullet.getY(),
7 * SCREEN_SCALE, 5 * SCREEN_SCALE):
self.lives -= 1
self.death()
except Exception as e:
pass
if self.player_sprite.center_x >= 193 * SCREEN_SCALE and self.movePlayer == PLAYER_SPEED * SCREEN_SCALE:
self.movePlayer = 0
elif self.player_sprite.center_x <= (PLAYER_BOUNDS - 2) * SCREEN_SCALE and self.movePlayer == -PLAYER_SPEED * SCREEN_SCALE:
self.movePlayer = 0
else:
self.player_sprite.center_x += self.movePlayer
if SLOW:
time.sleep(SLOW_SPEED)
else:
test = False
if test:
self.player_sprite = arcade.Sprite("assets/Sprites/Player/PlayerDeath1.png", SCREEN_SCALE)
self.player_list.draw()
else:
self.player_sprite = arcade.Sprite("assets/Sprites/Player/PlayerDeath2.png", SCREEN_SCALE)
self.player_list.draw()
def main():
window = MyGame()
window.setup()
arcade.run()
if __name__ == "__main__":
main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
assets/Sprites/House.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
assets/Sprites/Ship.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
assets/Sprites/Sprites.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 823 B

0
test.py Normal file
View File