pygame.display.update causing syntax error - python

I have a python program using pygame, but when I run it it gives me a syntax error on the pygame.display.update line. I'm not sure what is causing it. Any help is appreciated!
import pygame, sys
import time
from pygame.locals import *
pygame.init()
area = pygame.display.set_mode((400, 300))
red=(255, 0, 0)
black=(0, 0, 0)
fps = 30
clock=pygame.time.Clock()
x=100
y=100
a=0
b=0
pygame.display.set_caption('Reddy For School')
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
b=-10
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
b=10
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
a=-10
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
a=10
if event.type == pygame.KEYUP and event.key == pygame.K_UP:
b=0
if event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
b=0
if event.type == pygame.KEYUP and event.key == pygame.K_LEFT:
a=0
if event.type == pygame.KEYUP and event.key == pygame.K_RIGHT:
a=0
x+=a
y+=b
pygame.draw.rect(area, red, (x, y, 100, 100)
python.display.update()
area.fill(black)
clock.tick(fps)

As per the comments, you're missing a parentheses.
I'm not sure what the python.display.update() was trying to achieve, but I guess flush the display-updates to the screen. I substituted this with pygame.display.flip().
There's a couple of painting issues too - the code is drawing a red square, but then filling the screen with black. These needed to be re-ordered.
Also your main event-loop was not exiting cleanly (under Linux, I could not close the window with the titlebar (x) ). And finally, you had not parameterised the window width and height. Soon you will need to calculate something that relies on the window width (e.g.: centering something), so it is best to start with these in a parameter instead of having "400" & "300" all over the code.
import pygame, sys
import time
from pygame.locals import *
pygame.init()
window_width = 400
window_height = 300
area = pygame.display.set_mode((window_width, window_height))
red=(255, 0, 0)
black=(0, 0, 0)
fps = 30
clock=pygame.time.Clock()
x=100
y=100
a=0
b=0
pygame.display.set_caption('Reddy For School')
running = True
while running:
for event in pygame.event.get():
# check for closing the window
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
b=-10
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
b=10
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
a=-10
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
a=10
if event.type == pygame.KEYUP and event.key == pygame.K_UP:
b=0
if event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
b=0
if event.type == pygame.KEYUP and event.key == pygame.K_LEFT:
a=0
if event.type == pygame.KEYUP and event.key == pygame.K_RIGHT:
a=0
x+=a
y+=b
area.fill(black)
pygame.draw.rect(area, red, (x, y, 100, 100) )
clock.tick(fps)
pygame.display.flip() #quicker process to draw things
pygame.quit()

Related

key.event confusion // pygame.K_a doesn't work

I'm new in this python industry, when i press 'd' my character should go right, but this isn t hapening, same thing to the 'a'. When I change K_a and K_d with K_LEFT and K_RIGHT it works, my character is moving well, but if I wanna make the character move with 'a' and 'd', it doesn't work, what's wrong with my code:
import pygame
import sys
import os
pygame.init()
screen = pygame.display.set_mode((1366,768))
pygame.display.set_caption("Adventure in the Woods")
icon = pygame.image.load('tree.png')
pygame.display.set_icon(icon)
#Player
playerImg = pygame.image.load('ninja.png')
playerX = 100
playerY = 650
velocity = 0.1
run = True
while run:
screen.fill( (9, 66, 2) )
screen.blit(playerImg, (playerX,playerY) )
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
print('a')
playerX -= velocity
if event.key == pygame.K_d:
print('d')
playerX += velocity
pygame.display.update()
Problem Insight
The problem lies in the scope of your if event.type == pygame.KEYDOWN: ... as it should be within the for-loop for event in pygame.event.get(): ... :
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN: #<-------------- From here
if event.key == pygame.K_a:
print('a')
playerX -= velocity
if event.key == pygame.K_d:
print('d')
playerX += velocity #<-------------- to here
Solution
This can be fixed by indenting the code block starting from if event.type == pygame.KEYDOWN: ..., like so:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN: #<-------------- Now inside the for-loop
if event.key == pygame.K_a:
print('a')
playerX -= velocity
if event.key == pygame.K_d:
print('d')
playerX += velocity
Other Remarks
To be more familiar with python code structures and styles, review some of the fundamental syntaxes like Indentation by visiting the style guide # https://www.python.org/dev/peps/pep-0008/
Cheers!

My player is not moving correctly, when the key is held down, it should move continuoulsy, but only moves once

I'm trying to write something simple in pygame, but this is something I don't understand.
I'm trying to add moving but it's not continuous. I press and hold the key but the key only goes through 1 x_change
import pygame, sys
from pygame.locals import *
pygame.init()
gDis=pygame.display.set_mode((400,300))
Black=(0,0,0)
White=(255,255,255)
Red=(255,0,0)
Blue=(0,0,255)
Silver=(192,192,192)
clock=pygame.time.Clock()
x=195
y=270
wid=20
hght=20
speed=5
x_change=0
y_change=0
left=False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == K_LEFT:
x_change-=5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change=0
x+=x_change
gDis.fill(Black)
pygame.draw.rect(gDis, Silver, (x, y, wid, hght))
clock.tick(60)
pygame.display.update()
It is a matter of Indentation. You must move and draw the player in the application loop instead of the event loop:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == K_LEFT:
x_change = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
# INDENTATION
#<--|
x += x_change
gDis.fill(Black)
pygame.draw.rect(gDis, Silver, (x, y, wid, hght))
pygame.display.update()
clock.tick(60)

pygame chromebook key names

I am currently following a pygame tutorial on a chromebook on which i have installed and linux to use IDLE. i am writing a block of code which assigns and x-axis increase or decrase to the arrow keys:
import pygame
pygame.init()
displayWidth = 800
displayHeight = 600
black = (0, 0, 0)
white = (255, 255, 255)
clock = pygame.time.Clock()
crashed = False
carImg = pygame.image.load('racecar.png')
def car(x,y):
gameDisplay.blit(carImg, (x,y))
x = (displayWidth * 0.45)
y = (displayHeight * 0.8)
x_change = 0
car_speed = 0
gameDisplay = pygame.display.set_mode((displayHeight, displayWidth))
pygame.display.set_caption('Zoomer')
clock = pygame.time.Clock()
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
car(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
when i try to run the code, the car sprite won't budge. is this due to the fact that i am on chromebook and key names are different or is it an other reason? thanks.
It doesn't work because you have wrong indentations.
You check pygame.KEYDOWN inside if ... pygame.QUIT: which is executed only when you close window.
You need all if event.type start in the same column
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0

Moving a drawn rectangle

I have the game come up and the rectangle rendered.
When I press my KEYDOWN it doesn't move the rectangle, it just makes it longer.
I have tried tons of stuff. I am new to Pygame.
Any help would be amazing.
Here is the code:
import pygame
import time
import random
import math
import sys
pygame.init()
display_width = 1200
display_height = 800
white = (255,255,255)
black = (0,0,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Vertical Pong')
clock = pygame.time.Clock()
def pongBoard(x,y,):
pygame.draw.rect(gameDisplay,white,(x,y,250,25))
def gameLoop():
x = 325
y = 750
xChange = 0
inGame = True
while inGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
xChange = -5
print("Left")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
xChange = 5
print("Right")
if event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
xChange = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
xChange = 0
pongBoard(x,y)
x += xChange
pygame.display.update()
clock.tick(60)
gameLoop()
pygame.quit()
quit()
So the problem is this:
The rectangle is being constantly redrawn at a different coord, but the screen is not being drawn over the rectangle to cover up the part that is not supposed to be there. In simpler terms, we need to constantly draw the background.
So now the code in the main game loop:
while inGame:
#This code below draws the background
pygame.draw.rect(gameDisplay, black, (0, 0, display_width, display_height))
That is it! The background will constantly cover up the pong ball, and the pong ball will be constantly blitted to a new position!
P.S, there is a better way to do arrow key movement here: How to get keyboard input in pygame?
it actualy does move it, but the old one just stays there, making it look like it does not move but just grows. one way to change that would be to change the old ones color to the background color
try this code it works :-)
import pygame
import time
import random
import math
import sys
pygame.init()
display_width = 1200
display_height = 800
white = (255,255,255)
black = (0,0,0)
red = (123,11,45)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Vertical Pong')
clock = pygame.time.Clock()
def pongBoard(x,y,xold):
pygame.draw.rect(gameDisplay,white,[x,y,250,25])
pygame.draw.rect(gameDisplay,red,[xold,y,250,25])
def gameLoop():
x = 325
y = 750
xChange = 0
inGame = True
while inGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
xChange = -50
pongBoard(x,y,xold)
print("Left")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
xChange = 50
pongBoard(x,y,xold)
print("Right")
if event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
xChange = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
xChange = 0
xold = x
x += xChange
xold = x-xChange
pygame.display.update()
clock.tick(60)
gameLoop()
pygame.quit()
quit()

keys do not move the object in the game written in python

Update: manish kumar gave me the solution for the problem. I had to write event.key instead of event.type in a certain part of the code.
check it out at it below:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
I am trying to make a simple game by using Pygame.
Simply I have written code to move a car to the left and right.
Everything works well except that the keys do not move the car.
here is the code:
import pygame
from pygame.locals import *
pygame.init()
black = (0,0,0)
white = (255,255,255)
GD1 = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Racing!")
clock = pygame.time.Clock()
carimg = pygame.image.load("C:/Users/Abdulaziz/Downloads/my_app___car_sprite_5_by_nicolaspok-d65xysp.png")
def car(x,y):
GD1.blit(carimg,(x,y))
x = (800 * 0.45)
y = (600 * 0.7)
x_change = 0
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
x_change = -5
elif event.type == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
GD1.fill(white)
car(x,y)
pygame.display.flip()
clock.tick(60)
pygame.quit()
quit()
the problem is probably in this part of the code:
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
x_change = -5
elif event.type == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
Again, the car appears but do not move.
you have to use event.key instead of event.type.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
I think the problem lies here:
for event in pygame.event.get():
# modifications to x_change
x += x_change
If the call to pygame.event.get() returns two events, a KEYDOWN and a KEYUP, then x_change will be set to 5 or -5, but then it will be set back to 0.
You should update x at each iteration:
for event in pygame.event.get():
# modifications to x_change
x += x_change

Categories

Resources