我正在制作一款平台游戏,但我遇到了两个问题。
我的精灵被限制在窗口中心的一个小盒子里。我怎样做才能覆盖整个窗口?
当我移动旧的精灵时,不会删除。这使得精灵看起来有一条尾巴。
任何一方的帮助都将不胜感激!
import random, sys, copy, os, pygame, time, math
from pygame.locals import *
TILESIZE = 20
WINDOWWIDTH = 1280
WINDOWHEIGHT = 720
FPS = 30
floorx = (WINDOWHEIGHT - (TILESIZE))
floory = (WINDOWWIDTH / TILESIZE)
TileOffset = 20
tilesNeeded = (WINDOWWIDTH / TILESIZE)
floorSize = TILESIZE * 2
OUTSIDE_DECORATION_PCT = 20
HALF_WINDOWHEIGHT = (WINDOWHEIGHT / 2)
HALF_WINDOWWIDTH = (WINDOWWIDTH / 2)
CAMERASLACK = 25
MOVERATE = 9
BOUNCERATE = 6
BOUNCEHEIGHT = 30
INVULTIME = 2
GAMEOVERTIME = 4
MAXHEALTH = 3
STARTSIZE = 30
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
LIGHTGRAY = (174, 174, 174)
DARKGRAY = ( 41, 41, 41)
MEDGRAY = (101, 101, 101)
SKYBLUE = (200, 210, 255)
DARKTURQUOISE = ( 3, 54, 73)
GREEN = ( 0, 92, 7)
LIGHTGREEN = ( 0, 135, 15)
BGCOLOR = LIGHTGRAY
TEXTCOLOR = BLACK
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
def main():
global FPSCLOCK, DISPLAYSURF, BASICFONT, TILESIZE, floorx, floory, floorCovered, tilesNeeded, OUTSIDEDECOMAPPING, L_Monster, R_Monster, BGIMAGE
pygame.init()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
FPSCLOCK = pygame.time.Clock()
pygame.display.set_caption('Alpha One')
# Set up the background image.
boardImage = pygame.image.load('bg.png')
# Use smoothscale() to stretch the board image to fit the entire board:
boardImageRect = boardImage.get_rect()
boardImageRect.topleft = (0, 0)
BGIMAGE = pygame.image.load('bg.png')
# Use smoothscale() to stretch the background image to fit the entire window:
BGIMAGE = pygame.transform.smoothscale(BGIMAGE, (WINDOWWIDTH, WINDOWHEIGHT))
BGIMAGE.blit(boardImage, boardImageRect)
#Draw the background
DISPLAYSURF.blit(BGIMAGE, BGIMAGE.get_rect())
#Draw the Floor
drawFloor()
L_Monster = pygame.image.load('monster.png')
L_Monster = pygame.transform.scale(L_Monster, (1000, 600))
R_Monster = pygame.transform.flip(L_Monster, True, False)
pygame.display.flip()
#Main Game Loop
while True:
runGame()
#pygame.display.update()
def runGame():
invulnerableMode = False
invulnerableStartTime = 0
gameOverMode = False
gameOverStartTime = 0
winMode = False
camerax = 0
cameray = 0
playerObj = {'surface': pygame.transform.scale(L_Monster,(STARTSIZE, STARTSIZE)),
'facing': LEFT,
'size': STARTSIZE,
'x': HALF_WINDOWWIDTH,
'y': HALF_WINDOWHEIGHT,
'bounce':0,
'health': MAXHEALTH}
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
while True:
DISPLAYSURF.blit(BGIMAGE, BGIMAGE.get_rect())
drawFloor()
#DISPLAYSURF.fill(WHITE)
if invulnerableMode and time.time() - invulnerableStartTime > INVULNTIME:
invulnerableMode = False
playerCenterx = playerObj['x'] + int(playerObj['size'] / 2)
playerCentery = playerObj['y'] + int(playerObj['size'] / 2)
if (camerax + HALF_WINDOWWIDTH) - playerCenterx > CAMERASLACK:
camerax = playerCenterx + CAMERASLACK - HALF_WINDOWWIDTH
elif playerCenterx - (camerax +HALF_WINDOWWIDTH) > CAMERASLACK:
camerax = playerCenterx - CAMERASLACK - HALF_WINDOWWIDTH
if (cameray + HALF_WINDOWHEIGHT) - playerCentery > CAMERASLACK:
cameray = playerCentery + CAMERASLACK - HALF_WINDOWHEIGHT
elif playerCentery - (cameray +HALF_WINDOWHEIGHT) > CAMERASLACK:
cameray = playerCentery - CAMERASLACK - HALF_WINDOWHEIGHT
flashIsOn = round(time.time(), 1) * 10 % 2 == 1
if not gameOverMode and not (invulnerableMode and flashIsOn):
playerObj['rect'] = pygame.Rect((playerObj['x'] - camerax,
playerObj['y'] - cameray - getBounceAmount(playerObj['bounce'], BOUNCERATE, BOUNCEHEIGHT),
playerObj['size'],
playerObj['size']))
DISPLAYSURF.blit(playerObj['surface'], playerObj['rect'])
for event in pygame.event.get():
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN:
if event.key in (K_UP, K_w):
moveDown = False
moveUp = True
elif event.key in (K_DOWN, K_s):
moveUp = False
moveDown = True
elif event.key in (K_LEFT, K_a):
moveRight = False
moveLeft = True
if playerObj['facing'] == RIGHT:
playerObj['surface'] = pygame.transform.scale(L_Monster, (playerObj['size'], playerObj['size']))
playerObj['facing'] == LEFT
elif event.key in (K_RIGHT, K_d):
moveLeft = False
moveRight = True
if playerObj['facing'] == LEFT:
playerObj['surface'] = pygame.transform.scale(R_Monster, (playerObj['size'], playerObj['size']))
playerObj['facing'] = RIGHT
elif winMode and event.key == K_r:
return
elif event.type == KEYUP:
if event.key in (K_LEFT, K_a):
moveLeft = False
elif event.key in (K_RIGHT, K_d):
moveRight = False
elif event.key in (K_UP, K_w):
moveUp = False
elif event.key in (K_DOWN, K_s):
moveDown = False
elif event.key == K_ESCAPE:
terminate()
if not gameOverMode:
if moveLeft:
playerObj['x'] -= MOVERATE
if moveRight:
playerObj['x'] += MOVERATE
if moveUp:
playerObj['y'] -= MOVERATE
if moveDown:
playerObj['y'] += MOVERATE
if (moveLeft or moveRight or moveUp or moveDown) or playerObj['bounce'] != 0:
playerObj['bounce'] += 1
if playerObj['bounce'] > BOUNCERATE:
playerObj['bounce'] = 0
else:
# game is over, show "game over" text
DISPLAYSURF.blit(gameOverSurf, gameOverRect)
if time.time() - gameOverStartTime > GAMEOVERTIME:
return
if winMode:
DISPLAYSURF.blit(winSurf, winRect)
DISPLAYSURF.blit(winSurf2, winRect2)
pygame.display.update()
FPSCLOCK.tick(FPS)
def getBounceAmount(currentBounce, bounceRate, bounceHeight):
return int(math.sin( (math.pi / float(bounceRate)) * currentBounce ) * bounceHeight)
def getRandomOffCameraPos(camerax, cameray, objWidth, objHeight):
# create a Rect of the camera view
cameraRect = pygame.Rect(camerax, cameray, WINDOWWIDTH, WINDOWHEIGHT)
while True:
x = random.randint(camerax - WINDOWWIDTH, camerax + (2 * WINDOWWIDTH))
y = random.randint(cameray - WINDOWHEIGHT, cameray + (2 * WINDOWHEIGHT))
# create a Rect object with the random coordinates and use colliderect()
# to make sure the right edge isn't in the camera view.
objRect = pygame.Rect(x, y, objWidth, objHeight)
if not objRect.colliderect(cameraRect):
return x, y
def isOutsideActiveArea(camerax, cameray, obj):
boundsLeftEdge = camerax - WINDOWWIDTH
boundsTopEdge = cameray - WINDOWHEIGHT
boundsRect = pygame.Rect(boundsLeftEdge, boundsTopEdge, WINDOWWIDTH * 3, WINDOWHEIGHT * 3)
objRect = pygame.Rect(obj['x'], obj['y'], obj['width'], obj['height'])
return not boundsRect.colliderect(objRect)
def checkForQuit():
for event in pygame.event.get(QUIT): # get all the QUIT events
terminate() # terminate if any QUIT events are present
for event in pygame.event.get(KEYUP): # get all the KEYUP events
if event.key == K_ESCAPE:
terminate() # terminate if the KEYUP event was for the Esc key
pygame.event.post(event)
def drawFloor():
#Open the image used for tiles and initialize N
floorTile = pygame.image.load('tile.png')
N = 0
while (N < tilesNeeded):
DISPLAYSURF.blit(floorTile,((20 * N, (floorx + (TILESIZE/4)) - TILESIZE)), )
DISPLAYSURF.blit(floorTile,(20 * N, (floorx + (TILESIZE/4))))
N = N + 1
#Updates the display
pygame.display.flip()
def checkCollide():
FLOOR_SURF = pygame.Rect( 0, (WINDOWHEIGHT - (TILESIZE * 2)), WINDOWWIDTH, WINDOWHEIGHT)
def terminate():
pygame.quit()
sys.exit()
if __name__ == '__main__':
main()
答案 0 :(得分:3)
你获得精灵“尾巴”的原因是你在开始绘制新画面之前没有清除画面。处理此问题的一种方法是在runGame()的开头输入类似下面的内容:
DISPLAYSURF.fill(white)
这将通过覆盖白色的一切来“清除”表面。当您开始为该帧绘制图像时,它们将被绘制在空白的白色表面上。需要注意的是,您需要重新绘制屏幕上的每个精灵,而不仅仅是在最后一帧中移动的精灵。
对于第一个问题,如果你问如何缩放图像的大小,你可以使用pygame.transform中的缩放函数
L_monster = pygame.transform.scale(L_monster, (500, 500))
这通过将给定曲面转换为元组给出的新大小来创建新曲面。
http://www.pygame.org/docs/ref/transform.html#pygame.transform.scale