So I'm back at making a game and I was coding in the gameplay mechanics (it's a dodge-the-bullet, I was making the bullets) The bullets should keep generating from the top sprite (Minty) and keep spreading out(trust me, I've spent a lot of time reading up and googling stuff.) This is how it was supposed to look: (the top sprite is the opponent, the little white square is player, the purple circles are the bullets by the way)
how it should be:
but here's how it is:
And I just don't understand why it's happening?
Here's my code:
import sys
import time
import pygame
from pygame.locals import *
pygame.init()
#INITIALISE THE WINDOW.
#CONSTANTS ARE CAPITAL, VARIABLES ARE LOWERCASE
SCREENWIDTH = 1000
SCREENHEIGHT = 650
SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
screen = pygame.display.set_mode(SCREENSIZE)
BG_COL = [255, 123, 67]
S1_COL = (0, 255, 188)
clock = pygame.time.Clock()
screen.fill(BG_COL)
pygame.draw.rect(screen, S1_COL,(50, 50, 900, 575), 0)
pygame.display.update()
clock.tick(60)
class Player(pygame.sprite.Sprite):
def __init__(self, sprite):
self.sprite = sprite
self.x = 445
self.y = 550
class Opponent(pygame.sprite.Sprite):
def __init__(self, sprite):
self.sprite = sprite
self.x = 445
self.y = 30
class Bullet:
def __init__(self, sprite, length, width):
self.sprite = sprite
self.x = 460
self.y = 50
self.length = length
self.width = width
self.area = self.sprite.get_rect(x=self.x, y=self.y)
self.area.clamp_ip((50, 50, 900, 575))
class BulletGroup(pygame.sprite.Group):
def __init__(self, typeof, numof):
self.typeof = typeof
self.numof = numof
self.listof = []
for i in range(0, self.numof):
self.listof.append(typeof)
player = Player(pygame.image.load("Sprites/player.png"))
Minty = Opponent(pygame.image.load("Sprites/minty.png"))
purple_glow = Bullet(pygame.image.load("Sprites/purple-glowey.png"), 70, 65)
test_bullets = BulletGroup(purple_glow, 5)
#make functions
def background(colour): #to make it easier to draw the background each time
screen.fill(BG_COL)
pygame.draw.rect(screen, colour,(50, 50, 900, 575), 0)
def handle_keys():
""" Handles Keys """
key = pygame.key.get_pressed()
dist = 2
if key[pygame.K_DOWN]: # down key
player.y += dist # move down
elif key[pygame.K_UP]: # up key
player.y -= dist # move up
if key[pygame.K_RIGHT]: # right key
player.x += dist # move right
elif key[pygame.K_LEFT]: # left key
player.x -= dist # move left
#MAIN GAME LOOP
running = True
while running:
for events in pygame.event.get():
if events.type == QUIT:
pygame.quit()
exit()
running = False
if events.type == KEYDOWN:
background(S1_COL)
#BULLETS:
def move_bullets(bullets, xchange, ychange):
#MOVE THE CLONED BULLETS IN ONE CONSTANT DIRECTION
for b in bullets.listof:
b.x += xchange
b.y += ychange
screen.blit(b.sprite, (b.x, b.y))
pygame.draw.rect(screen, S1_COL, b.area, 0)
xchange += 10
pygame.time.delay(50)
#STAGE1 MAKE
screen.blit(pygame.transform.scale(Minty.sprite, (130, 140)), [Minty.x, Minty.y])
for events in pygame.event.get():
screen.blit(pygame.transform.scale(Minty.sprite, (130, 140)), [Minty.x, Minty.y]) #i'm going to change image size on phone later on
print(test_bullets.listof[0].x)
print(test_bullets.listof[0].y)
#IF KEY PRESSED
#MOVE PLAYER SPRITE
screen.blit(player.sprite, (player.x, player.y))
handle_keys()
move_bullets(test_bullets, -10, 10)
pygame.display.update()
Any feedback, explanation, edit suggests or something like that would be greatly appreciated.