python 3.x - stuck on pygame.draw.rect() -
im trying draw shape pygame.draw.rect() when press key shape wont stay when key or event happen , shape moves ever ship moves want move when shot , stay in place , shape wont stay on surface after release key
import pygame pygame.init() display_width = 800 display_hight = 600 colors = {"black": (0, 0, 0), "white": (255, 255, 255), "green": (0, 255, 0), "red": (255, 0, 0), "blue": (0, 0, 255)} ship_width = 75 ship_hight = 72 gamedisplay = pygame.display.set_mode((display_width, display_hight)) pygame.display.set_caption('test') clock = pygame.time.clock() shipimg = pygame.image.load('d:\\index.png') def ship(x, y): gamedisplay.blit(shipimg, (x, y)) def game_loop(): x = (display_width * 0.41) y = (display_hight * 0.8) x_change = 0 fire_start_y = y - ship_hight fire_start_x = x + (ship_width / 2) gameexit = false while not gameexit: if fire_start_y < 0: fire_start_y = y - 30 event in pygame.event.get(): if event.type == pygame.quit: gameexit = true 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.type == pygame.keyup: if event.key == pygame.k_left or event.key == pygame.k_right: x_change = 0 x += x_change gamedisplay.fill(colors["white"]) if event.type == pygame.keydown: if event.key == pygame.k_x: pygame.draw.rect(gamedisplay, colors["green"], [fire_start_x, fire_start_y, 2, 10]) ship(x, y) if x <= 0 or x >= display_width - ship_width: x -= x_change fire_start_y -= 5 fire_start_x = x + (ship_width / 2) pygame.display.flip() clock.tick(60) game_loop() pygame.quit()
i tried code manage disappearing problem made worse ever lopping after press key once
if event.type == pygame.keydown: if event.key == pygame.k_x: pygame.draw.rect(gamedisplay, colors["green"], [fire_start_x, fire_start_y, 2, 10]) fire_start_x = x + (ship_width / 2) elif event.type == pygame.keyup: if event.key == pygame.k_x: pygame.draw.rect(gamedisplay, colors["green"], [fire_start_x, fire_start_y, 2, 10]) fire_start_x = x + (ship_width / 2)
thanks guys found answer looking in how create bullets in pygame?
creating list of bullets , use append method add direction fo every bullet shot when press x key , removeing them list if out of game surface
here code looks
bullets = [] bullet_width = 2 bullet_hight = 10 def game_loop(): x = (display_width * 0.41) y = (display_hight * 0.8) x_change = 0 bullet_y = y - (ship_hight/2) gameexit = false while not gameexit: bullet_x = x + (ship_width / 2) event in pygame.event.get(): if event.type == pygame.quit: gameexit = true 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_x: bullets.append([bullet_x,bullet_y]) if event.type == pygame.keyup: if event.key == pygame.k_left or event.key == pygame.k_right: x_change = 0 x += x_change gamedisplay.fill(colors["white"]) bullet in bullets: pygame.draw.rect(gamedisplay, colors["green"], (bullet[0], bullet[1], bullet_width, bullet_hight)) ship(x, y) if x <= 0 or x >= display_width - ship_width: x -= x_change bullet in bullets: bullet[1] -= 5 if bullet[1] < 0: bullets.remove(bullet) pygame.display.flip() clock.tick(60)
Comments
Post a Comment