我正在尝试下载pygame中名为“ images”的子目录中存储的图像,但是我得到的文件不是Windows BMP文件。我在Mac OS的Spyder中运行python 2.7。我有pygame.image.get_extended() = 1
,所以不确定是什么问题。我的pygame似乎没有损坏,因为没有png可以正常运行(尝试其他游戏)。一直在努力寻找解决方案。我已经尝试过对图像使用.png和.bmp扩展名。谢谢
# From Al Sweigart's book
import pygame, sys, time, random, os
from pygame.locals import *
print os.getcwd()
# set up pygame
pygame.init()
mainClock = pygame.time.Clock()
# Set up window
windowWidth = 400
windowHeight = 400
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
pygame.display.set_caption("Sprites and Sounds")
# Set up colors
white = (255,255,255)
# Set up block data structure
player = pygame.Rect(300, 100, 40, 40)
playerImage = pygame.image.load("images/ninja.bmp")
playerStretchedImage = pygame.transform.scale(playerImage, (40,40))
foodImage = pygame.image.load("images/apple.bmp")
foodStretchedImage = pygame.transform.scale(foodImage, (15,15))
foods = []
for i in range(20):
foods.append(pygame.Rect(random.randint(0, windowWidth- 20),
random.randint(0, windowHeight-20), 20, 20))
foodCounter = 0
newfood = 40
# set up keyboard vars
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
movespeed = 6
## Add sound here
# Run game loop
while True:
# check for quit event
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
# change keyboard variables
if event.key == K_LEFT or event.key == K_a:
moveRight = False
moveLeft = True
if event.key == K_RIGHT or event.key == K_d:
moveLeft = False
moveRight = True
if event.key == K_UP or event.key == K_w:
moveDown = False
moveUp = True
if event.key == K_DOWN or event.key == K_s:
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT or event.key == K_a:
moveLeft = False
if event.key == K_RIGHT or event.key == K_d:
moveRight = False
if event.key == K_UP or event.key == K_w:
moveUp = False
if event.key == K_DOWN or event.key == K_s:
moveDown = False
if event.key == K_x:
player.top = random.randint(0, windowHeight-player.height)
player.left = random.randint(0, windowWidth-player.width)
if event.type == MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0], event.pos[1], 20, 20))
## Draw white background onto surface
windowSurface.fill(White)
## move player
if moveDown and player.bottom < windowHeight:
player.top += movespeed
if moveUp and player.top > 0:
player.top -= movespeed
if moveLeft and player.left >0:
player.left -= movespeed
if moveRight and player.right < windowWidth:
player.left += movespeed
# draw block image onto surface player object
windowSurface.blit(playerStretchedImage, player)
# check whether player has intersected with food squares
for food in foods[:]:
if player.colliderect(food):
foods.remove(food)
player = pygame.Rect(player.left, player.top,
player.width+2, player.height +2)
playerStretchedImage = pygame.transform.scale(playerImage,
(player.width, player.height))
# draw food
for food in foods:
windowSurface.blit(foodImage,food)
# draw window
pygame.display.update()
mainClock.tick(40)