我对pygame和python很新,最近开始为学校项目创建以下内容但问题是,对象apple没有移动或掉线,但其他一切正常!请帮帮我!我的完整代码如下:
import sys, pygame, random
from menu_lib import *
from credit import credit
class Player(pygame.sprite.Sprite):
def __init__(self, *groups):
super(Player, self).__init__(*groups)
self.image = pygame.image.load('catcher_left.png')
self.rect = pygame.rect.Rect((320,240), self.image.get_size())
self.rect.bottom = 452
self.rect.left = 320
def update(self):
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.rect.x -= 10
self.image = pygame.image.load('catcher_left.png')
pygame.display.flip()
if key[pygame.K_RIGHT]:
self.rect.x += 10
self.image = pygame.image.load('catcher_right.png')
pygame.display.flip()
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > 640:
self.rect.right = 640
#if self.rect.colliderect(Apple.rect):
#print "Collided!"
class Apple(object):
def __init__(self):
self.image = pygame.image.load('apple.png')
self.rect=pygame.rect.Rect((32,37), self.image.get_size())
self.rect.top=0
self.rect.left=0
def draw(self, surface):
surface.blit(self.image, (self.rect.top, self.rect.left))
def update(self):
if self.rect.top==0:
self.rect.top+=10
self.rect.left=random.randint(10, 630)
elif self.rect.top!=0 and self.rect.top>480:
self.rect.top+=10
elif self.rect.top>=480:
self.rect.top=0
class Game(object):
def main(self, screen):
clock = pygame.time.Clock()
background = pygame.image.load('background.jpg')
background = pygame.transform.scale(background, (640, 480))
apple_count=0
sprites = pygame.sprite.Group()
self.player = Player(sprites)
apple = Apple()
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
screen.fill((0,0,0))
pygame.display.flip()
Mainmenu()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
screen.fill((0,0,0))
pygame.display.flip()
Mainmenu()
sprites.update()
apple.update()
screen.fill((200, 200, 200))
screen.blit(background, (0, 0))
sprites.draw(screen)
apple.draw(screen)
score_txt='Score: '+str(apple_count)
label = myfont.render(score_txt, 1, (255,0,0))
screen.blit(label, (500, 10))
pygame.display.flip()
def Mainmenu():
screen = pygame.display.set_mode((640, 480))
menu = cMenu(50, 50, 20, 5, 'vertical', 300, screen,
[('Start Game', 1, None),
('Credits', 2, None),
('Exit', 3, None)])
menu.set_center(True, True)
menu.set_alignment('center', 'center')
state = 0
prev_state = 1
rect_list = []
pygame.event.set_blocked(pygame.MOUSEMOTION)
while 1:
key = pygame.key.get_pressed()
if key[pygame.K_ESCAPE]:
pygame.quit()
sys.exit()
if prev_state != state:
pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key = 0))
prev_state = state
e = pygame.event.wait()
if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE:
if state == 0:
rect_list, state = menu.update(e, state)
elif state == 1:
screen = pygame.display.set_mode((640, 480))
Game().main(screen)
elif state == 2:
credits()
else:
print 'Exit!'
pygame.quit()
sys.exit()
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update(rect_list)
def credits():
clock = pygame.time.Clock()
screen=pygame.display.set_mode((640,480))
screen.fill((0, 0, 0))
text = "Credits \n _ _ _ _ _ _ _ _ _ _ _ _ \n\n\n Designer \n Creator \n\n Manikiran P"
color = 0xa0a0a000
credit(text,myfont,color)
Mainmenu()
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
screen.fill((0,0,0))
pygame.display.flip()
Mainmenu()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
screen.fill((0,0,0))
pygame.display.flip()
Mainmenu()
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
screen=pygame.display.set_mode((640,480))
pygame.display.set_caption("Evenure - (c) Manikiran")
myfont = pygame.font.Font("captureit.ttf", 20)
Mainmenu()
答案 0 :(得分:0)
您在elif语句中输入了错误:
elif self.rect.top!=0 and self.rect.top>480:
校正:
elif self.rect.top!=0 and self.rect.top<480: