I am trying to create a game in pygame that involves one character shooting bullets at enemies on the screen (a bullethell type). I have created classes for the main protagonist and for the main enemy and have them all on one main script.
import pygame
import numpy as np
from apple_class import *
from snake_class import *
from explosion_class import *
from bullets_class import *
score = 0
pygame.init()
screen_width = 640
screen_height = 400
FPS = 40
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
snake = Snake()
apple = Apple()
explosion = Explosion()
apple_size = 12
snake_size = 8
bulletlist = []
def CollisionDet(x1, y1, x2, y2, size1, size2):
if x1 >= x2 and x1 <= x2 + size2 or x1 + size1 >= x2 and x1 + size1 <= x2 + size2:
if y1 >= y2 and y1 <= y2 + size2:
return True
elif y1 + size1 >= y2 and y1 + size1 <= y2 + size2 or y2 >= y1 and y2 + size2 <= y1 + size1:
return True
running = 1
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = False
score += 0.1
key = pygame.key.get_pressed()
if snake.hit_points > 0:
snake.handle_keys()
screen.fill((255,255,255))
snake.draw(screen)
##VARIOUS APPLE MOVEMENTS##
if apple.x <= 0 or apple.x == 0 and apple.y <= 0:
side = 1
if apple.y <= 0:
apple.y = 0
if apple.x < 620 and side == 1:
apple.move_par()
elif apple.x >= 620:
side = 2
elif apple.y >= 380:
side = 3
if side == 2:
apple.move_par_right()
if side == 3:
apple.move_par_bot()
apple.draw(screen)
if CollisionDet(snake.x, snake.y, apple.x, apple.y, snake_size, apple_size):
snake.hit_points -= 1
print(snake.hit_points)
##FIRING BULLETS##
if key[pygame.K_SPACE]:
bullet = Bullets(snake.x + 5, snake.y)
bulletlist.append(bullet)
for b in bulletlist:
b.move()
b.draw(screen)
if CollisionDet(b.x, b.y, apple.x, apple.y, 5, apple_size):
apple.x = 0
apple.y = 0
##GAMEOVER, BLIT EXPLOSION IMAGE##
if snake.hit_points <= 0:
print('you lose, dingo')
explosion.x = snake.x
explosion.y = snake.y
explosion.draw(screen)
pygame.display.update()
clock.tick(FPS)
I am able to get the main character (snake) to fire bullets at the constantly moving target (apple) when I press space. The problem is a very strange one however, whenever I move diagonally upward as the snake, I cannot fire bullets at the same time. Here is the snake class:
import pygame
pygame.init()
class Snake():
def __init__(self):
self.image = pygame.image.load('snakehead2.png')
self.x = 320 #width/2
self.y = 200 #height/2
self.hit_points = 5
def handle_keys(self):
#GET_PRESSED RETURNS TRUE OR FALSE VALUES
key = pygame.key.get_pressed()
dist = 5
if key[pygame.K_DOWN]:
self.y += dist
elif key[pygame.K_UP]:
self.y -= dist
if key[pygame.K_RIGHT]:
self.x += dist
elif key[pygame.K_LEFT]:
self.x -= dist
def draw(self, surface):
surface.blit(self.image, (self.x, self.y))
Essentially, whenever I press K_UP and K_LEFT and K_SPACE all at the same time, the space button does not seem to render. Is there something wrong with the snake class or the main code? Thanks for the help.