My sprite image is not displaying in the pygame window. It's probably something really simple, but I don't understand why it isn't showing. I'm fairly new to programming, so it's probably why I don't understand this. Is it something I've done wrong in the class itself? Here is my code:
import pygame, sys
from pygame.locals import *
windowwidth = 600
windowheight = 600
WALLWIDTH = 30
WALLHEIGHT = 30
PLAYERWIDTH = 20
PLAYERHEIGHT = 20
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
LIMEGREEN = (50, 205, 50)
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('Green circle png.png')
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def changespeed(self, x, y):
self.change_x = x
self.change_y = y
def update_pos(self, x, y):
old_x_pos = self.rect.x
new_x_pos = old_x_pos + self.change_x
self.rect.x = new_x_pos
old_y_pos = self.rect.y
new_y_pos = old_y_pos + self.change_y
self.rect.x = new_y_pos
def main():
pygame.init()
thescreen = pygame.display.set_mode((windowwidth, windowheight))
pygame.display.set_caption('Maze Game')
thescreen.fill((BLACK))
mainclock = pygame.time.Clock()
running = True
while running == True:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN:
if (event.key == K_UP or event.key == K_w):
Player.changespeed = (-3, 0)
elif (event.key == K_DOWN or event.key == K_s):
Player.changespeed = (3, 0)
elif (event.key == K_LEFT or event.key == K_a):
Player.changespeed = (0, -3)
elif (event.key == K_RIGHT or event.key == K_d):
Player.changespeed = (0, 3)
if event.type == KEYUP:
if (event.key == K_UP or event.key == K_w):
Player.changespeed = (0, 0)
elif (event.key == K_DOWN or event.key == K_s):
Player.changespeed = (0, 0)
elif (event.key == K_LEFT or event.key == K_a):
Player.changespeed = (0, 0)
elif (event.key == K_RIGHT or event.key == K_d):
Player.changespeed = (0, 0)
mainclock.tick(60)
pygame.display.update()
pygame.quit()
if __name__ == '__main__':
main()
You need to draw the player on the screen. Call some code to draw it, (this is easiest to do in a render updates group but you could simply blit it as well). You probably also want to call the update function, but I won't include that in my example. Here is what you can do:
players = pygame.sprite.RenderUpdates()
players.add(Player(200,200))
def mainloop():
while running == True:
pygame.display.update(players.draw(thescreen))
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN:
if (event.key == K_UP or event.key == K_w):
Player.changespeed = (-3, 0)
elif (event.key == K_DOWN or event.key == K_s):
Player.changespeed = (3, 0)
elif (event.key == K_LEFT or event.key == K_a):
Player.changespeed = (0, -3)
elif (event.key == K_RIGHT or event.key == K_d):
Player.changespeed = (0, 3)
if event.type == KEYUP:
if (event.key == K_UP or event.key == K_w):
Player.changespeed = (0, 0)
elif (event.key == K_DOWN or event.key == K_s):
Player.changespeed = (0, 0)
elif (event.key == K_LEFT or event.key == K_a):
Player.changespeed = (0, 0)
elif (event.key == K_RIGHT or event.key == K_d):
Player.changespeed = (0, 0)
Related
I'm creating a basic game, but I have a problem with keyboard responding in my code. When you click on start, the cube is going all the way the direction of your pressed arrow key, but I want it to move any direction you press with arrow keys and it stops if no keys are pressed. Could someone show me what error do I have in my code and how to do it right?
Here is the code:
import pygame
import time
import sys
import pygame.event as EVENTS
from pygame.locals import *
import random
print("Welcome to the prototype of rpg game -Made by Adam Pospíchal.")
pygame.init()
sirka = 800
vyska = 800
okno = pygame.display.set_mode((sirka,vyska))
pygame.display.set_caption("RPG GAME")
#PARAMETRE OKNA#
#PARAMETRE OKNA#
#PARAMETRE KOCKY HRACA#
#PARAMETRE MENU#
#farby - RGB#
RM = 255
GM = 255
BM = 0
player_r = 0
player_g = 255
player_b = 0
player_height = 20
player_width = 20
smerx = 0
smery = 0
x_pos = 390
y_pos = 390
#farby - RGB#
x = 400
y = 400
#PARAMETRE MENU#
#TEXT#
font = pygame.font.SysFont('Arial', 40)
textsurface_title = font.render('CUBE ADVENTURE', False, (255, 0, 0))
textsurface_Controls = font.render('CONTROLS', False, (255, 0, 0))
textsurface = font.render('START', False, (255, 0, 0))
textsurface2 = font.render('CONTROLS', False, (255, 0, 0))
textsurface3 = font.render('EXIT', False, (255, 0, 0))
#TEXT#
#MAIN MENU#
obdlznik_start = pygame.Rect(300,200,200,100)
obdlznik_controls = pygame.Rect(300,350,200,100)
obdlznik_exit = pygame.Rect(300,500,200,100)
#MAIN MENU
def game():
x_pos = 360
y_pos = 360
smerx = 0
smery = 0
player_r = 0
player_g = 255
player_b = 0
player_height = 20
player_width = 20
sirka = 800
vyska = 800
while True:
for event in pygame.event.get():
if event.type == QUIT:
koniec()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
smerx = 1
if event.key == pygame.K_LEFT:
smerx = -1
if event.key == pygame.K_DOWN:
smery = 1
if event.key == pygame.K_UP:
smery = -1
if event.key == pygame.K_ESCAPE:
koniec()
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
smerx = 0
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
smery = 0
#BORDER
if x_pos + player_width > 800:
x_pos = 800
if x_pos < 0:
x_pos = 0
if y_pos + player_height > 800:
y_pos = 800
if y_pos < 0:
y_pos = 0
#BORDER
#PLAYER CUBE
okno.fill((0,0,0))
x_pos = x_pos + smerx
y_pos = y_pos + smery
obdlznik_player = pygame.Rect(x_pos,y_pos,player_height,player_width)
pygame.draw.rect(okno,(player_r,player_g,player_b),obdlznik_player)
pygame.display.update()
#PLAYER CUBE
#HEALTHBAR
#HEALTHBAR
def koniec(): #EXIT GAME
pygame.quit()
sys.exit()
def menu_controls(): #CONTROLS MENU
while True:
for event in pygame.event.get():
if event.type == QUIT:
koniec()
#FARBY KOCKY KURZORA
RR = 255
GR = 0
BR = 0
#FARBY KOCKY KURZORA
mouse_x,mouse_y = pygame.mouse.get_pos() #POZÍCIA MYŠI
mouse_rect = pygame.Rect(mouse_x-10,mouse_y-10,20,20) #KOCKA MYŠI
obdlznik_spat = pygame.Rect(10,10,40,40)
obdlznik_spat_2 = pygame.Rect(30,25,10,10)
spat_collision = mouse_rect.colliderect(obdlznik_spat)
if spat_collision:
RR = 0
GR = 0
BR = 255
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
menu_main()
pygame.mouse.set_visible(0) #VIDITEĽNOSŤ MYŠI
okno.fill((0,0,0))
okno.blit(textsurface_Controls,(300,50))#NADPIS
pygame.draw.rect(okno,(RM,GM,BM),obdlznik_spat)
pygame.draw.polygon(okno, (255,0,0), ((15,30), (30,15), (30,45)))
pygame.draw.rect(okno,(255,0,0),obdlznik_spat_2)
pygame.draw.rect(okno,(RR,GR,BR),mouse_rect) #KRESLENIE KOCKY KURZORA MYSI
pygame.display.update() #UPDATE OBRAZOVKY
def menu_main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
koniec()
#FARBY KOCKY KURZORA
RR = 255
GR = 0
BR = 0
#FARBY KOCKY KURZORA
mouse_x,mouse_y = pygame.mouse.get_pos() #POZÍCIA MYŠI
pygame.mouse.set_visible(0) #VIDITEĽNOSŤ MYŠI
mouse_rect = pygame.Rect(mouse_x-10,mouse_y-10,20,20) #KOCKA MYŠI
s_collision = mouse_rect.colliderect(obdlznik_start)
c_collision = mouse_rect.colliderect(obdlznik_controls)
e_collision = mouse_rect.colliderect(obdlznik_exit)
if s_collision:
RR = 0
GR = 255
BR = 0
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
game()
if c_collision:
RR = 0
GR = 0
BR = 255
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
menu_controls()
if e_collision:
RR = 255
GR = 255
BR = 255
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
koniec()
okno.fill((0,0,0))
okno.blit(textsurface_title,(250,50))#NADPIS
pygame.draw.rect(okno,(RM,GM,BM),obdlznik_start)
okno.blit(textsurface,(350,230))
pygame.draw.rect(okno,(RM,GM,BM),obdlznik_controls)
okno.blit(textsurface2,(315,370))
pygame.draw.rect(okno,(RM,GM,BM),obdlznik_exit)
okno.blit(textsurface3,(360,520))
pygame.draw.rect(okno,(RR,GR,BR),mouse_rect) #KRESLENIE KOCKY KURZORA MYSI
pygame.display.update() #UPDATE OBRAZOVKY
menu_main()
#MAIN MENU#
#HERNY CYKLUS - PRIKAZY V HRE#
The problem is that you are using two event loops inside game(). You must add all the events in the same event loop.
So,
for event in pygame.event.get():
if event.type == QUIT:
koniec()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
smerx = 1
if event.key == pygame.K_LEFT:
smerx = -1
if event.key == pygame.K_DOWN:
smery = 1
if event.key == pygame.K_UP:
smery = -1
if event.key == pygame.K_ESCAPE:
koniec()
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
smerx = 0
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
smery = 0
this ^ will become just
for event in pygame.event.get():
if event.type == QUIT:
koniec()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
smerx = 1
if event.key == pygame.K_LEFT:
smerx = -1
if event.key == pygame.K_DOWN:
smery = 1
if event.key == pygame.K_UP:
smery = -1
if event.key == pygame.K_ESCAPE:
koniec()
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
smerx = 0
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
smery = 0
So basically, I did a mistake in my code by using (2x for) in the game(). Next time when I will use (for), I will check if the commands below it are in the same cycle. Thanks for answer :)
I'm working on making a version of the game snake and I'm trying to prevent diagonal movement. Is there a way to prevent multiple KEYDOWN events
This is my helper function that detects KEYDOWN events.
def _keydown_events(self, event):
'''Responds to pressing keystrokes'''
if event.type == pg.KEYDOWN:
if event.key == pg.K_q:
pg.display.quit()
running = False
sys.exit()
elif event.key == pg.K_RIGHT:
self.snake.move_right = True
elif event.key == pg.K_LEFT:
self.snake.move_left = True
elif event.key == pg.K_UP:
self.snake.move_up = True
elif event.key == pg.K_DOWN:
self.snake.move_down = True
You have to ensure that just self.snake.move_right, self.snake.move_left, self.snake.move_up or self.snake.move_down is set, but never 2 or more states simultaneously. e.g:
def _keydown_events(self, event):
'''Responds to pressing keystrokes'''
if event.type == pg.KEYDOWN:
if event.key == pg.K_q:
pg.display.quit()
running = False
sys.exit()
elif if event.key in [pg.K_RIGHT, pg.K_LEFT, pg.K_UP, pg.K_DOWN]:
self.snake.move_right = False
self.snake.move_left = False
self.snake.move_up = False
self.snake.move_down = False
if event.key == pg.K_RIGHT:
self.snake.move_right = True
elif event.key == pg.K_LEFT:
self.snake.move_left = True
elif event.key == pg.K_UP:
self.snake.move_up = True
elif event.key == pg.K_DOWN:
self.snake.move_down = True
Anyway I recommend to remove the states self.snake.move_right, self.snake.move_left, self.snake.move_up or self.snake.move_down.
Use a single state which indicates the direction of the snake. Add a state move_dir and assign pygame.key.name(event.key). The value of move_dir becomes "right", "left", "up" respectively "down" for RIGHT, LEFT, UP or DOWN. e.g:
def _keydown_events(self, event):
'''Responds to pressing keystrokes'''
if event.type == pg.KEYDOWN:
if event.key == pg.K_q:
pg.display.quit()
running = False
sys.exit()
elif if event.key in [pg.K_RIGHT, pg.K_LEFT, pg.K_UP, pg.K_DOWN]:
# move_dir becomes "right", "left", "up" or "down"
self.snake.move_dir = pygame.key.name(event.key)
Of course you can state the move vector directly:
def _keydown_events(self, event):
'''Responds to pressing keystrokes'''
if event.type == pg.KEYDOWN:
if event.key == pg.K_q:
pg.display.quit()
running = False
sys.exit()
elif if event.key in [pg.K_RIGHT, pg.K_LEFT, pg.K_UP, pg.K_DOWN]:
move_dirs = {pg.K_RIGHT: (1, 0), pg.K_LEFT: (-1, 0),
pg.K_UP: (0, -1), pg.K_DOWN: (0, 1)}
self.snake.move_dir = move_dirs[event.key]
You may want to turn your movement direction variables into a single delta value instead:
if event.key == pg.K_RIGHT:
self.snake.move_dir = (+1, 0)
elif event.key == pg.K_LEFT:
self.snake.move_dir = (-1, 0)
elif event.key == pg.K_UP:
self.snake.move_dir = (0, -1)
elif event.key == pg.K_DOWN:
self.snake.move_dir = (0, +1)
and wherever you move your snake,
dx, dy = self.move_dir
x += dx
y += dy
I wrote this simple code that draws and then lets you move a rectangle, but even after I change the coordinates of the rectangle by calling the move_player_x_ function, it doesn't move at all. I don't understand why. I came here looking for clarification and a detailed solution to my problem.
Here's the code:
import pygame
white = (255, 255, 255)
black = (0, 0, 0)
class Game():
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
def __init__(self):
pass
def fill_screen(self, color):
self.color = color
self.screen.fill(self.color)
def update_method(self):
pygame.display.update()
game = Game()
class Player(pygame.sprite.Sprite):
lead_x = game.width/2
lead_y = game.height/2
lead_x_change = 0
lead_y_change = 0
velocity = 0.2
block_size = 10
def __init__(self):
pygame.sprite.Sprite.__init__(self)
def move_player_x_left(self):
self.lead_x_change += -self.velocity
def move_player_x_right(self):
self.lead_x_change += self.velocity
def move_player_y_up(self):
self.lead_y_change += -self.velocity
def move_player_y_down(self):
self.lead_y_change += self.velocity
def draw_player(self):
pygame.draw.rect(game.screen, black, [self.lead_x, self.lead_y, self.block_size, self.block_size])
def key_up_x_stop(self):
self.lead_x = 0
def key_up_y_stop(self):
self.lead_y = 0
def constant_x_movement(self):
self.lead_x += self.lead_x_change
def constant_y_movement(self):
self.lead_y += self.lead_y_change
player = Player()
exitGame = False
while not exitGame:
game.fill_screen(white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitGame = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player.move_player_y_up()
if event.key == pygame.K_s:
player.move_player_y_down()
if event.key == pygame.K_a:
player.move_player_x_left()
if event.key == pygame.K_d:
player.move_player_x_right()
if event.type == pygame.KEYUP:
if event.key == pygame.K_w or event.key == pygame.K_s:
player.key_up_y_stop()
if event.key == pygame.K_a or event.key == pygame.K_d:
player.key_up_x_stop()
player.constant_x_movement()
player.constant_y_movement()
player.draw_player()
game.update_method()
pygame.quit()
quit()
The code in the event loop is not indented correctly. Here's a corrected version:
while not exitGame:
game.fill_screen(white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitGame = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player.move_player_y_up()
if event.key == pygame.K_s:
player.move_player_y_down()
if event.key == pygame.K_a:
player.move_player_x_left()
if event.key == pygame.K_d:
player.move_player_x_right()
if event.type == pygame.KEYUP:
if event.key == pygame.K_w or event.key == pygame.K_s:
player.key_up_y_stop()
if event.key == pygame.K_a or event.key == pygame.K_d:
player.key_up_x_stop()
Also, in the ...stop methods, you have to set lead_x_change and lead_y_change to 0 not lead_x and lead_y.
def key_up_x_stop(self):
self.lead_x_change = 0
def key_up_y_stop(self):
self.lead_y_change = 0
I edited your code, i added a function in the player object move_player with local booleans bUp,bDown,bLeft,bRight. If python had enumerations, it would be so much better. Any how, on event KEY DOWN and KEY UP, they toggle these booleans in the player. after the input is calculated and these booleans are toggled/set, it called move_player() in player object that checks its booleans and sets a while loop while one is true and adds velocity in the respected location and redraws player.
Here the source i got for you...
import pygame
white = (255, 255, 255)
black = (0, 0, 0)
class Game():
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
def __init__(self):
pass
def fill_screen(self, color):
self.color = color
self.screen.fill(self.color)
def update_method(self):
pygame.display.update()
class Player(pygame.sprite.Sprite):
lead_x = 800/2
lead_y = 600/2
velocity = 0.002
block_size = 10
bUp = false
bDown = false
bLeft = false
bRight = false
def __init__(self):
pygame.sprite.Sprite.__init__(self)
def draw_player(self):
pygame.draw.rect(game.screen, black, [self.lead_x, self.lead_y, self.block_size, self.block_size])
def move_player(self):
while bLeft:
self.lead_x += -self.velocity
self.draw_player()
while bRight:
self.lead_x += self.velocity
self.draw_player()
while bUp:
self.lead_y += -self.velocity
self.draw_player()
while bDown:
self.lead_y += self.velocity
self.draw_player()
game = Game()
player = Player()
exitGame = False
while not exitGame:
game.fill_screen(white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitGame = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player.bUp = true
if event.key == pygame.K_s:
player.bDown = true
if event.key == pygame.K_d:
player.bRight = true
if event.key == pygame.K_a:
player.bLeft = true
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w:
player.bUp = false
if event.key == pygame.K_s:
player.bDown = false
if event.key == pygame.K_d:
player.bRight = false
if event.key == pygame.K_a:
player.bLeft = false
player.move_player()
game.update_method()
pygame.quit()
quit()
I wrote this simple program on Raspberry Pi from the user guide. The problem is, that when I run it, Python says that video system not declared in line 29 (for event in pygame.event.get():). I tried initializing pygame twice, but it didn't work. Any suggestions how can I fix it?
#!/usr/bin/env python
import pygame, sys, time, random
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
playSurface = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Raspberry Snake')
redColour = pygame.Color(255, 0, 0)
blackColour = pygame.Color(0, 0, 0)
whiteColour = pygame.Color(255, 255, 255)
greyColour = pygame.Color(150, 150, 150)
snakePosition = [100,100]
snakeSegments = [[100,100],[80,100],[60,100]]
raspberryPosition = [300,300]
raspberrySpawned = 1
direction = 'right'
changeDirection = direction
def gameOver():
gameOveerFont = pygame.font.Font('freesansbold.ttf', 72)
gameOverSurf = gameOverFont.render('Game Over', True, greyColour)
gameOverRect = gameOverSurf.get_rect()
gameOverRect.midtop = (320, 10)
playSurface.blit(gameOverSurf, gameOverRect)
pygame.display.flip()
time.sleep(5)
pygame.quit()
sys.exit()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT or event.key == ord('d'):
changeDirection = 'right'
if event.key == K_LEFT or event.key == ord('a'):
changeDirection = 'left'
if event.key == K_UP or event.key == ord('w'):
changeDirection = 'up'
if event.key == K_DOWN or event.key == ord('s'):
changeDirection = 'down'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if changeDirection == 'right' and not direction == 'left':
direction = changeDirection
if changeDirection == 'left' and not direction == 'right':
direction = changeDirection
if changeDirection == 'up' and not direction == 'down':
direction = changeDirection
if changeDirection == 'down' and not direction == 'up':
direction = changeDirection
if direction == 'right':
snakePosition[0] += 20
if direction == 'left':
snakePosition[0] -= 20
if direction == 'up':
snakePosition[1] -= 20
if direction == 'down':
snakePosition[1] += 20
snakeSegments.insert(0,list(snakePosition))
if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]:
raspberrySpawned = 0
else:
snakeSegments.pop()
if raspberrySpawned == 0:
x = random.randrange(1,32)
y = random.randrange(1,24)
raspberryPosition = [int(x*20),int(y*20)]
raspberrySpawned = 1
playSurface.fill(blackColour)
for position in snakeSegments:
pygame.draw.rect(playSurface,whiteColour,Rect (position[0], position[1], 20, 20))
pygame.draw.rect(playSurface,redColour,Rect (raspberryPosition[0], raspberryPosition[1], 20, 20))
pygame.display.flip()
if snakePosition[0] > 620 or snakePosition[0] < 0:
gameOver()
if snakePosition[1] > 460 or snakePosition[1] < 0:
gameOver()
for snakeBody in snakeSegments[1:]:
if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:
gameOver()
fpsClock.tick(20)}
1) Have you installed PyGame correctly?
2) If so... explore around with the locals. See if there's a specific pygame.video initilization.
3) Or, try using another IDE, like IDLE
I hope I helped!
import pygame
class Sprite:
def __init__(self, x, y, curren_time):
self.rect = pygame.Rect(x, y, 100, 110)
self.images = []
#Tells pygame the image list that you want to stitch together
for x in range(10):
img = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST6/Cernunnos" + str(x) +".PNG")
#Append the image to each other to create the animation effect
self.images.append( img )
self.current_image = 0
self.time_num = 100
self.time_target = curren_time + self.time_num
def update(self, curren_time):
if curren_time >= self.time_target:
self.time_target = curren_time + self.time_num
self.current_image += 1
if self.current_image == len(self.images):
self.current_image = 0
def render(self, window):
window.blit(self.images[self.current_image], self.rect)
#Colors
black = (0,0,0)
white = (255,255,255)
def main():
pygame.init()
window = pygame.display.set_mode((800,600))
pygame.display.set_caption("Sprites")
move_x, move_y = 0, 0
clock = pygame.time.Clock()
curren_time = pygame.time.get_ticks()
player = Sprite(110,100,curren_time)
font = pygame.font.SysFont(None, 150)
pause_text = font.render("PAUSE",1,white)
pause_rect = pause_text.get_rect( center = window.get_rect().center )
#Adding how many places the image moves when using the key function
state_game = True
state_pause = False
#So while True
while state_game:
curren_time = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
state_game = False
elif event.type == pygame.KEYDOWN:
if event.key ==pygame.K_ESCAPE:
state_game = False
elif event.key == pygame.K_SPACE:
state_pause = not state_pause
if event.key == pygame.K_LEFT:
move_x = -3
elif event.key == pygame.K_RIGHT:
move_y = 3
elif event.key == pygame.K_UP:
move_x = -3
elif event.key == pygame.K_DOWN:
move_y = 3
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
move_x = 0
elif event.key in (pygame.K_UP, pygame.K_DOWN):
move_y = 0
if not state_pause:
player.rect.x += move_x
player.rect.y += move_y
player.update(curren_time)
#Fills the window with a color
window.fill(black)
player.render(window)
if state_pause:
window.blit(pause_text,pause_rect)
pygame.display.flip()
clock.tick(50)
pygame.quit()
if __name__ == '__main__':
main()
#Code constructed by furas
So the problem is whenever I hit the right key the animation slides off the screen on the left. the animation does not stop when you take your finger off the key. I have searched for the problem myself and am unable to find any problems. Please if you see something that may be causing the problem let me know. Thanks! (Up key = Down, Down key = Down, Right key = Left, Left key = Left)
Your problem is that you have move_x and move_y swapped for K_RIGHT and K_UP.
elif event.key == pygame.K_RIGHT:
move_y = 3
elif event.key == pygame.K_UP:
move_x = -3
Should be:
elif event.key == pygame.K_RIGHT:
move_x = 3
elif event.key == pygame.K_UP:
move_y = -3