I've been working on a Space Invaders clone, and currently I am in the early development stages of it. However, I've been trying to figure out how to get the enemies moving down the screen and shoot bullets from the player's position and make them move upward.
import pygame
from sqlite3 import *
from tkinter import *
pygame.init()
clock=pygame.time.Clock()#limit the framerate (set to 60 in line 60)
black=(0,0,0)
white=(255,255,255)#defining the colors black & white in pygame
surface = pygame.display.set_mode((500,500))#Setting the window size of the pygame window
gamequit=True#add a variable to instantiate the quit function for the game
while not gamequit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gamequit=False
pygame.quit()
player_x_coordinate=235
player_y_coordinate=450
bullet_speed=-5
bullet_y_coordinate=player_y_coordinate
bullet_x_coordinate=player_x_coordinate
bullets_on_screen=[]
default_player_speed=0#The standard speed of the player with no movement upgrades applied (changes to 10 if right directional key is pressed & changes to -10 if left directional key is pressed)
class Player():
def __init__(self):
self.score_held=0#set the initial amount of score the player has
self.player_health=1#set the player's initial health
class enemy():
def __init__(self,score_distribution,enemy_health,enemy_object_x,enemy_object_y):
self.score_distribution=score_distribution
self.enemy_health=1
enemy_object=pygame.draw.rect(surface, (white), (enemy_object_x,enemy_object_y,25,25))#Drawing the enemy's object rectangle
if enemy_object_y==enemy_object_y:
enemy_object_y+=default_player_speed#Enemy moves down the screen
while gamequit:
for event in pygame.event.get():#Allow the player to move their character around
if event.type==pygame.KEYDOWN:
if event.key== pygame.K_LEFT:
default_player_speed=-3
if event.key== pygame.K_RIGHT:
default_player_speed=3
if event.type==pygame.KEYUP:
if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
default_player_speed=0#stop player movement when directional keys aren't being pressed
if player_x_coordinate<=0:
player_x_coordinate+=5
elif player_x_coordinate>=475:
player_x_coordinate-=5
#fire_bullet_player()
player_x_coordinate+=default_player_speed#The speed factor by which the player moves
bullet_y_coordinate+=bullet_speed
surface.fill(black)
player_object=pygame.draw.rect(surface, white, [player_x_coordinate,player_y_coordinate,25,25])#draw the ship the player will be using in the game: 1st bracket=window drawn onto, 2nd bracket=color values in RGB, 3rd bracket= Co-ordinates & width & height)
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_SPACE:
pygame.draw.rect(surface, (255,255,255), (bullet_x_coordinate,bullet_y_coordinate,10,10))
player_character=Player()
enemy(500,1,235,30)
enemy(500,1,285,30)
enemy(500,1,185,30)
enemy(500,1,135,30)
enemy(500,1,335,30)
enemy(500,1,85,30)
enemy(500,1,385,30)
enemy(500,1,235,70)
enemy(500,1,285,70)
enemy(500,1,185,70)
enemy(500,1,135,70)
enemy(500,1,335,70)
enemy(500,1,85,70)
enemy(500,1,385,70)
enemy(500,1,235,110)
enemy(500,1,285,110)
enemy(500,1,185,110)
enemy(500,1,135,110)
enemy(500,1,335,110)
enemy(500,1,85,110)
enemy(500,1,385,110)#Assign enemies here
clock.tick(60)
pygame.display.update()
Here's my sample code. I've tried implementing the code for both functions in different parts of the whole code, but none of the times has it actually worked for either of them. Anybody willing to show me how to get them working? To clarify: When an enemy is hit with a bullet, it's supposed to decrease their health by 1 hit point (which is the exact health some enemies have, and others will have more, which means it'll take a few bullets to eliminate them.). I also intend for the enemy to move at a decimal rate (slow enough for the user to take down all of them at once).