I am making a simply game with pygame, and the objective is to dodge the incoming enemies. I want a tie fighter image to come down randomly, and the xwing has to dodge them or you will die. How do I implement random tie fighters into my script? In my code I get an error that says x is not defined.
import pygame
import time
import random
pygame.init()
display_width = 1280
display_height= 800
pygame.display.set_mode((display_width, display_height), pygame.FULLSCREEN)
gameDisplay= pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('X-Wing Game')
clock= pygame.time.Clock()
black= (0,0,0)
white= (255,255,255)
red = (255,0,0)
blue_violet = (138,43,226)
xwing_width = 65
xwing_height = 130
tie_width = 80
tie_height =64
#images
xwingImg = pygame.image.load('X-Wing.bmp')
tieImg= pygame.image.load('tiefighter.png')
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
gameDisplay.blit(tieImg,(x,y))
def tieImg (x,y):
pygame.image.load('tieImg')
def xwing (x,y):
gameDisplay.blit (xwingImg,(x,y))
def text_objects(text, font):
textSurface = font.render(text, True, red)
return textSurface, textSurface.get_rect()
def crash ():
message_display ('Ouch, You Crashed!')
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',25)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2), (display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(1.3)
game_loop()
def game_loop():
x = (display_width * 0.45)
y = (display_height * .75)
x_change = 0
y_change = 0
thing_startx = random.randrange(0, display_width)
thing_starty = -600
thing_speed = 7
thing_width = 81
thing_height =65
gameEXIT = False
while not gameEXIT:
for event in pygame.event.get() :
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_UP:
y_change= -5
if event.key == pygame.K_DOWN:
y_change =5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change=0
x += x_change
y += y_change
gameDisplay.fill(black)
things(thing_startx, thing_starty, thing_width, thing_height, black )
thing_starty += thing_speed
xwing (x,y)
if x > display_width - xwing_width or x < 0:
x_change=0
if y>display_height-xwing_height or y<0:
y_change=0
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()