Cannot move player on pygame - pygame

i am learning pygame.I get a little problem with moving and i dont know
how to make so that player move.i cannot understand where my mistake.Can you help me on this problem.And i will so appriciate if you give me some advice on pygame.I'm such a newbie
import pygame
widthSurface = 640
heightSurface = 480
keys = [False, False, False, False]
playerpos = [100, 100]
vel = 5
# images
player = pygame.image.load('resources/images/dude.png')
grass = pygame.image.load('resources/images/grass.png')
castle = pygame.image.load('resources/images/castle.png')
def blitGrassAndCastle():
for x in range(widthSurface // grass.get_width() + 1):
for y in range(heightSurface // grass.get_height() + 1):
surface.blit(grass, (x * grass.get_width(), y * grass.get_height()))
surface.blit(castle, (0, 30))
surface.blit(castle, (0, 135))
surface.blit(castle, (0, 240))
surface.blit(castle, (0, 345))
if __name__ == '__main__':
pygame.init()
pressed = pygame.key.get_pressed()
surface = pygame.display.set_mode((widthSurface, heightSurface))
while True:
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
if pressed[pygame.K_LEFT]:
playerpos[0] -= vel
if pressed[pygame.K_RIGHT]:
playerpos[0] += vel
if pressed[pygame.K_UP]:
playerpos[1] -= vel
if pressed[pygame.K_DOWN]:
playerpos[1] += vel
blitGrassAndCastle()
surface.blit(player, playerpos)
pygame.display.update()
Thank you in advanced!

pygame.key.get_pressed() returns a list with the current state of all keyboard buttons. You need to get the state of the keys ** in ** the application loop every frame, rather than once before the loop. The states you get before the loop will never change.
if __name__ == '__main__':
pygame.init()
# pressed = pygame.key.get_pressed() # <-- DELETE
surface = pygame.display.set_mode((widthSurface, heightSurface))
while True:
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
pressed = pygame.key.get_pressed() # <--- INSERT
if pressed[pygame.K_LEFT]:
playerpos[0] -= vel
if pressed[pygame.K_RIGHT]:
playerpos[0] += vel
if pressed[pygame.K_UP]:
playerpos[1] -= vel
if pressed[pygame.K_DOWN]:
playerpos[1] += vel
blitGrassAndCastle()
surface.blit(player, playerpos)
pygame.display.update()

Related

How can i move the Instances of the game on shifted y axis? [duplicate]

I'm a beginner programmer who is starting with python and I'm starting out by making a game in pygame.
The game basically spawns circles at random positions and when clicked, it gives you points.
Recently I've hit a roadblock when I want to spawn multiple instances of the same object (in this case circles) at the same time.
I've tried stuff like sleep() and some other code related to counters, but it always results in the next circle spawned overriding the previous one (i.e the program spawns circle 1, but when circle 2 comes in, circle 1 disappears).
Does anyone know a solution to this? I would really appreciate your help!
import pygame
import random
import time
pygame.init()
window = pygame.display.set_mode((800,600))
class circle():
def __init__(self, color, x, y, radius, width,):
self.color = color
self.x = x
self.y = y
self.radius = radius
self.width = width
def draw(self, win, outline=None):
pygame.draw.circle(win, self.color, (self.x, self.y, self.radius, self.width), 0)
run=True
while run:
window.fill((0, 0, 0))
pygame.draw.circle(window, (255, 255, 255), (random.randint(0, 800),random.randint(0, 600)), 20, 20)
time.sleep(1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run=False
pygame.quit()
quit()
It does not work that way. time.sleep, pygame.time.wait() or pygame.time.delay is not the right way to control time and gameplay within an application loop. The game does not respond while you wait. The application loop runs continuously. You have to measure the time in the loop and spawn the objects according to the elapsed time.
pygame.Surface.fill clears the entire screen. Add the newly created objects to a list. Redraw all of the objects and the entire scene in each frame.
See also Time, timer event and clock
You have 2 options. Use pygame.time.get_ticks() to measure the time. Define a time interval after which a new object should appear. Create an object when the point in time is reached and calculate the point in time for the next object:
object_list = []
time_interval = 500 # 500 milliseconds == 0.1 seconds
next_object_time = 0
while run:
# [...]
current_time = pygame.time.get_ticks()
if current_time > next_object_time:
next_object_time += time_interval
object_list.append(Object())
Minimal example:
repl.it/#Rabbid76/PyGame-TimerSpawnObjects
import pygame, random
pygame.init()
window = pygame.display.set_mode((300, 300))
class Object:
def __init__(self):
self.radius = 50
self.x = random.randrange(self.radius, window.get_width()-self.radius)
self.y = random.randrange(self.radius, window.get_height()-self.radius)
self.color = pygame.Color(0)
self.color.hsla = (random.randrange(0, 360), 100, 50, 100)
object_list = []
time_interval = 200 # 200 milliseconds == 0.2 seconds
next_object_time = 0
run = True
clock = pygame.time.Clock()
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
current_time = pygame.time.get_ticks()
if current_time > next_object_time:
next_object_time += time_interval
object_list.append(Object())
window.fill(0)
for object in object_list[:]:
pygame.draw.circle(window, object.color, (object.x, object.y), round(object.radius))
object.radius -= 0.2
if object.radius < 1:
object_list.remove(object)
pygame.display.flip()
pygame.quit()
exit()
The other option is to use the pygame.event module. Use pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue. The time has to be set in milliseconds. e.g.:
object_list = []
time_interval = 500 # 500 milliseconds == 0.1 seconds
timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, time_interval)
Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to be between pygame.USEREVENT (24) and pygame.NUMEVENTS (32). In this case pygame.USEREVENT+1 is the event id for the timer event.
Receive the event in the event loop:
while run:
for event in pygame.event.get():
if event.type == timer_event:
object_list.append(Object())
The timer event can be stopped by passing 0 to the time argument of pygame.time.set_timer.
Minimal example:
repl.it/#Rabbid76/PyGame-TimerEventSpawn
import pygame, random
pygame.init()
window = pygame.display.set_mode((300, 300))
class Object:
def __init__(self):
self.radius = 50
self.x = random.randrange(self.radius, window.get_width()-self.radius)
self.y = random.randrange(self.radius, window.get_height()-self.radius)
self.color = pygame.Color(0)
self.color.hsla = (random.randrange(0, 360), 100, 50, 100)
object_list = []
time_interval = 200 # 200 milliseconds == 0.2 seconds
timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, time_interval)
run = True
clock = pygame.time.Clock()
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == timer_event:
object_list.append(Object())
window.fill(0)
for object in object_list[:]:
pygame.draw.circle(window, object.color, (object.x, object.y), round(object.radius))
object.radius -= 0.2
if object.radius < 1:
object_list.remove(object)
pygame.display.flip()
pygame.quit()
exit()

Making a countdown timer in a Quiz game with pygame [duplicate]

I started using pygame and I want to do simple game. One of the elements which I need is countdown timer.
How can I do the countdown time (eg 10 seconds) in PyGame?
Another easy way is to simply use pygame's event system.
Here's a simple example:
import pygame
pygame.init()
screen = pygame.display.set_mode((128, 128))
clock = pygame.time.Clock()
counter, text = 10, '10'.rjust(3)
pygame.time.set_timer(pygame.USEREVENT, 1000)
font = pygame.font.SysFont('Consolas', 30)
run = True
while run:
for e in pygame.event.get():
if e.type == pygame.USEREVENT:
counter -= 1
text = str(counter).rjust(3) if counter > 0 else 'boom!'
if e.type == pygame.QUIT:
run = False
screen.fill((255, 255, 255))
screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
pygame.display.flip()
clock.tick(60)
On this page you will find what you are looking for http://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks
You download ticks once before beginning the countdown (which can be a trigger in the game - the key event, whatever).
For example:
start_ticks=pygame.time.get_ticks() #starter tick
while mainloop: # mainloop
seconds=(pygame.time.get_ticks()-start_ticks)/1000 #calculate how many seconds
if seconds>10: # if more than 10 seconds close the game
break
print (seconds) #print how many seconds
In pygame exists a timer event. Use pygame.time.set_timer() to repeatedly create an USEREVENT. e.g.:
timer_interval = 500 # 0.5 seconds
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event , timer_interval)
Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to be between pygame.USEREVENT (24) and pygame.NUMEVENTS (32). In this case pygame.USEREVENT+1 is the event id for the timer event.
To disable the timer for an event, set the milliseconds argument to 0.
Receive the event in the event loop:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == timer_event:
# [...]
The timer event can be stopped by passing 0 to the time parameter.
See the example:
import pygame
pygame.init()
window = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 100)
counter = 10
text = font.render(str(counter), True, (0, 128, 0))
timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, 1000)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == timer_event:
counter -= 1
text = font.render(str(counter), True, (0, 128, 0))
if counter == 0:
pygame.time.set_timer(timer_event, 0)
window.fill((255, 255, 255))
text_rect = text.get_rect(center = window.get_rect().center)
window.blit(text, text_rect)
pygame.display.flip()
pygame.time.Clock.tick returns the time in milliseconds since the last clock.tick call (delta time, dt), so you can use it to increase or decrease a timer variable.
import pygame as pg
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
font = pg.font.Font(None, 40)
gray = pg.Color('gray19')
blue = pg.Color('dodgerblue')
# The clock is used to limit the frame rate
# and returns the time since last tick.
clock = pg.time.Clock()
timer = 10 # Decrease this to count down.
dt = 0 # Delta time (time since last tick).
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
timer -= dt
if timer <= 0:
timer = 10 # Reset it to 10 or do something else.
screen.fill(gray)
txt = font.render(str(round(timer, 2)), True, blue)
screen.blit(txt, (70, 70))
pg.display.flip()
dt = clock.tick(30) / 1000 # / 1000 to convert to seconds.
if __name__ == '__main__':
main()
pg.quit()
There are several ways you can do this- here's one. Python doesn't have a mechanism for interrupts as far as I know.
import time, datetime
timer_stop = datetime.datetime.utcnow() +datetime.timedelta(seconds=10)
while True:
if datetime.datetime.utcnow() > timer_stop:
print "timer complete"
break
There are many ways to do this and it is one of them
import pygame,time, sys
from pygame.locals import*
pygame.init()
screen_size = (400,400)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("timer")
time_left = 90 #duration of the timer in seconds
crashed = False
font = pygame.font.SysFont("Somic Sans MS", 30)
color = (255, 255, 255)
while not crashed:
for event in pygame.event.get():
if event.type == QUIT:
crashed = True
total_mins = time_left//60 # minutes left
total_sec = time_left-(60*(total_mins)) #seconds left
time_left -= 1
if time_left > -1:
text = font.render(("Time left: "+str(total_mins)+":"+str(total_sec)), True, color)
screen.blit(text, (200, 200))
pygame.display.flip()
screen.fill((20,20,20))
time.sleep(1)#making the time interval of the loop 1sec
else:
text = font.render("Time Over!!", True, color)
screen.blit(text, (200, 200))
pygame.display.flip()
screen.fill((20,20,20))
pygame.quit()
sys.exit()
This is actually quite simple. Thank Pygame for creating a simple library!
import pygame
x=0
while x < 10:
x+=1
pygame.time.delay(1000)
That's all there is to it! Have fun with pygame!
Another way to do it is to set up a new USEREVENT for a tick, set the time interval for it, then put the event into your game loop
'''
import pygame
from pygame.locals import *
import sys
pygame.init()
#just making a window to be easy to kill the program here
display = pygame.display.set_mode((300, 300))
pygame.display.set_caption("tick tock")
#set tick timer
tick = pygame.USEREVENT
pygame.time.set_timer(tick,1000)
while 1:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.USEREVENT:
if event.type == tick:
## do whatever you want when the tick happens
print('My tick happened')

I'm trying to make a wild west game with pygame, but I can't get the shooting functions to work

I've been trying to make a wild west game in Pygame in which the player duels an NPC, but I can't get the shooting functions to work. I've basically tried to keep it simple, and I have a series of functions to make the bullet go across the screen, but they aren't working. Here's the code:
import pygame, time
pygame.init()
display_width = 800
display_height = 600
win = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('The Old Wild West')
black = (0,0,0)
white = (255,255,255)
clock = pygame.time.Clock()
dead = False
avatar = pygame.image.load('cowboy.jfif')
avatar2 = pygame.image.load('enemy.jfif')
bullet = pygame.image.load('bullet.png')
bx2 = 330
bx3 = 440
bx4 = 550
bx = 220
by = 220
bspeed = 165
x = 50
ex = 550
ey = 225
y = 200
x_change = 0
speed = 0
def cowboy(x,y):
win.blit(avatar, (x,y))
def enemy(ex, ey):
win.blit(avatar2, (ex, ey))
def shootframe1():
win.blit(bullet, (bx, by))
def antiframe1():
pygame.draw.rect(win, (255, 255, 255), (bx, by, 50, 150))
def shootframe2():
win.blit(bullet, (bx2, by))
def antiframe2():
pygame.draw.rect(win, (255, 255, 255), (bx2, by, 50, 150))
def shootframe3():
win.blit(bullet, (bx3, by))
def antiframe3():
pygame.draw.rect(win, (255, 255, 255), (bx3, by, 50, 150))
def shootframe4():
win.blit(bullet, (bx4, by))
def antiframe4():
pygame.draw.rect(win, (255, 255, 255), (bx4, by, 50, 150))
while not dead:
for event in pygame.event.get():
if event.type == pygame.QUIT:
dead = True
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
if event.type == pygame.K_SPACE:
if event.key == pygame.K_SPACE:
shootframe1()
time.sleep(0.5)
antiframe1()
shootframe2()
time.sleep(0.5)
antiframe2()
shootframe3()
time.sleep(0.5)
antiframe3()
shootframe4()
time.sleep(0.5)
antiframe4()
x += x_change
win.fill(white)
cowboy(x,y)
enemy(ex, ey)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
Here is cowboy.jfif, enemy.jfif and bullet.png:
bullet.png
cowboy.jfif
enemy.jfif
Please help me out with this confusion.
I updated your code drastically, I tried to comment everything pretty well so it's clear what is happening.
First, you were filling the screen white after drawing the bullet onto the screen so it never showed up.
Second, the space bar key didn't fire because you were checking if the space bar was an event type. So I moved it under the event.type == pygame.KEYDOWN
Third, using time.sleep(0.5) makes the game window freeze and stutter a lot so I would avoid doing that.
If you have any questions about the changes feel free to ask.
import pygame, time
pygame.init()
display_width = 800
display_height = 600
win = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('The Old Wild West')
black = (0,0,0)
white = (255,255,255)
clock = pygame.time.Clock()
dead = False
avatar = pygame.image.load('cowboy.jfif')
avatar2 = pygame.image.load('enemy.jfif')
bullet = pygame.image.load('bullet.png')
bx2 = 330
bx3 = 440
bx4 = 550
bx = 220
by = 220
bspeed = 165
x = 50
ex = 550
ey = 225
y = 200
x_change = 0
speed = 0
def cowboy(x,y):
win.blit(avatar, (x,y))
def enemy(ex, ey):
win.blit(avatar2, (ex, ey))
# A single shoot function that appends a new bullet to our bullet list below.
def shoot():
bullet_list.append(Bullet(bx, by))
# A bullet class that holds information about a single bullet
class Bullet:
def __init__(self, x, y):
# Initializing variables for the bullet
self.x = x
self.y = y
self.image = pygame.image.load('bullet.png')
self.bullet_speed = 165
# Moves the bullet accross the screen by updating the x variable after each draw()
def _move_bullet(self):
self.x += self.bullet_speed
def draw(self):
# Get the surface (window)
surface = pygame.display.get_surface()
# Blit the bullet to the screen
surface.blit(self.image, (self.x, self.y))
# Update its position with the function above
self._move_bullet()
bullet_list = [] # Since the user can have multiple bullets firing, we need to keep track of them
while not dead:
win.fill(white) #Move this to the top so you're filling the screen before drawing anything
for event in pygame.event.get():
if event.type == pygame.QUIT:
dead = True
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.key == pygame.K_SPACE: # Moved this up here
shoot() # Spawn the bullet
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
cowboy(x,y)
enemy(ex, ey)
# Draw each bullet onto the screen by iterating over the list
for bullet in bullet_list:
# If the bullet has exited the screen we remove it from the list so they are not kept in memory
if bullet.x > display_width:
bullet_list.remove(bullet)
# Draw the bullet (called from the Class)
bullet.draw()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()

Cannot shoot more than one projectile

Shooting one bullet works but i can only shoot one and after that it stops working. I've tried to change my "bullet_fire" to False after KEYUP but this doesnt work either since then the bullet is only visible while i hold the arrow up key while the problem remain. How should i solve this problem?
Here is the code:
import pygame
pygame.init()
# Skärm
Screen = pygame.display.set_mode((1600, 900))
# Title och Logo
pygame.display.set_caption("Fjärt Spel")
icon = pygame.image.load('planet.png')
pygame.display.set_icon(icon)
# backround
backround = pygame.image.load("0000.png")
# player
playerIMG = pygame.image.load("soldier.png")
playerX = 350
playerY = 836
playerX_Change = 0
playerY_Change = 30
# enemy
enemyIMG = pygame.image.load("terrorist2.png")
enemyX = 500
enemyY = 810
enemyX_Change = 0
enemyY_Change = 0
# bullet
bulletIMG = pygame.image.load("pung(32x32).png")
bulletX = playerX
bulletY = playerY
bulletX_Change = 50
bulletY_Change = 0
bullet_fire = False
offScreen = 1600, 900
def player(x, y):
Screen.blit(playerIMG, (x, y))
def enemy(x, y):
Screen.blit(enemyIMG, (x, y))
Running = True
while Running:
Screen.fill((0, 0, 0))
# backround
Screen.blit(backround, (0, 0))
if bullet_fire == True:
Screen.blit(bulletIMG, (bulletX + 100, playerY))
bulletX += bulletX_Change
if bulletX > 1600:
bullet_fire == False
for event in pygame.event.get():
if event.type == pygame.QUIT:
Running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_Change = -10
if event.key == pygame.K_RIGHT:
playerX_Change = 10
if event.key == pygame.K_SPACE:
playerY_Change = -70
if event.key == pygame.K_UP:
bullet_fire = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_SPACE:
playerX_Change = 0
playerY_Change = 30
# Ha player innan fill!!
playerX += playerX_Change
playerY += playerY_Change
if playerX <= 0:
playerX = 0
elif playerX >= 1436:
playerX = 1436
if playerY <= 0:
playerY = 0
elif playerY >= 836:
playerY = 836
player(playerX, playerY)
enemy(enemyX, enemyY)
pygame.display.update()
if bulletX > 1600:
bullet_fire == False
# ^^
This appears to want to set bullet_fire to False when it reaches a certain ordinate but, in reality, is comparing the two and throwing away the result. That means the first bullet you've fired is still going, has left the screen, and is probably halfway to Jupiter by now :-)
By way of example, this is similar to the following transcript:
>>> a = 7
>>> if a == 7:
... a == 14
... print(a)
...
False
7
You need to use == for comparison and = for assignment.
Keep in mind that's the likely cause of your issue. Since your question does not contain the code that decides whether a new bullet should be generated, it's a little hard to tell for sure. I'm therefore basing it on the assumption that one bullet at a time is all that's permitted (as in quite a few arcade games). Hence, if you never "destroy" the current bullet, you'll never be able to fire another one.
If that turns out to not be the case, you probably need to post more code so we can do an exhaustive analysis.
As an aside, I'm a little concerned about the physics involved in:
Screen.blit(bulletIMG, (bulletX + 100, playerY))
This appears to draw the bullet at the players current Y position meaning that, after the bullet is fired, it will track the player in some weird Einsteinian "spooky action at a distance" manner. While that makes the game a lot easier since you can direct the bullets after firing, I'm not convinced it will get a high score in the realism category :-)

Open AI gym and pygame: pygame.error: display Surface quit [duplicate]

i'm making a start menu for my game but when i hit the exit button i made in the start menu, it doesn't exit. Is there anything wrong with my code?
I tried making a function for the exit, put it in the code that exit the game with the window exit button, but nothing worked.
import pygame
import os
pygame.mixer.pre_init()
pygame.mixer.init(44100, 16, 2, 262144)
pygame.init()
from pygame.locals import*
pygame.mixer.music.load(os.path.join(os.getcwd(), 'Sounds',
'intro.ogg'))
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1)
FPS = 60
white = (255,255,255)
grey = (128,128,128)
black = (0,0,0)
red = (255,0,0)
orange = (255,128,0)
yellow = (255,255,0)
green = (0,255,0)
Lgreen = (128,255,0)
blue = (0,0,255)
Lblue = (0,255,255)
purple = (255,0,255)
pink = (255,0,127)
pygame.display.set_caption('Snake Universe')
Title = pygame.image.load('Graphics/Title.png')
Play = pygame.image.load('Graphics/Play.png')
Option = pygame.image.load('Graphics/Option.png')
Exit = pygame.image.load('Graphics/Exit.png')
LinePX = pygame.image.load('Graphics/LinePX.png')
LineO = pygame.image.load('Graphics/LineO.png')
clock = pygame.time.Clock()
movie = pygame.movie.Movie('Video/bg.mpg')
screen = pygame.display.set_mode((1280, 720))
bgE = pygame.image.load('Graphics/transparent.png')
movie_screen = pygame.Surface(movie.get_size()).convert()
movie.set_display(movie_screen)
movie.play()
y = 235
y1 = 3000
cnt = 0
playing = True
while playing:
cnt+=1
if cnt>=1870:
cnt=0
movie.rewind()
movie.play()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key==pygame.K_RETURN:
if y == 426:
movie.stop()
playing = False
pygame.quit()
quit()
if event.key == pygame.K_UP:
y += 1
if y == 3236:
y = 235
y1 = 3000
if y == 236:
y = 425
y1 = 3000
if y == 426:
y1 =335
y = 3235
if event.key == pygame.K_DOWN:
y += 1
if y == 236:
y = 3235
y1 = 335
if y == 3236:
y1 = 3000
y = 425
if y == 426:
y1 = 3000
y = 235
if event.type == pygame.QUIT:
movie.stop()
playing = False
pygame.quit()
quit()
screen.blit(movie_screen,(0, 0))
screen.blit(Title, (360, 0))
screen.blit(Play, (460, 250))
screen.blit(Exit, (460, 450))
screen.blit(LinePX, (482.5, y))
screen.blit(LineO, (482.5, y1))
screen.blit(Option, (460, 350))
screen.blit(bgE, (-100, 0))
pygame.display.update()
clock.tick(FPS)
i expected it to exit the window but instead it doesn't do anything.
The main loop runs as long as playing is True.
playing = True
while playing:
# [...]
When the pygame.QUIT event is handled, playing is set False the main loop condition is fails:
if event.type == pygame.QUIT:
playing = False
# [...]
Note, pygame.quit() doesn't terminate the loop, but it uninitialize all pygame modules, which will cause an exception in the following, if it is done in the middle of the application.
If you want to quit the application by the keypad enter key pygame.K_KP_ENTER, the you've to do the same when the pygame.KEYDOWN event is handled:
if event.type == pygame.KEYDOWN:
if event.key==pygame.K_KP_ENTER:
playing = False
Or you've to send a pygame.QUIT event by pygame.event.post():
if event.type == pygame.KEYDOWN:
if event.key==pygame.K_KP_ENTER:
pygame.event.post(pygame.event.Event(pygame.QUIT))