python - How do I get random enemies to appear from list? -




i'm trying random car appear list of cars saved in variables. issue seems chooses random car list , doesn't change while game being played. i'd change car each time previous car goes off screen.

each time game starts chooses 1 car random list, not change car each time game loop runs.

i believe problem lies in bit of code

randomcars = [car1, car2, car3, car4, car5, car6, car7, car8] enemy = random.choice(randomcars) 

i have pasted full code below:

import pygame import time import random  pygame.init()  #############  #############  display_width = 800 display_height = 600  black = (0, 0, 0) white = (255, 255, 255) red = (200, 0, 0) green = (0, 200, 0) bright_red = (255, 0, 0) bright_green = (0, 255, 0) block_color = (53, 115, 255)  crash_sound = pygame.mixer.sound("crash.mp3")  car_width = 55  gamedisplay = pygame.display.set_mode((display_width, display_height)) pygame.display.set_caption('super racer') clock = pygame.time.clock()  gameicon = pygame.image.load('caricon.png')  backgroundimage = pygame.image.load("background.png") backgroundimage = pygame.transform.scale(backgroundimage, (800, 600)) gamedisplay.blit(backgroundimage, (0, 0))      pygame.display.set_icon(gameicon)  pause = false   # crash = true  def score(count):     font = pygame.font.sysfont("comicsansms", 25)     text = font.render("score: " + str(count), true, red)     gamedisplay.blit(text, (0, 0))  def load_image(name_img):     car = pygame.image.load(name_img)     car = pygame.transform.scale(car, (60, 100)) # resize graphic     return car.convert_alpha() # remove whitespace graphic  carimg = load_image('racecar.png') enemies_list = ['diablo.png', 'aventador.png', 'nsx.png', 'bike.png', 'mach6.png', 'speeder.png', 'stingray.png', 'slr.png' ] # add other cars randomcars = [load_image(img) img in enemies_list]  def things(enemy, thingx, thingy, thingw, thingh, color):     gamedisplay.blit(enemy, [thingx, thingy, thingw, thingh])  def car(x, y):     gamedisplay.blit(carimg, (x, y))   def text_objects(text, font):     textsurface = font.render(text, true, black)     return textsurface, textsurface.get_rect()   def crash():     ####################################      pygame.mixer.sound.play(crash_sound)     pygame.mixer.music.stop()     ####################################     largetext = pygame.font.sysfont("comicsansms", 115)     textsurf, textrect = text_objects("you crashed", largetext)     textrect.center = ((display_width / 2), (display_height / 2))     gamedisplay.blit(textsurf, textrect)      while true:         event in pygame.event.get():             if event.type == pygame.quit:                 pygame.quit()                 quit()          button("play again", 150, 450, 100, 50, green, bright_green, game_loop)         button("quit", 550, 450, 100, 50, red, bright_red, quitgame)          pygame.display.update()         clock.tick(15)   def button(msg, x, y, w, h, ic, ac, action=none):     mouse = pygame.mouse.get_pos()     click = pygame.mouse.get_pressed()      if x + w > mouse[0] > x , y + h > mouse[1] > y:         pygame.draw.rect(gamedisplay, ac, (x, y, w, h))         if click[0] == 1 , action != none:             action()     else:         pygame.draw.rect(gamedisplay, ic, (x, y, w, h))     smalltext = pygame.font.sysfont("comicsansms", 20)     textsurf, textrect = text_objects(msg, smalltext)     textrect.center = ((x + (w / 2)), (y + (h / 2)))     gamedisplay.blit(textsurf, textrect)   def quitgame():     pygame.quit()     quit()   def unpause():     global pause     pygame.mixer.music.unpause()     pause = false   def paused():     ############     pygame.mixer.music.pause()     #############     largetext = pygame.font.sysfont("comicsansms", 115)     textsurf, textrect = text_objects("paused", largetext)     textrect.center = ((display_width / 2), (display_height / 2))     gamedisplay.blit(textsurf, textrect)      while pause:         event in pygame.event.get():             if event.type == pygame.quit:                 pygame.quit()                 quit()          button("continue", 150, 450, 100, 50, green, bright_green, unpause)         button("quit", 550, 450, 100, 50, red, bright_red, quitgame)          pygame.display.update()         clock.tick(15)   def game_intro():     intro = true      while intro:         event in pygame.event.get():             # print(event)             if event.type == pygame.quit:                 pygame.quit()                 quit()          gamedisplay.fill(white)         largetext = pygame.font.sysfont("comicsansms", 115)         textsurf, textrect = text_objects("super racer", largetext)         textrect.center = ((display_width / 2), (display_height / 2))         gamedisplay.blit(textsurf, textrect)          button("leggo!", 150, 450, 100, 50, green, bright_green, game_loop)         button("quit", 550, 450, 100, 50, red, bright_red, quitgame)          pygame.display.update()         clock.tick(15)   def game_loop():     global pause     enemy = random.choice(randomcars)     ############      pygame.mixer.music.load('bgmusic.mp3')     pygame.mixer.music.play(-1)     ############     x = (display_width * 0.45)     y = (display_height * 0.8)      x_change = 0      thing_startx = random.randrange(0, display_width)     thing_starty = -600     enemy_speed = 4     thing_width = 55     thing_height = 95     enemy = random.choice(randomcars)     thingcount = 1      dodged = 0      gameexit = false      while not gameexit:          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_right:                     x_change = 5                 if event.key == pygame.k_p:                     pause = true                     paused()              if event.type == pygame.keyup:                 if event.key == pygame.k_left or event.key == pygame.k_right:                     x_change = 0          x += x_change          gamedisplay.blit(backgroundimage, (0, 0))          things(enemy, thing_startx, thing_starty, thing_width, thing_height, block_color)          thing_starty += enemy_speed         car(x, y)         score(dodged)          if x > display_width - car_width or x < 0:             crash()          if thing_starty > display_height:             thing_starty = 0 - thing_height             thing_startx = random.randrange(0, display_width)             dodged += 1             #enemy_speed += .25             if dodged % 5 == 0:                 enemy_speed += (dodged * 1)           if y < thing_starty + thing_height:             #print('y crossover')              if x > thing_startx , x < thing_startx + thing_width or x + car_width > thing_startx , x + car_width < thing_startx + thing_width:                 #print('x crossover')                 crash()          pygame.display.update()         clock.tick(60)   game_intro() game_loop() pygame.quit() quit() 

here's minimal example. use random.choice new car out of list current car leaves screen.

import random import pygame   pygame.init()  car_img1 = pygame.surface((30, 50)) car_img1.fill((10, 150, 200)) car_img2 = pygame.surface((30, 50)) car_img2.fill((210, 150, 0)) car_img3 = pygame.surface((30, 50)) car_img3.fill((10, 250, 0))  cars = [car_img1, car_img2, car_img3]   def main():     screen = pygame.display.set_mode((640, 480))     screen_rect = screen.get_rect()     clock = pygame.time.clock()     car = random.choice(cars)     pos_x = random.randrange(screen_rect.width-30)     pos_y = -50      done = false      while not done:         event in pygame.event.get():             if event.type == pygame.quit:                 done = true          pos_y += 15         if pos_y > screen_rect.height:             car = random.choice(cars)             pos_x = random.randrange(screen_rect.width-30)             pos_y = -50          screen.fill((30, 30, 30))         screen.blit(car, (pos_x, pos_y))          pygame.display.flip()         clock.tick(30)   if __name__ == '__main__':     pygame.init()     main()     pygame.quit() 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -