“pygame.error: No video mode has been set” even after pygame.display.set_mode has been assigned
I've done research on this error and it seems all the solutions lie in needing to do something like "screen = pygame.display.set_mode(x, y)", which I've done, however the video mode error still occurs.
further research just shows that the solution lies in adding a line of code I've already implemented. I've tried moving the location to within the loop which plays when my game is being ran, however I cannot seem to get it working.
here is my full code. the "screen = set_mode" code is found on line 11 and screen.blit on line 222
import pygame
import time
progress = 0
pygame.init()
(width, height) = (600, 400) #specify window resolution
background = pygame.image.load("spacebackground.png").convert()
bgx = 0
bgy = 0
screen = pygame.display.set_mode((width, height)) #create window
pygame.display.set_caption('EduGame') #specify window name
player_path = "downChara.png" #specifies image path
class Player(object): #representitive of the player's overworld sprite
def __init__(self):
self.image = pygame.image.load(player_path).convert_alpha() #creates image, the player_path variable allowing it to be updated
self.X = (width/2) -16; # x co-ord of player
self.Y = (height/2)-16; # y co-ord of player
self.width = self.image.get_width()
self.height = self.image.get_height()
self.hitbox = (self.X, self.Y, self.width, self.height)
def handle_keys(self, up, down, left, right): #handling the keys/inputs
key = pygame.key.get_pressed()
dist = 3 #distance travelled in one frame of the program
if key[pygame.K_DOWN] and down == True: #if down
self.Y += dist #move down the length of dist
player_path = "downChara.png" #change image to down
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_UP] and up == True: #if up
self.Y -= dist #move up the length of dist
player_path = "upChara.png" #change to up
self.image = pygame.image.load(player_path).convert_alpha()
if key[pygame.K_RIGHT] and right == True: #etc.
self.X += dist
player_path = "rightChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_LEFT] and left == True:
self.X -= dist
player_path = "leftChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
def outX(coord): #"coord" acts the same as "self"
return (coord.X)
def outY(coord):
return (coord.Y)
def draw(self, surface): #draw to the surface/screen
surface.blit(self.image, (self.X, self.Y))
#pygame.draw.rect(screen, (255, 255, 255), self.hitbox, 2)
return self.X, self.Y, self.width, self.height
class NPC(object): #NPC class
def __init__(self, path, x, y,text):
self.image = pygame.image.load(path).convert_alpha()
self.x = x
self.y = y
self.width = self.image.get_width()
self.height = self.image.get_height()
self.hitbox = (self.x, self.y, self.width, self.height)
self.text = text
def spawn(self, surface): #NPC spawn location
surface.blit(self.image, (self.x, self.y))
self.hitbox = (self.x, self.y, self.width, self.height)
#pygame.draw.rect(screen, (255, 255, 255), self.hitbox, 2)
def collide(self, x, y, width, height): #collision with player character
key = pygame.key.get_pressed()
othery = self.y + self.height
otherx = self.x + self.width
upperx = otherx - self.width/10
lowerx = self.x + self.width/10
#uppery = self.y + self.height/10
#lowery = othery - self.height/10
if self.y <= y <= othery and self.x <= x <= otherx or self.y <= y + height <= othery and self.x <= x <= otherx or self.y <= y <= othery and self.x <= x + width <= otherx or self.y <= y + height <= othery and self.x <= x + width <= otherx:
if key[pygame.K_RETURN]:
setup(self.text)
#older code:
#if phitbox == self.hitbox:
## if uppery <= y <= othery: #toggles if you're touching an entity or not
## self.touchEntityBelow = True
## print("up")
## else:
## self.touchEntityBelow = False
## if self.y <= y <= lowery: #etc
## self.touchEntityAbove = True
## print("down")
## else:
## self.touchEntityAbove = False
## if upperx <= x <= otherx:
## self.touchEntityRight = True
## print("right")
## else:
## self.touchEntityRight = False
## if self.x <= x <= lowerx:
## self.touchEntityLeft = True
## print("left")
## else:
## self.touchEntityLeft = False
## else:
## self.touchEntityRight = False
## self.touchEntityLeft = False
## self.touchEntityAbove = False
## self.touchEntityBelow = False
## def grabValues(self, x, y):
## return self.touchEntityBelow, self.touchEntityAbove, self.touchEntityLeft, self.touchEntityRight
def text_objects(text, font): #code for rendering the text in the colour and font
textSurface = font.render(text, True, (255, 255, 255))
return textSurface, textSurface.get_rect()
def interact(text): #code for blitting text to screen, specifies font and textbox.
textbox = pygame.transform.scale(pygame.image.load("bigbox.png"), (600, 111))
textSize = pygame.font.Font("cour.ttf",28) #specify text size
TextSurf, TextRect = text_objects(text, textSize) #allow text to be positioned
TextRect.topleft = (12, 297) #where text will be
screen.blit(textbox, (0, 289))
screen.blit(TextSurf, TextRect) #display text
pygame.display.update() #updates screen
time.sleep(2)
screen.blit(background, TextRect)
def playerMove(posX, posY, width, height):
if posX > width - 32: #this is because the sprite's "X" is determined in the top left corner, meaning we have to subtract the width from the measurement
moveRight = False #following selection loops determine if the player can move. may need to be moved into the collision code so it does not intervene with NPC
#collisions
else:
moveRight = True
if posX < 0:
moveLeft = False
else:
moveLeft = True
if posY > height - 32: #this is because the sprite's "Y" is determined in the top left corner, meaning we have to subtract the width from the measurement
moveDown = False
else:
moveDown = True
if posY < 0:
moveUp = False
else:
moveUp = True
return moveDown, moveUp, moveLeft, moveRight
##def checker(array):
## ## check first value of each part of array (all numbers)
## ## compare it to progress
## ## if equal to or less than, cycle through rest of that part of array.
## ## if greater than, then ignore.
## ## e.g: progress = 49, NPC1 will still be on text "0", NPC2 will now be on "33" and NPC3 will be on "0"
##
## placeholderList =
##
## for x in range(len(array)):
## if array[x][0] <= progress:
## del placeholderList[0:]
## placeholderList.append(array[x][1:])
## for x in range(len(placeholderList)):
## passMe = placeholderList[x]
## print (passMe)
## npc.interact(passMe)
player = Player() #instance of Player()
playerx, playery, playerwidth, playerheight= player.draw(screen) # gets the players coordinates and measurements
clock = pygame.time.Clock() #instance of the Clock() from the pygame module that specifies fps
personText = ("hello","hi","bye") #some placeholder text
lizardText = ("IM A LIZARD, IM A WIZARD,","IM THE LIZARD WIZARD!")
person1 = NPC("talkToThis.png",100, 200, personText) #instance of NPC(), an entity
lizard = NPC("lizardWizard.png", 300, 250, lizardText)
#npc = NPC("test.png",0, 0) #another NPC() entity
def setup(text): #if an NPC says more than one line of text, this makes sure that they can all be said in succession
for x in range(len(text)):
passtext = text[x]
interact(passtext)
boarderX = player.outX() #code for assigning window boarders
boarderY = player.outY()
##print (boarderX, boarderY) #making sure they both returned properly
pygame.display.flip() #paints screen
gameRun = True #allow game events to loop/be carried out more than once
while gameRun: #while game is running:
##person1text2 = [[0,"beginng","www","xxdqsd"],[1,"middle","aa"],[2,"end!"]]
personText = ("hello","hi","bye") #some placeholder text
playerx, playery, playerwidth, playerheight = player.draw(screen) #gets new player coords
#print(playerx, playery, playerwidth, playerheight) # prints them for testing reasons
person1.collide(playerx, playery, playerwidth, playerheight) # runs collision code to check for collision
lizard.collide(playerx, playery, playerwidth, playerheight)
#entityUp, entityDown, entityLeft, entityRight = person1.grabValues(playerx, playery)
event = pygame.event.poll() #assigns the pygame event code to the variable event
if event.type == pygame.QUIT: #if the "x" is pressed
pygame.quit() #quit game
gameRun = False #break the loop.
quit()
## if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
#### checker(person1text2)
## setup(personText)
screen.blit(background, (bgx, bgy)) #draw background colour
player.draw(screen) #draws player
person1.spawn(screen) #spawns an NPC entity
lizard.spawn(screen)
moveDown, moveUp, moveLeft, moveRight = playerMove(playerx, playery, width, height)
player.handle_keys(moveUp, moveDown, moveLeft, moveRight) #handle keys
pygame.display.update() #updates display
posX = player.outX() # gets player positions
posY = player.outY()
clock.tick(60) #clock cycle
the error is as follows:
Traceback (most recent call last):
File "C:UsersadamhDesktopCOMP3iteration1OVERWORLDalphaVER6.py", line 8, in <module>
background = pygame.image.load("spacebackground.png").convert()
pygame.error: No video mode has been set
so there is an error on the 8th line specifically.
python pygame
add a comment |
I've done research on this error and it seems all the solutions lie in needing to do something like "screen = pygame.display.set_mode(x, y)", which I've done, however the video mode error still occurs.
further research just shows that the solution lies in adding a line of code I've already implemented. I've tried moving the location to within the loop which plays when my game is being ran, however I cannot seem to get it working.
here is my full code. the "screen = set_mode" code is found on line 11 and screen.blit on line 222
import pygame
import time
progress = 0
pygame.init()
(width, height) = (600, 400) #specify window resolution
background = pygame.image.load("spacebackground.png").convert()
bgx = 0
bgy = 0
screen = pygame.display.set_mode((width, height)) #create window
pygame.display.set_caption('EduGame') #specify window name
player_path = "downChara.png" #specifies image path
class Player(object): #representitive of the player's overworld sprite
def __init__(self):
self.image = pygame.image.load(player_path).convert_alpha() #creates image, the player_path variable allowing it to be updated
self.X = (width/2) -16; # x co-ord of player
self.Y = (height/2)-16; # y co-ord of player
self.width = self.image.get_width()
self.height = self.image.get_height()
self.hitbox = (self.X, self.Y, self.width, self.height)
def handle_keys(self, up, down, left, right): #handling the keys/inputs
key = pygame.key.get_pressed()
dist = 3 #distance travelled in one frame of the program
if key[pygame.K_DOWN] and down == True: #if down
self.Y += dist #move down the length of dist
player_path = "downChara.png" #change image to down
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_UP] and up == True: #if up
self.Y -= dist #move up the length of dist
player_path = "upChara.png" #change to up
self.image = pygame.image.load(player_path).convert_alpha()
if key[pygame.K_RIGHT] and right == True: #etc.
self.X += dist
player_path = "rightChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_LEFT] and left == True:
self.X -= dist
player_path = "leftChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
def outX(coord): #"coord" acts the same as "self"
return (coord.X)
def outY(coord):
return (coord.Y)
def draw(self, surface): #draw to the surface/screen
surface.blit(self.image, (self.X, self.Y))
#pygame.draw.rect(screen, (255, 255, 255), self.hitbox, 2)
return self.X, self.Y, self.width, self.height
class NPC(object): #NPC class
def __init__(self, path, x, y,text):
self.image = pygame.image.load(path).convert_alpha()
self.x = x
self.y = y
self.width = self.image.get_width()
self.height = self.image.get_height()
self.hitbox = (self.x, self.y, self.width, self.height)
self.text = text
def spawn(self, surface): #NPC spawn location
surface.blit(self.image, (self.x, self.y))
self.hitbox = (self.x, self.y, self.width, self.height)
#pygame.draw.rect(screen, (255, 255, 255), self.hitbox, 2)
def collide(self, x, y, width, height): #collision with player character
key = pygame.key.get_pressed()
othery = self.y + self.height
otherx = self.x + self.width
upperx = otherx - self.width/10
lowerx = self.x + self.width/10
#uppery = self.y + self.height/10
#lowery = othery - self.height/10
if self.y <= y <= othery and self.x <= x <= otherx or self.y <= y + height <= othery and self.x <= x <= otherx or self.y <= y <= othery and self.x <= x + width <= otherx or self.y <= y + height <= othery and self.x <= x + width <= otherx:
if key[pygame.K_RETURN]:
setup(self.text)
#older code:
#if phitbox == self.hitbox:
## if uppery <= y <= othery: #toggles if you're touching an entity or not
## self.touchEntityBelow = True
## print("up")
## else:
## self.touchEntityBelow = False
## if self.y <= y <= lowery: #etc
## self.touchEntityAbove = True
## print("down")
## else:
## self.touchEntityAbove = False
## if upperx <= x <= otherx:
## self.touchEntityRight = True
## print("right")
## else:
## self.touchEntityRight = False
## if self.x <= x <= lowerx:
## self.touchEntityLeft = True
## print("left")
## else:
## self.touchEntityLeft = False
## else:
## self.touchEntityRight = False
## self.touchEntityLeft = False
## self.touchEntityAbove = False
## self.touchEntityBelow = False
## def grabValues(self, x, y):
## return self.touchEntityBelow, self.touchEntityAbove, self.touchEntityLeft, self.touchEntityRight
def text_objects(text, font): #code for rendering the text in the colour and font
textSurface = font.render(text, True, (255, 255, 255))
return textSurface, textSurface.get_rect()
def interact(text): #code for blitting text to screen, specifies font and textbox.
textbox = pygame.transform.scale(pygame.image.load("bigbox.png"), (600, 111))
textSize = pygame.font.Font("cour.ttf",28) #specify text size
TextSurf, TextRect = text_objects(text, textSize) #allow text to be positioned
TextRect.topleft = (12, 297) #where text will be
screen.blit(textbox, (0, 289))
screen.blit(TextSurf, TextRect) #display text
pygame.display.update() #updates screen
time.sleep(2)
screen.blit(background, TextRect)
def playerMove(posX, posY, width, height):
if posX > width - 32: #this is because the sprite's "X" is determined in the top left corner, meaning we have to subtract the width from the measurement
moveRight = False #following selection loops determine if the player can move. may need to be moved into the collision code so it does not intervene with NPC
#collisions
else:
moveRight = True
if posX < 0:
moveLeft = False
else:
moveLeft = True
if posY > height - 32: #this is because the sprite's "Y" is determined in the top left corner, meaning we have to subtract the width from the measurement
moveDown = False
else:
moveDown = True
if posY < 0:
moveUp = False
else:
moveUp = True
return moveDown, moveUp, moveLeft, moveRight
##def checker(array):
## ## check first value of each part of array (all numbers)
## ## compare it to progress
## ## if equal to or less than, cycle through rest of that part of array.
## ## if greater than, then ignore.
## ## e.g: progress = 49, NPC1 will still be on text "0", NPC2 will now be on "33" and NPC3 will be on "0"
##
## placeholderList =
##
## for x in range(len(array)):
## if array[x][0] <= progress:
## del placeholderList[0:]
## placeholderList.append(array[x][1:])
## for x in range(len(placeholderList)):
## passMe = placeholderList[x]
## print (passMe)
## npc.interact(passMe)
player = Player() #instance of Player()
playerx, playery, playerwidth, playerheight= player.draw(screen) # gets the players coordinates and measurements
clock = pygame.time.Clock() #instance of the Clock() from the pygame module that specifies fps
personText = ("hello","hi","bye") #some placeholder text
lizardText = ("IM A LIZARD, IM A WIZARD,","IM THE LIZARD WIZARD!")
person1 = NPC("talkToThis.png",100, 200, personText) #instance of NPC(), an entity
lizard = NPC("lizardWizard.png", 300, 250, lizardText)
#npc = NPC("test.png",0, 0) #another NPC() entity
def setup(text): #if an NPC says more than one line of text, this makes sure that they can all be said in succession
for x in range(len(text)):
passtext = text[x]
interact(passtext)
boarderX = player.outX() #code for assigning window boarders
boarderY = player.outY()
##print (boarderX, boarderY) #making sure they both returned properly
pygame.display.flip() #paints screen
gameRun = True #allow game events to loop/be carried out more than once
while gameRun: #while game is running:
##person1text2 = [[0,"beginng","www","xxdqsd"],[1,"middle","aa"],[2,"end!"]]
personText = ("hello","hi","bye") #some placeholder text
playerx, playery, playerwidth, playerheight = player.draw(screen) #gets new player coords
#print(playerx, playery, playerwidth, playerheight) # prints them for testing reasons
person1.collide(playerx, playery, playerwidth, playerheight) # runs collision code to check for collision
lizard.collide(playerx, playery, playerwidth, playerheight)
#entityUp, entityDown, entityLeft, entityRight = person1.grabValues(playerx, playery)
event = pygame.event.poll() #assigns the pygame event code to the variable event
if event.type == pygame.QUIT: #if the "x" is pressed
pygame.quit() #quit game
gameRun = False #break the loop.
quit()
## if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
#### checker(person1text2)
## setup(personText)
screen.blit(background, (bgx, bgy)) #draw background colour
player.draw(screen) #draws player
person1.spawn(screen) #spawns an NPC entity
lizard.spawn(screen)
moveDown, moveUp, moveLeft, moveRight = playerMove(playerx, playery, width, height)
player.handle_keys(moveUp, moveDown, moveLeft, moveRight) #handle keys
pygame.display.update() #updates display
posX = player.outX() # gets player positions
posY = player.outY()
clock.tick(60) #clock cycle
the error is as follows:
Traceback (most recent call last):
File "C:UsersadamhDesktopCOMP3iteration1OVERWORLDalphaVER6.py", line 8, in <module>
background = pygame.image.load("spacebackground.png").convert()
pygame.error: No video mode has been set
so there is an error on the 8th line specifically.
python pygame
add a comment |
I've done research on this error and it seems all the solutions lie in needing to do something like "screen = pygame.display.set_mode(x, y)", which I've done, however the video mode error still occurs.
further research just shows that the solution lies in adding a line of code I've already implemented. I've tried moving the location to within the loop which plays when my game is being ran, however I cannot seem to get it working.
here is my full code. the "screen = set_mode" code is found on line 11 and screen.blit on line 222
import pygame
import time
progress = 0
pygame.init()
(width, height) = (600, 400) #specify window resolution
background = pygame.image.load("spacebackground.png").convert()
bgx = 0
bgy = 0
screen = pygame.display.set_mode((width, height)) #create window
pygame.display.set_caption('EduGame') #specify window name
player_path = "downChara.png" #specifies image path
class Player(object): #representitive of the player's overworld sprite
def __init__(self):
self.image = pygame.image.load(player_path).convert_alpha() #creates image, the player_path variable allowing it to be updated
self.X = (width/2) -16; # x co-ord of player
self.Y = (height/2)-16; # y co-ord of player
self.width = self.image.get_width()
self.height = self.image.get_height()
self.hitbox = (self.X, self.Y, self.width, self.height)
def handle_keys(self, up, down, left, right): #handling the keys/inputs
key = pygame.key.get_pressed()
dist = 3 #distance travelled in one frame of the program
if key[pygame.K_DOWN] and down == True: #if down
self.Y += dist #move down the length of dist
player_path = "downChara.png" #change image to down
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_UP] and up == True: #if up
self.Y -= dist #move up the length of dist
player_path = "upChara.png" #change to up
self.image = pygame.image.load(player_path).convert_alpha()
if key[pygame.K_RIGHT] and right == True: #etc.
self.X += dist
player_path = "rightChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_LEFT] and left == True:
self.X -= dist
player_path = "leftChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
def outX(coord): #"coord" acts the same as "self"
return (coord.X)
def outY(coord):
return (coord.Y)
def draw(self, surface): #draw to the surface/screen
surface.blit(self.image, (self.X, self.Y))
#pygame.draw.rect(screen, (255, 255, 255), self.hitbox, 2)
return self.X, self.Y, self.width, self.height
class NPC(object): #NPC class
def __init__(self, path, x, y,text):
self.image = pygame.image.load(path).convert_alpha()
self.x = x
self.y = y
self.width = self.image.get_width()
self.height = self.image.get_height()
self.hitbox = (self.x, self.y, self.width, self.height)
self.text = text
def spawn(self, surface): #NPC spawn location
surface.blit(self.image, (self.x, self.y))
self.hitbox = (self.x, self.y, self.width, self.height)
#pygame.draw.rect(screen, (255, 255, 255), self.hitbox, 2)
def collide(self, x, y, width, height): #collision with player character
key = pygame.key.get_pressed()
othery = self.y + self.height
otherx = self.x + self.width
upperx = otherx - self.width/10
lowerx = self.x + self.width/10
#uppery = self.y + self.height/10
#lowery = othery - self.height/10
if self.y <= y <= othery and self.x <= x <= otherx or self.y <= y + height <= othery and self.x <= x <= otherx or self.y <= y <= othery and self.x <= x + width <= otherx or self.y <= y + height <= othery and self.x <= x + width <= otherx:
if key[pygame.K_RETURN]:
setup(self.text)
#older code:
#if phitbox == self.hitbox:
## if uppery <= y <= othery: #toggles if you're touching an entity or not
## self.touchEntityBelow = True
## print("up")
## else:
## self.touchEntityBelow = False
## if self.y <= y <= lowery: #etc
## self.touchEntityAbove = True
## print("down")
## else:
## self.touchEntityAbove = False
## if upperx <= x <= otherx:
## self.touchEntityRight = True
## print("right")
## else:
## self.touchEntityRight = False
## if self.x <= x <= lowerx:
## self.touchEntityLeft = True
## print("left")
## else:
## self.touchEntityLeft = False
## else:
## self.touchEntityRight = False
## self.touchEntityLeft = False
## self.touchEntityAbove = False
## self.touchEntityBelow = False
## def grabValues(self, x, y):
## return self.touchEntityBelow, self.touchEntityAbove, self.touchEntityLeft, self.touchEntityRight
def text_objects(text, font): #code for rendering the text in the colour and font
textSurface = font.render(text, True, (255, 255, 255))
return textSurface, textSurface.get_rect()
def interact(text): #code for blitting text to screen, specifies font and textbox.
textbox = pygame.transform.scale(pygame.image.load("bigbox.png"), (600, 111))
textSize = pygame.font.Font("cour.ttf",28) #specify text size
TextSurf, TextRect = text_objects(text, textSize) #allow text to be positioned
TextRect.topleft = (12, 297) #where text will be
screen.blit(textbox, (0, 289))
screen.blit(TextSurf, TextRect) #display text
pygame.display.update() #updates screen
time.sleep(2)
screen.blit(background, TextRect)
def playerMove(posX, posY, width, height):
if posX > width - 32: #this is because the sprite's "X" is determined in the top left corner, meaning we have to subtract the width from the measurement
moveRight = False #following selection loops determine if the player can move. may need to be moved into the collision code so it does not intervene with NPC
#collisions
else:
moveRight = True
if posX < 0:
moveLeft = False
else:
moveLeft = True
if posY > height - 32: #this is because the sprite's "Y" is determined in the top left corner, meaning we have to subtract the width from the measurement
moveDown = False
else:
moveDown = True
if posY < 0:
moveUp = False
else:
moveUp = True
return moveDown, moveUp, moveLeft, moveRight
##def checker(array):
## ## check first value of each part of array (all numbers)
## ## compare it to progress
## ## if equal to or less than, cycle through rest of that part of array.
## ## if greater than, then ignore.
## ## e.g: progress = 49, NPC1 will still be on text "0", NPC2 will now be on "33" and NPC3 will be on "0"
##
## placeholderList =
##
## for x in range(len(array)):
## if array[x][0] <= progress:
## del placeholderList[0:]
## placeholderList.append(array[x][1:])
## for x in range(len(placeholderList)):
## passMe = placeholderList[x]
## print (passMe)
## npc.interact(passMe)
player = Player() #instance of Player()
playerx, playery, playerwidth, playerheight= player.draw(screen) # gets the players coordinates and measurements
clock = pygame.time.Clock() #instance of the Clock() from the pygame module that specifies fps
personText = ("hello","hi","bye") #some placeholder text
lizardText = ("IM A LIZARD, IM A WIZARD,","IM THE LIZARD WIZARD!")
person1 = NPC("talkToThis.png",100, 200, personText) #instance of NPC(), an entity
lizard = NPC("lizardWizard.png", 300, 250, lizardText)
#npc = NPC("test.png",0, 0) #another NPC() entity
def setup(text): #if an NPC says more than one line of text, this makes sure that they can all be said in succession
for x in range(len(text)):
passtext = text[x]
interact(passtext)
boarderX = player.outX() #code for assigning window boarders
boarderY = player.outY()
##print (boarderX, boarderY) #making sure they both returned properly
pygame.display.flip() #paints screen
gameRun = True #allow game events to loop/be carried out more than once
while gameRun: #while game is running:
##person1text2 = [[0,"beginng","www","xxdqsd"],[1,"middle","aa"],[2,"end!"]]
personText = ("hello","hi","bye") #some placeholder text
playerx, playery, playerwidth, playerheight = player.draw(screen) #gets new player coords
#print(playerx, playery, playerwidth, playerheight) # prints them for testing reasons
person1.collide(playerx, playery, playerwidth, playerheight) # runs collision code to check for collision
lizard.collide(playerx, playery, playerwidth, playerheight)
#entityUp, entityDown, entityLeft, entityRight = person1.grabValues(playerx, playery)
event = pygame.event.poll() #assigns the pygame event code to the variable event
if event.type == pygame.QUIT: #if the "x" is pressed
pygame.quit() #quit game
gameRun = False #break the loop.
quit()
## if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
#### checker(person1text2)
## setup(personText)
screen.blit(background, (bgx, bgy)) #draw background colour
player.draw(screen) #draws player
person1.spawn(screen) #spawns an NPC entity
lizard.spawn(screen)
moveDown, moveUp, moveLeft, moveRight = playerMove(playerx, playery, width, height)
player.handle_keys(moveUp, moveDown, moveLeft, moveRight) #handle keys
pygame.display.update() #updates display
posX = player.outX() # gets player positions
posY = player.outY()
clock.tick(60) #clock cycle
the error is as follows:
Traceback (most recent call last):
File "C:UsersadamhDesktopCOMP3iteration1OVERWORLDalphaVER6.py", line 8, in <module>
background = pygame.image.load("spacebackground.png").convert()
pygame.error: No video mode has been set
so there is an error on the 8th line specifically.
python pygame
I've done research on this error and it seems all the solutions lie in needing to do something like "screen = pygame.display.set_mode(x, y)", which I've done, however the video mode error still occurs.
further research just shows that the solution lies in adding a line of code I've already implemented. I've tried moving the location to within the loop which plays when my game is being ran, however I cannot seem to get it working.
here is my full code. the "screen = set_mode" code is found on line 11 and screen.blit on line 222
import pygame
import time
progress = 0
pygame.init()
(width, height) = (600, 400) #specify window resolution
background = pygame.image.load("spacebackground.png").convert()
bgx = 0
bgy = 0
screen = pygame.display.set_mode((width, height)) #create window
pygame.display.set_caption('EduGame') #specify window name
player_path = "downChara.png" #specifies image path
class Player(object): #representitive of the player's overworld sprite
def __init__(self):
self.image = pygame.image.load(player_path).convert_alpha() #creates image, the player_path variable allowing it to be updated
self.X = (width/2) -16; # x co-ord of player
self.Y = (height/2)-16; # y co-ord of player
self.width = self.image.get_width()
self.height = self.image.get_height()
self.hitbox = (self.X, self.Y, self.width, self.height)
def handle_keys(self, up, down, left, right): #handling the keys/inputs
key = pygame.key.get_pressed()
dist = 3 #distance travelled in one frame of the program
if key[pygame.K_DOWN] and down == True: #if down
self.Y += dist #move down the length of dist
player_path = "downChara.png" #change image to down
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_UP] and up == True: #if up
self.Y -= dist #move up the length of dist
player_path = "upChara.png" #change to up
self.image = pygame.image.load(player_path).convert_alpha()
if key[pygame.K_RIGHT] and right == True: #etc.
self.X += dist
player_path = "rightChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_LEFT] and left == True:
self.X -= dist
player_path = "leftChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
def outX(coord): #"coord" acts the same as "self"
return (coord.X)
def outY(coord):
return (coord.Y)
def draw(self, surface): #draw to the surface/screen
surface.blit(self.image, (self.X, self.Y))
#pygame.draw.rect(screen, (255, 255, 255), self.hitbox, 2)
return self.X, self.Y, self.width, self.height
class NPC(object): #NPC class
def __init__(self, path, x, y,text):
self.image = pygame.image.load(path).convert_alpha()
self.x = x
self.y = y
self.width = self.image.get_width()
self.height = self.image.get_height()
self.hitbox = (self.x, self.y, self.width, self.height)
self.text = text
def spawn(self, surface): #NPC spawn location
surface.blit(self.image, (self.x, self.y))
self.hitbox = (self.x, self.y, self.width, self.height)
#pygame.draw.rect(screen, (255, 255, 255), self.hitbox, 2)
def collide(self, x, y, width, height): #collision with player character
key = pygame.key.get_pressed()
othery = self.y + self.height
otherx = self.x + self.width
upperx = otherx - self.width/10
lowerx = self.x + self.width/10
#uppery = self.y + self.height/10
#lowery = othery - self.height/10
if self.y <= y <= othery and self.x <= x <= otherx or self.y <= y + height <= othery and self.x <= x <= otherx or self.y <= y <= othery and self.x <= x + width <= otherx or self.y <= y + height <= othery and self.x <= x + width <= otherx:
if key[pygame.K_RETURN]:
setup(self.text)
#older code:
#if phitbox == self.hitbox:
## if uppery <= y <= othery: #toggles if you're touching an entity or not
## self.touchEntityBelow = True
## print("up")
## else:
## self.touchEntityBelow = False
## if self.y <= y <= lowery: #etc
## self.touchEntityAbove = True
## print("down")
## else:
## self.touchEntityAbove = False
## if upperx <= x <= otherx:
## self.touchEntityRight = True
## print("right")
## else:
## self.touchEntityRight = False
## if self.x <= x <= lowerx:
## self.touchEntityLeft = True
## print("left")
## else:
## self.touchEntityLeft = False
## else:
## self.touchEntityRight = False
## self.touchEntityLeft = False
## self.touchEntityAbove = False
## self.touchEntityBelow = False
## def grabValues(self, x, y):
## return self.touchEntityBelow, self.touchEntityAbove, self.touchEntityLeft, self.touchEntityRight
def text_objects(text, font): #code for rendering the text in the colour and font
textSurface = font.render(text, True, (255, 255, 255))
return textSurface, textSurface.get_rect()
def interact(text): #code for blitting text to screen, specifies font and textbox.
textbox = pygame.transform.scale(pygame.image.load("bigbox.png"), (600, 111))
textSize = pygame.font.Font("cour.ttf",28) #specify text size
TextSurf, TextRect = text_objects(text, textSize) #allow text to be positioned
TextRect.topleft = (12, 297) #where text will be
screen.blit(textbox, (0, 289))
screen.blit(TextSurf, TextRect) #display text
pygame.display.update() #updates screen
time.sleep(2)
screen.blit(background, TextRect)
def playerMove(posX, posY, width, height):
if posX > width - 32: #this is because the sprite's "X" is determined in the top left corner, meaning we have to subtract the width from the measurement
moveRight = False #following selection loops determine if the player can move. may need to be moved into the collision code so it does not intervene with NPC
#collisions
else:
moveRight = True
if posX < 0:
moveLeft = False
else:
moveLeft = True
if posY > height - 32: #this is because the sprite's "Y" is determined in the top left corner, meaning we have to subtract the width from the measurement
moveDown = False
else:
moveDown = True
if posY < 0:
moveUp = False
else:
moveUp = True
return moveDown, moveUp, moveLeft, moveRight
##def checker(array):
## ## check first value of each part of array (all numbers)
## ## compare it to progress
## ## if equal to or less than, cycle through rest of that part of array.
## ## if greater than, then ignore.
## ## e.g: progress = 49, NPC1 will still be on text "0", NPC2 will now be on "33" and NPC3 will be on "0"
##
## placeholderList =
##
## for x in range(len(array)):
## if array[x][0] <= progress:
## del placeholderList[0:]
## placeholderList.append(array[x][1:])
## for x in range(len(placeholderList)):
## passMe = placeholderList[x]
## print (passMe)
## npc.interact(passMe)
player = Player() #instance of Player()
playerx, playery, playerwidth, playerheight= player.draw(screen) # gets the players coordinates and measurements
clock = pygame.time.Clock() #instance of the Clock() from the pygame module that specifies fps
personText = ("hello","hi","bye") #some placeholder text
lizardText = ("IM A LIZARD, IM A WIZARD,","IM THE LIZARD WIZARD!")
person1 = NPC("talkToThis.png",100, 200, personText) #instance of NPC(), an entity
lizard = NPC("lizardWizard.png", 300, 250, lizardText)
#npc = NPC("test.png",0, 0) #another NPC() entity
def setup(text): #if an NPC says more than one line of text, this makes sure that they can all be said in succession
for x in range(len(text)):
passtext = text[x]
interact(passtext)
boarderX = player.outX() #code for assigning window boarders
boarderY = player.outY()
##print (boarderX, boarderY) #making sure they both returned properly
pygame.display.flip() #paints screen
gameRun = True #allow game events to loop/be carried out more than once
while gameRun: #while game is running:
##person1text2 = [[0,"beginng","www","xxdqsd"],[1,"middle","aa"],[2,"end!"]]
personText = ("hello","hi","bye") #some placeholder text
playerx, playery, playerwidth, playerheight = player.draw(screen) #gets new player coords
#print(playerx, playery, playerwidth, playerheight) # prints them for testing reasons
person1.collide(playerx, playery, playerwidth, playerheight) # runs collision code to check for collision
lizard.collide(playerx, playery, playerwidth, playerheight)
#entityUp, entityDown, entityLeft, entityRight = person1.grabValues(playerx, playery)
event = pygame.event.poll() #assigns the pygame event code to the variable event
if event.type == pygame.QUIT: #if the "x" is pressed
pygame.quit() #quit game
gameRun = False #break the loop.
quit()
## if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
#### checker(person1text2)
## setup(personText)
screen.blit(background, (bgx, bgy)) #draw background colour
player.draw(screen) #draws player
person1.spawn(screen) #spawns an NPC entity
lizard.spawn(screen)
moveDown, moveUp, moveLeft, moveRight = playerMove(playerx, playery, width, height)
player.handle_keys(moveUp, moveDown, moveLeft, moveRight) #handle keys
pygame.display.update() #updates display
posX = player.outX() # gets player positions
posY = player.outY()
clock.tick(60) #clock cycle
the error is as follows:
Traceback (most recent call last):
File "C:UsersadamhDesktopCOMP3iteration1OVERWORLDalphaVER6.py", line 8, in <module>
background = pygame.image.load("spacebackground.png").convert()
pygame.error: No video mode has been set
so there is an error on the 8th line specifically.
python pygame
python pygame
edited Nov 26 '18 at 14:44
Polybrow
asked Nov 25 '18 at 15:23
PolybrowPolybrow
305
305
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
TURNS OUT I JUST HAD TO PUT THE CODE DEFINING THE SCREEN BEFORE THE CODE LOADING THE BACKGROUND
whoopsies
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468961%2fpygame-error-no-video-mode-has-been-set-even-after-pygame-display-set-mode-ha%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
TURNS OUT I JUST HAD TO PUT THE CODE DEFINING THE SCREEN BEFORE THE CODE LOADING THE BACKGROUND
whoopsies
add a comment |
TURNS OUT I JUST HAD TO PUT THE CODE DEFINING THE SCREEN BEFORE THE CODE LOADING THE BACKGROUND
whoopsies
add a comment |
TURNS OUT I JUST HAD TO PUT THE CODE DEFINING THE SCREEN BEFORE THE CODE LOADING THE BACKGROUND
whoopsies
TURNS OUT I JUST HAD TO PUT THE CODE DEFINING THE SCREEN BEFORE THE CODE LOADING THE BACKGROUND
whoopsies
answered Nov 26 '18 at 14:50
PolybrowPolybrow
305
305
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468961%2fpygame-error-no-video-mode-has-been-set-even-after-pygame-display-set-mode-ha%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown