First, as soon as the mouse is clicked, it saves itself in the bullets_array list the angle of the shot (which is the same angle as the player). Second, speed is given to the shot through the 'cos' and 'sin' and then its movement is limited to a maximum of 640px and 480px by the loop (thus, if overtaken the shot will be erased). Third, the shot is displayed on the screen at player's position (player_x, player_y)
The shot was supposed to start on player_x, player_y, walk straight through the cosine and sine and then "die" at the edge of the screen.
But the following sentence is shown:
line 53
vel_x = math.cos (bullet [0]) * 10
TypeError: 'float' object has no attribute 'getitem'
Here is the code:
#
# Title: Shoot The Zombies
# Author(s): John Redbeard, ("http://john-redbeard.tumblr.com/")
#
import pygame
from pygame.locals import *
import math
# Initiate Pygame
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
pygame.display.set_caption("Shoot the Zombies")
fps = 60
fpsclock = pygame.time.Clock()
# Load images
player = pygame.image.load("images/player.png")
player_x = 100
player_y = 100
posplayer_x = 0
posplayer_y = 0
grass = pygame.image.load("images/grass.png")
bullet = pygame.image.load("images/bullet.png")
bullets_array = []
target = pygame.image.load("images/target.png")
# Main Loop Game
while True:
pygame.mouse.set_visible(False)
screen.fill(False)
# Load on screen the grass in isometric way
for x in range(width/grass.get_width()+1):
for y in range(height/grass.get_height()+1):
screen.blit(grass,(x*100,y*100))
# Load on screen the rotated-positioned-player
mouse_position = pygame.mouse.get_pos()
angle = math.atan2(mouse_position[0] - player_x, mouse_position[1] - player_y)
player_rotate = pygame.transform.rotate(player, 360+angle*57.29)
player_position = (player_x - player_rotate.get_rect().width/2, player_y - player_rotate.get_rect().height/2)
screen.blit(player_rotate, player_position)
# load on screen the rotated-posioted-bullets
for bullet in bullets_array:
vel_x = math.cos(bullet[0])*10
vel_y = math.sin(bullet[1])*10
bullet[0] += vel_x
bullet[1] += vel_y
if bullet[0] >640 or bullet[1] > 480:
bullets_array.remove(bullet)
bullet1 = pygame.transform.rotate(bullet, 360+angle*57.29)
screen.blit(bullet1, (player_x, player_y))
# Load on screen the target
screen.blit(target, (mouse_position))
# Display window
pygame.display.flip()
# Run events
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
exit(False)
# Event: W/A/S/D (Keyboard) moving player
elif event.type == KEYDOWN:
if event.key == K_w:
posplayer_y -= 3
elif event.key == K_a:
posplayer_x -= 3
elif event.key == K_s:
posplayer_y += 3
elif event.key == K_d:
posplayer_x += 3
elif event.type == KEYUP:
if event.key == K_w:
posplayer_y = 0
elif event.key == K_a:
posplayer_x = 0
elif event.key == K_s:
posplayer_y= 0
elif event.key == K_d:
posplayer_x = 0
# Event: Mouse click shoot the bullet
elif event.type == MOUSEBUTTONDOWN:
bullets_array.append(math.atan2(mouse_position[0] - player_x, mouse_position[1] - player_y))
player_x += posplayer_x
player_y +=posplayer_y
fpsclock.tick(fps)