运行黑屏
import sys,time,random,math,pygame
from pygame.locals import *
def print_text(font,x,y,text,color=(255,255,255)):
imgText = font,render(text,True,color)
screen.blit(imgText,(x,y))
def reset_arrow():
y = randint(250,350)
arrow.position = 800,y
#main program begins
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Escape The Dragon Game")
font = pygame.font.Font(None,18)
framerate = pygame.time.Clock()
#load bitmaps
bo = pygame.image.load("background.png").convert_alpha()
#create a spritegroup
group = pygame.sprite.Group()
#create the dragon sprite
dragon = MySprite(screen)
dragon.load("dragon.png",260,150,3)
dragon.position = 100,230
group.add(dragon)
#create the player sprite
player = MySprite(screen)
player.load("caveman.png",50,64,8)
player.first_frame = 1
player.last_frame = 7
player.position = 400, 303
group.add(arrow)
arrow_vel = 8.0
game_ovor = False
you_win = False
player_jumping = False
jump_vel = 0.0
player_start_Y = player.Y
#repeating loop
while Ture:
framerate.tick(30)
ticks = pygame.time.get_ticks()
for event in pygame.event_get():
if event.type == oygame.OUIT:sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:SYS.EXIT()
elif keys[pygame.K_SPACE]:
if not player_jumping:
player_kumping = True
jump_vel = -8.0
#update the arrow
if not game_ovor:
arrow.X -= arrow_vel
if arrow.X < -40: reset_arrow()
#did arrow hit player?
if pygame.sprite.colllide_rect(arrow, player):
reset_arrow()
player.X -= 10
#did arrow hit dragon?
if pygame.sprite.collide_rect(arrow,dragon):
reset_arrow()
dragon.X -= 10
#did dragon eat the player?
if pygame.sprite.collide_rect(player,dragon):
game_ovor = Ture
#did the dragon get defeated?
if dragon.X < -100:
you_win = Ture
game_ovor = Ture
#is the player jumping?
if player_jumping:
player.Y += jumping_vel
jump_vel += 0.5
if player.Y > player_start_y:
player_jumping = False
player.Y = player_start_y
jumg_vel = 0.0
#draw the background
screen.blit(bg,(0.0))
#draw sprites
if not game_ovor:
group.update(ticks, 50)
#draw sprites
group.draw(screen)
print_text(font,350,560,"Press SPACE to jump!")
if game_ovor:
print_text(font, 360, 100, "G A M E O V E R")
if you_win:
print_text(font, 330, 130, "YOU BEAT THE DRAGON!")
else:
print_text(font, 330, 130, "THE DRAGON GOT YOU!")
pygame.display.update()
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\1.py", line 22, in
bo = pygame.image.load("background.png").convert_alpha()
FileNotFoundError: No file 'background.png' found in working directory 'C:\Users\Administrator\Desktop'.
这个问题是没有'background.png' 这张图片,你看看是不是在同一个文件夹里