我开始在pygame中创建一个新游戏。首先,我制作了游戏的窗口和背景。我制作了背景(bg和fg)。当我的角色精灵移动时,我将保留fg并保留第二个背景bg作为滚动背景。之后,我粘贴了之前的pygame项目中的代码,该项目只是一个简单的sprite角色,可以在背景前行走和跳跃。当我粘贴它时,窗口出现,仅显示我的背景,根本不显示我的精灵角色。它也没有引发任何错误。发生了什么?我的精灵只是隐藏吗?
import pygame
from random import randint, choice
from pygame.locals import *
pygame.init()
#(pasted code start)-----------------------------------------------------------------------------
walkRight = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/Examples/Ben-Game/ben characters main/R %s.png' % frame) for frame in range(1, 9)]
walkLeft = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/Examples/Ben-Game/ben characters main/L %s.png' % frame) for frame in range(14, 21)]
#(pasted code end)-----------------------------------------------------------------------------
bg = pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/Llama game/Llama imaging/backgrounds concepts/Mountains/PPP_BG1.png')
fg = pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/Llama game/Llama imaging/backgrounds concepts/Mountains/PPP_fg1.png')
screen = pygame.display.set_mode((276, 216), HWSURFACE|DOUBLEBUF|RESIZABLE)
#(pasted code start)---------------------------------------------------------------------------
class player(object):
def __init__(self,x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.velocity = 15
self.isJump = False
self.jumpCount = 7
self.right = False
self.left = False
self.walkCount = 0
self.screen = screen
def draw(self, screen):
if self.walkCount + 1 >= 8:
self.walkCount = 0
if self.left:
self.screen.blit(walkLeft[self.walkCount//1], (self.x, self.y))
self.walkCount += 1
elif self.right:
self.screen.blit(walkRight[self.walkCount//1], (self.x, self.y))
self.walkCount += 1
else:
self.screen.blit(char, (self.x, self.y))
#(pasted code end)-----------------------------------------------------------------------------
pygame.display.set_caption("Game")
screen.blit(pygame.transform.scale(bg,(276, 216)),(0,0))
screen.blit(pygame.transform.scale(fg,(276, 216)),(0,0))
pygame.display.flip()
#while loop for screen resize, fulscreen:
while True:
pygame.event.pump()
event=pygame.event.wait()
if event.type==QUIT:
pygame.quit()
elif event.type==VIDEORESIZE:
screen=pygame.display.set_mode(event.dict['size'],HWSURFACE|DOUBLEBUF|RESIZABLE)
screen.blit(pygame.transform.scale(bg,event.dict['size']),(0,0))
screen.blit(pygame.transform.scale(fg,event.dict['size']),(0,0))
pygame.display.flip()
#(pasted code start)---------------------------------------------------------------------------
#drawing in sprite character
def redrawGamescreen():
ben.screen.blit(bg,(0,0))
ben.screen.blit(fg,(0,0))
ben.draw(ben.screen)
pygame.display.update()
ben = player(50, 279, 64, 64)
run = True
#main loop and character control
while run:
clock.tick(8)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and ben.x > ben.velocity:
ben.x -= ben.velocity
ben.left = True
ben.right = False
elif keys[pygame.K_RIGHT] and ben.x < 735 - ben.width - ben.velocity:
ben.x += ben.velocity
ben.right = True
ben.left = False
else:
ben.right = False
ben.left = False
ben.walkCount = 0
if not(ben.isJump):
if keys[pygame.K_SPACE]:
ben.isJump = True
ben.right = False
ben.left = False
else:
if ben.jumpCount >= -7:
neg = 1
if ben.jumpCount < 0:
neg = -1
ben.y -= (ben.jumpCount ** 2) * 0.5 * neg
ben.jumpCount -= 1
else:
ben.isJump = False
ben.jumpCount = 7
ben.velocity = 15
redrawGamescreen()
#(pasted code end)-----------------------------------------------------------------------------
pygame.quit()
答案 0 :(得分:0)
我无法对其进行测试,但是如果您更好地组织代码,则应该看到运行了两个while
循环。在第一个循环中,您仅显示背景。而且您永远不会结束第一个循环,因此它永远不会运行第二个循环,从而在背景上绘制精灵。您应该删除第一个循环。