2
First, as soon as the mouse is clicked, the shooting angle ( that is the same angle of the player) is stored in the bullets_array list. Second, it is given speed to the shot through the 'cos' and 'sin' and then limits its movement to at most 640px and 480px by the loop (so if it passes the shot it will be erased). Third, it is shown on the screen the shot in the position of the Player (player_x, player_y)
The shot was supposed to start at player_x, player_y, walk straight through the cosine and sine and then "die" at the screen limits.
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)
Has this problem been solved? Were any of the answers helpful? If yes, mark it as accepted.
– Miguel Angelo