我已经对此错误进行了研究,似乎所有解决方案都在于需要执行“ screen = pygame.display.set_mode(x,y)”之类的操作,但是视频模式错误仍然发生。
进一步的研究仅表明,解决方案在于添加我已经实现的代码行。我曾尝试将位置移动到运行游戏时会播放的循环中,但是似乎无法正常工作。
这是我的完整代码。在第11行找到了“ screen = set_mode”代码,在第222行找到了screen.blit
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
错误如下:
Traceback (most recent call last):
File "C:\Users\adamh\Desktop\COMP3\iteration1\OVERWORLDalphaVER6.py", line 8, in <module>
background = pygame.image.load("spacebackground.png").convert()
pygame.error: No video mode has been set
所以在第8行上有一个错误。
答案 0 :(得分:0)
结果是我不得不在代码加载到后台之前输入定义屏幕的代码
whoopsies