python - How can i make the ship shoot bullets properly? -


i creating ship game can shoot down enemies , upgrade ship. have not done because have problem bullets.

before started point out using python 2.7 , pygame 1.9. aware there later versions of pygame , python started writing before knew there later versions of pygame python 3.4.

i point out basic coder , not know of advanced things yet.

import pygame import time  pygame.init()  white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255)  display_width = 800 display_height = 600  gamedisplay = pygame.display.set_mode((display_width,display_height)) pygame.display.set_caption('destroyer')  clock = pygame.time.clock() fps = 60  font = pygame.font.sysfont(none, 25)  shipw = 69 shiph = 88  #~~~~~~~~~~~~~~~~~~~other functions~~~~~~~~~~~~~~~~~#    #~~~~~~~~~~~~~~~~~~~~~~the game~~~~~~~~~~~~~~~~~~~~~#  def game_loop():      shipimg = pygame.image.load('ship1.png')      face = 1      #the direction ship facing.      #1 = north, 2 = east, 3 = south, 4 = west      x = (display_width * 0.45 )#location of ship - x axis     y = (display_height * 0.45) #location of ship - y axis      x_change = 0 #change in location of ship - x axis     y_change = 0 #change in locations of ship - y axis      #start of bullets      #1      lgun = x + 9 #location of left gun     rgun = x + 59 #location of right gun      ybul = 0 #change in location of bullet - y axis     xbul = 0 #change in loaction of bullet - x axis      fire = 0                           #this record direction of ship when bullet fired      #end of bullets      ybul_change = 0     xbul_change = 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:       #the next 4 lines rotating ship left                     shipimg = pygame.transform.rotate(shipimg,90)                     face -= 1                     face = (face - 1) % 4                     face += 1                 elif event.key == pygame.k_right:    #the next 4 lines rotating ship right                     shipimg = pygame.transform.rotate(shipimg,-90)                     face -= 1                     face = (face + 1) % 4                     face += 1                 elif event.key == pygame.k_up:                     if face == 1:                         y_change = -5                         x_change = 0                     elif face == 2:                         x_change = 5                         y_change = 0                     elif face == 3:                         y_change = 5                         x_change = 0                     elif face == 4:                         x_change = -5                         y_change = 0                 elif event.key == pygame.k_down:                     if face == 1:                         y_change = 5                         x_change = 0                     elif face == 2:                         x_change = -5                         y_change = 0                     elif face == 3:                         y_change = -5                         x_change = 0                     elif face == 4:                         x_change = 5                         y_change = 0                 elif event.key == pygame.k_s:                     y_change = 0                     x_change = 0                 elif event.key == pygame.k_pageup:                     if face == 1:                         x_change = -5                         y_change = 0                     elif face == 2:                         y_change = -5                         x_change = 0                     elif face == 3:                         x_change = 5                         y_change = 0                     elif face == 4:                         y_change = 5                         x_change = 0                 elif event.key == pygame.k_pagedown:                     if face == 1:                         x_change = 5                         y_change = 0                     elif face == 2:                         y_change = 5                         x_change = 0                     elif face == 3:                         x_change = -5                         y_change = 0                     elif face == 4:                         y_change = -5                         x_change = 0                 elif event.key == pygame.k_f:                     fire = face                 #record direction of fire                     if fire == 1 or fire == 3:  #if facing north or south                         xbul_change = 0         #no movement - x axis                         xbul = 0                #not facing east or west, location 0                         lgun = x + 9            #location of left gun - x axis                         rgun = x + 59           #location of right gun - x axis                         if fire == 1:           #if facing north                             ybul_change = -5    #bullets go                             ybul = y - 10       #location of bullet when firing                         else:                             ybul_change = 5     #bullets go down                             ybul = y + 88       #location of bullet when firing - y axis                     else: #facing east or west                         ybul_change = 0         #no movement - y axis                         ybul = 0                #not facing north or south,location 0                         lgun = y + 59           #location of left gun - y axis                         rgun = y + 9            #location of right gun - y axis                         if fire == 2:           #if facing west                             xbul_change = 5     #bullets supposed  move right move down                             xbul = x + 88       #location of bullets, don't know wrong doesn't work                         else:                             xbul_change = -5    #bullets supposed  move left move                             xbul = x - 10       #location of bullets, don't know wrong doesn't work            gamedisplay.fill(green)          gamedisplay.blit(shipimg,(x,y))          if fire == 1 or fire == 3:                                   #if facing north or south             pygame.draw.rect(gamedisplay, red, [lgun, ybul, 2, 10])             pygame.draw.rect(gamedisplay, red, [rgun, ybul, 2, 10])         elif fire == 2 or fire == 4:                                 #if facing east or west             pygame.draw.rect(gamedisplay, blue, [lgun, xbul, 10, 2]) #the bullets not drawing in correct position , don't know why             pygame.draw.rect(gamedisplay, blue, [rgun, xbul, 10, 2]) #the bullets not drawing in correct position , don't know why           ybul += ybul_change             #add change in location of bullet current location - y axis         xbul += xbul_change             #add change in location of bullet current location - x axis          y += y_change                   #add change in location of ship current location - y axis         x += x_change                   #add change in location of ship current location - x axis          pygame.display.update()         clock.tick(fps)   game_loop() pygame.quit() quit() 

the ship shoot bullets correctly both guns when facing or facing down, however, when facing left or facing right, bullets still next 1 instead of on top of 1 guns , move or down instead of left or right.

here link ship: http://tinypic.com/r/2zszjv4/8

in case other link doesn't work: http://i61.tinypic.com/2zszjv4.jpg

i don't know wrong code, rewrote code twice couldn't figure out problem, please help.

the problem in following section. exchange xbul's , lgun's position , fine

    elif fire == 2 or fire == 4:                                 #if facing east or west         pygame.draw.rect(gamedisplay, blue, [ xbul,lgun, 10, 2]) #the bullets not drawing in correct position , don't know why         pygame.draw.rect(gamedisplay, blue, [ xbul,rgun, 10, 2]) #the bullets not drawing in correct position , don't know why 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -