I am trying to build a basic helicopter game where the aim is to avoid hitting the blocks.
When I hit a block I want the game to freeze and then pressing the space bar will start a fresh game.
Here is my code:
Main.py
import pygame
from helicopter import Helicopter
from block import Block
import random
pygame.init()
win = pygame.display.set_mode((700,400))
w, h = pygame.display.get_surface().get_size()
clock = pygame.time.Clock()
blocks = []
score = 0
helicopter = Helicopter(100, 200)
def drawGameWindow():
helicopter.draw(win)
for block in blocks:
if block.visible:
block.draw(win)
else:
blocks.pop(blocks.index(block))
pygame.display.update()
def main():
run = True
blockLimiter = 0
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if blockLimiter > 0:
blockLimiter +=1
if blockLimiter > 100:
blockLimiter = 0
if blockLimiter == 0:
blocks.append(Block(random.randint(50, 350)))
blockLimiter += 1
for block in blocks:
if helicopter.hitbox[1] < block.hitbox[1] + block.hitbox[3] and helicopter.hitbox[1] + helicopter.hitbox[3] > block.hitbox[1]:
if helicopter.hitbox[0] + helicopter.hitbox[2] > block.hitbox[0] and helicopter.hitbox[0] < block.hitbox[0] + block.hitbox[2]:
helicopter.hit()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
helicopter.y -= abs(helicopter.speed)
else:
helicopter.y += helicopter.speed
drawGameWindow()
pygame.quit()
main()
block.py
import pygame
class Block(object):
def __init__(self, y):
self.x = 700
self.y = y
self.height = 70
self.width = 40
self.visible = True
self.hitbox = (self.x -3, self.y -3, self.width + 6, self.height + 6)
def draw(self, win):
if self.x + self.width < 0:
self.visible = False
self.x -= 5
self.hitbox = (self.x -3, self.y -3, self.width + 6, self.height + 6)
pygame.draw.rect(win, (0, 255, 0), (self.x, self.y, self.width, self.height))
pygame.draw.rect(win, (255,0,0), self.hitbox, 2)
helicopter.py
import pygame
class Helicopter(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 70
self.height = 30
self.speed = 1 * (self.y - 100)*0.05
self.hitbox = (self.x - 3, self.y - 3, self.width + 6, self.height + 6)
self.alive = True
def draw(self, win):
win.fill((0,0,0))
self.hitbox = (self.x - 3, self.y - 3, self.width + 6, self.height + 6)
pygame.draw.rect(win, (0, 255, 0), (self.x, self.y, 70, 30))
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
def hit(self):
self.alive = False
while not self.alive:
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
self.alive = True
self.x = 100
self.y = 200
What I want to happen:
helicopter hits block -> helicopter.hit()
is called -> helicopter.alive
is made False
-> Game is checking for space button to be pressed at which point it helicopter.alive becomes True the x,y coordinates of the helicopter is reset and the game starts over (I am yet to implement scoring but the scoring will reset).
What actually happens is the game crashes when I hit a block.
Can anyone explain how I can fix this?
Thanks.