New to python and pygame. I was trying to make the ship move left or right continuously, which I did by creating a class "move_ship" and passing self.arguments as false. Then created a function inside which works when self.arguments are True and increase or decreases the x-position value of ship. Then called the class with variable "ship_moving". ...created a event which would set self.argument as true and at last called the function inside the class to move the ship:-
class move_ship():
def __init__(self):
self.moving_left=False
self.moving_right=False
def moving(self):
if self.moving_left:
ship_rect.centerx-=1
if self.moving_right:
ship_rect.centerx+=1
ship_moving=move_ship()
def run_game:
pygame.init()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type ==pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
ship_moving.moving_right=True
elif event.key == pygame.K_LEFT:
ship_moving.moving_left=True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship_moving.moving_right=False
elif event.key == pygame.K_LEFT:
ship_moving.moving_left=False
ship_moving.moving()
which worked perfectly fine but the I wanted to know why it didn't work when I tried the same thing but with function only.
moving_right=False
moving_left= False
def move_ship():
if moving_left:
ship_rect.centerx-=1
if moving_right:
ship_rect.centerx+=1
def run_game:
pygame.init()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type ==pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
moving_right=True
elif event.key == pygame.K_LEFT:
moving_left=True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
moving_right=False
elif event.key == pygame.K_LEFT:
moving_left=False
move_ship()
You must use the global statement if you want to set variables in global namespace within a function:
def run_game():
global moving_right, moving_left
pygame.init()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type ==pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
moving_right=True
elif event.key == pygame.K_LEFT:
moving_left=True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
moving_right=False
elif event.key == pygame.K_LEFT:
moving_left=False
If you do not specify moveing_right, moveing_left to be interpreted as global, only local variables with the same name are set in the scope of the run_game function, but the global variables are never changed.
Related
while gameStart == True:
startScreen()
for event in pygame.event.get():
if event.type == pygame.QUIT:
youLose = True
gameStart = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
moveY = -snek.fast
moveX = 0
gameStart = False
if event.key == pygame.K_ESCAPE:
youLose = True
gameStart = False
I have no trouble when I run the game the first time around. I'm trying to get the game to reset after I've lost but I keep getting the error:
if event.type == pygame.KEYDOWN:
UnboundLocalError: local variable 'event' referenced before assignment
I don't understand why this is giving me an error, and even less why it gives an error only on the second time around. I've tried indenting the block so as to include it inside the for loop, but it makes my game crash.
The keydown if-else statement should be inside the for-loop
while gameStart == True:
startScreen()
for event in pygame.event.get():
if event.type == pygame.QUIT:
youLose = True
gameStart = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
moveY = -snek.fast
moveX = 0
gameStart = False
if event.key == pygame.K_ESCAPE:
youLose = True
gameStart = False
import sys
import pygame
def check_events(ship):
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
def update_screen(ai_settings, screen, ship):
'''Update images on the screen and flip to the new screen.'''
Error:
File "/home/mark/python_work/game_functions.py", line 8
elif event.type == pygame.KEYDOWN:
^
SyntaxError: invalid syntax
[Finished in 0.2s with exit code 1]
In your function, your first condition should start with an if.
def check_events(ship):
if event.type == pygame.KEYDOWN: # <--- note the change here
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
You have an elif without an if preceding it. Just change the first elif to an if.
You cannot use elif before an if statement. Elif cannot be used without an if statement.
This is the right syntax.
import sys
import pygame
def check_events(ship):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
def update_screen(ai_settings, screen, ship):
'''Update images on the screen and flip to the new screen.'''
Good day. I am trying to make a ground move left and right when the opposite keys are pressed, but for some reason, the image just keeps moving to right. Here's the reasonable part of my code:
while not crashed and not timeOut and not Quit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Quit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
gx_change = 2.5
elif pygame.key == pygame.K_RIGHT:
gx_change = -2.5
if pygame.key == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
gx_change = 0
print (event)
gx += gx_change
As I said, it just keeps going right.
UPDATE: It is fixed
Thank you!!!
The problem is that your if statement for KEYUP is actually inside the if statement for KEYDOWN. This is the correct code:
while not crashed and not timeOut and not Quit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Quit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
gx_change = 2.5
elif pygame.key == pygame.K_RIGHT:
gx_change = -2.5
if pygame.key == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
gx_change = 0
print (event)
gx += gx_change
I know this question can be seen as a duplicate, but I spent some hours searching and figuring out what is wrong at my code.
My problem is that my object, called player, doesn't move constantly when left or right key is being held down:
for event in pygame.event.get():
if event.type == QUIT:
self.terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
self.terminate()
if event.key == K_LEFT or event.key == K_a:
self.moveRight = False
self.moveLeft = True
if event.key == K_RIGHT or event.key == K_d:
self.moveLeft = False
self.moveRight = True
if event.type == KEYUP:
if event.key == K_LEFT or event.key == K_a:
self.moveLeft = False
if event.key == K_RIGHT or event.key == K_d:
self.moveRight = False
# Move the player around
if self.moveLeft :
# Moves the player object to left with self.PLAYERMOVERATE pixels.
self.player.setLeftRight(-1 * self.PLAYERMOVERATE)
if self.moveRight :
self.player.setLeftRight(self.PLAYERMOVERATE)
I also tried this alternative:
while True:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.player.setLeftRight(-1 * self.PLAYERMOVERATE)
if keys[pygame.K_RIGHT]:
self.player.setLeftRight(self.PLAYERMOVERATE)
I think that the problem is that you are not handling the input in the main game loop.
In your code you seem to be handling the input inside a method of the object Player. This is not how input should be handled. In your second code example there is a while True: loop which will mean the loop is never exited from and thus the method's execution is never finished. I suspect that there may be a similar issue in your first example.
Instead you should:
Create all objects and classes.
Write the main game loop.
The main game loop should handle input, then process the logic of the game and then render whatever should be rendered.
Here is a short code example.
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # Exit from pygame window
quit() # End python thread
if event.type == KEYDOWN:
if event.key == K_LEFT or event.key == K_a:
player.moveRight = False
player.moveLeft = True
if event.key == K_RIGHT or event.key == K_d:
player.moveLeft = False
player.moveRight = True
if event.type == KEYUP:
if event.key == K_LEFT or event.key == K_a:
player.moveLeft = False
if event.key == K_RIGHT or event.key == K_d:
player.moveRight = False
# Move player using method
if player.moveLeft:
# Move player
# ...
# Render player
I hope that this helped you and if you have any further questions please feel free to post them in the comment section below!
I'm making a simple top-down game with moving and collision between player and walls, using pygame.
I made my code so that when keys[pygame.K_LSHIFT] is True during the pygame.key.get_pressed at the state of the keyboard when it's called, block_speed changes to 5 instead of 3. So it works and when I hold SHIFT and move with WASD keys at the same time my speed becomes 5.
Now, the problem is when I let go of SHIFT while still moving, my speed doesn't change back to 3 but stays at 5. But if I stop moving, then move, block_speed changes back to 3 as expected. It seems as if any change to block_speed only happens when I stop moving then move again.
I'm guessing it only receives the new value of block_speed after the current copy of block_speed is done being used.
How do I change block_speed with SHIFT while this variable is being used by the program?
def run(self):
pygame.init()
pygame.display.set_caption("Platformer v1")
block_speed = 3
sprite_list = pygame.sprite.Group()
wall_list = pygame.sprite.Group()
left_wall = Wall(0,0,10,resolution[1])
wall_list.add(left_wall)
sprite_list.add(left_wall)
top_wall = Wall(10,0,resolution[0]-10,10)
wall_list.add(top_wall)
sprite_list.add(top_wall)
test_wall = Wall(10,150,200,10)
wall_list.add(test_wall)
sprite_list.add(test_wall)
player = Player(50,50)
player.walls = wall_list
sprite_list.add(player)
while True:
self.screen.fill(black)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if keys[pygame.K_LSHIFT]:
block_speed = 5
else:
block_speed = 3
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
player.change_x += block_speed
elif event.key == pygame.K_a:
player.change_x += -block_speed
elif event.key == pygame.K_w:
player.change_y += -block_speed
elif event.key == pygame.K_s:
player.change_y += block_speed
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_a:
player.change_x = 0
elif event.key == pygame.K_w or event.key == pygame.K_s:
player.change_y = 0
sprite_list.update()
sprite_list.draw(self.screen)
pygame.display.update()
self.clock.tick(FPS)
Your issue is that when you add block speed to player.change_x/change_y, it performs that calculation at that time. Changing the variable afterwards won't make a difference.
A simple solution for your case is to simply make the change to block_speed happen before you use it. If you do this, block_speed has the value you want when it comes to changing the other variables, giving you the result you want.
Edit: I realise there is another issue - that is, you only change the movement speed if the key is newly pressed (the event is fired), so changing block speed still won't change the speed unless a direction key is pressed. The solution is to do your calculation every time, regardless.
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if keys[pygame.K_LSHIFT]:
block_speed = 5
else:
block_speed = 3
if keys[pygame.K_d]:
player.change_x += block_speed
if keys[pygame.K_a]:
player.change_x += -block_speed
if keys[pygame.K_w]:
player.change_y += -block_speed
if keys[pygame.K_s]:
player.change_y += block_speed
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_a:
player.change_x = 0
elif event.key == pygame.K_w or event.key == pygame.K_s:
player.change_y = 0
An alternative would be to store the direction when a key is pressed, then multiply this up by the speed at the time you do the movement each frame:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if keys[pygame.K_LSHIFT]:
player.block_speed = 5
else:
player.block_speed = 3
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
player.change_x += 1
elif event.key == pygame.K_a:
player.change_x -= 1
elif event.key == pygame.K_w:
player.change_y -= 1
elif event.key == pygame.K_s:
player.change_y += 1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_a:
player.change_x = 0
elif event.key == pygame.K_w or event.key == pygame.K_s:
player.change_y = 0
And then in your position code, do self.rect.x += self.change_x * self.block_speed instead. This is probably a preferable solution.
I've had the same issue and tried using the code from the suggested answer but found it did not work for me. My sprite still moved at the base speed until a key was lifted.
Looking at this thread, I tried moving the get_pressed() key detection outside of the event loop, which fixed things.
keys = pygame.key.get_pressed()
if keys[pygame.K_LSHIFT]:
player.block_speed = 5
else:
player.block_speed = 3
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
player.change_x += 1
#etc.
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_a:
player.change_x = 0
#etc.
As in the above answer, in your position code have self.rect.x += self.change_x * self.block_speed.
This allowed me to get a speed boost effect when a certain key was held down and revert back to the base speed when it was not.
import pygame
running = True
while running:
for event in pygame.event.get():
#K_UP mean releasing the key
if event.type == pygame.K_UP:
if event.key == pygame.K_LSHIFT:
block_speed = 3