Re: "unexpected argument"
On 3/9/2021 3:03 PM, Quentin Bock wrote: Error 1: Space Invaders.py:90: SyntaxWarning: "is" with a literal. Did you mean "=="? if bullet_state is "fire": Multiple string objects can have the same string value. Just because bullet_state == 'fire' does not mean that it *is* the particular object being compared to. Leaving testing aside, 'is' should only be used when the language mandates that there can only be one object with the value you want. 'is None' is the most common proper use. Using 'is' when you should use '==' is a common and hard to understand beginner bug. Error 2: line 66, in if event.key == pygame.K_SPACE: AttributeError: 'Event' object has no attribute 'key' # Check whether keystroke is being pressed if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player_X_change = -6 if event.key == pygame.K_RIGHT: player_X_change = 6 if event.key == pygame.K_SPACE: fire_bullet(player_X, bullet_Y) For other keys, you check event.type first. Here you do not. At some point, you asked for the key for a non-key event. Put this statement under the keyup or keydown event type check, depending on when you want fire to happen. if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: player_X_change = 0 -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: "unexpected argument"
On 2021-03-09 20:03, Quentin Bock wrote: Error 1: Space Invaders.py:90: SyntaxWarning: "is" with a literal. Did you mean "=="? if bullet_state is "fire": Error 2: line 66, in if event.key == pygame.K_SPACE: AttributeError: 'Event' object has no attribute 'key' Code: import pygame import random # Space Invaders! [snip] I see 2 problems: 1. You define "fire_bullet" to take 0 arguments, but then try to call it with 2 arguments. 2. What attributes an event has depends on the type of event. For example, KEYDOWN events have a "key" attribute, but most other types of event don't. Your code tries to look at the "key" attribute even when it's not a KEYDOWN event because of the amount of indentation. -- https://mail.python.org/mailman/listinfo/python-list
Re: "unexpected argument"
On Tue, 9 Mar 2021 15:03:54 -0500, Quentin Bock wrote: > Error 1: > Space Invaders.py:90: SyntaxWarning: "is" with a literal. Did you mean "=="? > if bullet_state is "fire": > > Error 2: > line 66, in > if event.key == pygame.K_SPACE: > AttributeError: 'Event' object has no attribute 'key' > > Code: > import pygame > import random [snip] > > Why is it saying unexpected argument? > Youtube Tutorial I'm Following: > https://www.youtube.com/watch?v=FfWpgLFMI7w Who is saying "unexpected argument"? I see two error messages, neither of which is "unexpected argument". -- To email me, substitute nowhere->runbox, invalid->com. -- https://mail.python.org/mailman/listinfo/python-list
"unexpected argument"
Error 1: Space Invaders.py:90: SyntaxWarning: "is" with a literal. Did you mean "=="? if bullet_state is "fire": Error 2: line 66, in if event.key == pygame.K_SPACE: AttributeError: 'Event' object has no attribute 'key' Code: import pygame import random # Space Invaders! background = pygame.image.load('space_invader_background.png') # Title and Icon pygame.display.set_caption ("Space Invaders") icon = pygame.image.load ('space-invaders.png') pygame.display.set_icon (icon) # Player player_Image = pygame.image.load ('player.png') player_X = 370 player_Y = 480 player_X_change = 0 # Enemy enemy_Image = pygame.image.load ('invader_enemy.png.') enemy_X = random.randint(0, 800) enemy_Y = random.randint(50, 150) enemy_X_change = 5 enemy_Y_change = 35 # Bullet bullet_Image = pygame.image.load ('bullet.png') bullet_X = 0 bullet_Y = 480 bullet_X_change = 0 bullet_Y_change = 10 bullet_state = "ready" def player(x, y): screen.blit (player_Image, (x, y)) def enemy(x, y): screen.blit (enemy_Image, (x, y)) def fire_bullet(): global bullet_state bullet_state = "fire" screen.blit(bullet_Image, (x + 16, y + 10)) # Game Loop pygame.init() screen = pygame.display.set_mode ((800, 600)) running = True while running: screen.fill ((0, 0, 0)) screen.blit(background, (0, 0)) for event in pygame.event.get (): if event.type == pygame.QUIT: running = False # Check whether keystroke is being pressed if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player_X_change = -6 if event.key == pygame.K_RIGHT: player_X_change = 6 if event.key == pygame.K_SPACE: fire_bullet(player_X, bullet_Y) if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: player_X_change = 0 #Player Movement and Boundaries player_X += player_X_change if player_X <= 0: player_X = 0 elif player_X >= 736: player_X = 736 #Enemy Movement and Boundaries enemy_X += enemy_X_change if enemy_X <= 0: enemy_X_change = 4 enemy_Y += enemy_Y_change elif enemy_X >= 736: enemy_X_change = -4 enemy_Y += enemy_Y_change # Bullet Movement if bullet_state == "fire": fire_bullet(player_X, bullet_Y) bullet_Y -= bullet_Y_change player(player_X, player_Y) enemy(enemy_X, enemy_Y) pygame.display.update() Why is it saying unexpected argument? Youtube Tutorial I'm Following: https://www.youtube.com/watch?v=FfWpgLFMI7w -- https://mail.python.org/mailman/listinfo/python-list