遇到麻烦的Python类

时间:2015-11-28 23:24:22

标签: python class pygame

所以我正在做一个简单的" Dodge the Meteor Game"使用Python 27和Pygame。所以一切都顺利进行,直到我想要上课,所以我可以制作多个流星而无需重新输入相同的代码。在我这样做之后,当我运行它时,它停止响应,没有错误消息。这是我的代码:

import pygame
from pygame.locals import *
import sys
import random

pygame.init()

width,height = 800,600

gameDisplay = pygame.display.set_mode((width,height))
pygame.display.set_caption("Fifteen Minute Game ")
gameStart = False

bg = pygame.image.load("C:\Users\DEREK\Desktop\Python\\space.jpg")
bg = pygame.transform.scale(bg,(900,600))

x = 300
y = 300
move_x = 0
move_y = 0

playerspeed = 3

pellet_x = random.randint(0,800)
pellet_y = random.randint(0,550)

player = pygame.draw.rect( gameDisplay, (255,255,255), (x,y,30,30) )
pellet = pygame.draw.rect( gameDisplay, (255,255,255),      (pellet_x,pellet_y,15,15) )

count = 0

#Functions
def pelletxy():
    global pellet_x, pellet_y
    pellet_x = random.randint(0,770)
    pellet_y = random.randint(0,570)

def collision(rect1,rect2):
    global player, count, pellet
    if rect1.colliderect(rect2):
        if rect2 == pellet:
            pelletxy()
            count +=1

class Meteor():
    def __init__(self):
        self.meteor_x = random.randint(0,800)
        self.meteor_y = 0
        self.meteorfall = 3
        self.meteor = pygame.draw.rect( gameDisplay, (255,255,255), (self.meteor_x,self.meteor_y,35,35) )

    def collision(self,rect1,rect2):
        if rect1.colliderect(rect2):
            if rect2 == self.meteor:
                print "Good Game"
                print "MUA HAHAHAHA"
                print ""
                print "Your score:" + str(count)
                pygame.quit()
                sys.exit()

    def meteorxy(self):
        self.meteor_x = random.randint(0,800)
        self.meteor_y = 0

    def render(self):
        self.meteor_y += self.meteorfall
        self.meteor
        if meteor_y > 600:
            meteorxy()

m1 = Meteor()    

#Game Loop
while gameStart:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        # Keyboard Movement
        if event.type == pygame.KEYDOWN:
            if event.key == K_UP:
                move_y -= playerspeed
            if event.key == K_DOWN:
                move_y += playerspeed
            if event.key == K_LEFT:
                move_x -= playerspeed
            if event.key == K_RIGHT:
                move_x += playerspeed

        if event.type == pygame.KEYUP:
            if event.key == K_UP:
                move_y = 0
            if event.key == K_DOWN:
                move_y = 0
            if event.key == K_LEFT:
                move_x = 0
            if event.key == K_RIGHT:
                move_x = 0

#Calculate new position  
x = x + move_x 
y = y + move_y

#Stop Movement on boundaries 
if x > 830:
    x = -30
elif x < -30:
    x = 830
elif y < -30:
    y = 630
elif y > 630:
    y = -30

#Check Different Collision Scenarios
collision(player, pellet)
m1.collision(player, m1.meteor)

#Draw the things onto the screen
gameDisplay.blit(bg,(0,0))

player = pygame.draw.rect( gameDisplay, (255,255,255), (x,y,30,30) )

pellet_outline = pygame.draw.rect( gameDisplay, (255,255,255), ((pellet_x - 1), (pellet_y - 1), 17,17))
pellet = pygame.draw.rect( gameDisplay, (0,0,255), (pellet_x,pellet_y,15,15) )

m1.render

pygame.display.update()

我不知道自己做错了什么,但我知道这是关于课程的。提前致谢

爱好程序员,Derek

2 个答案:

答案 0 :(得分:1)

if event.key == K_RIGHT:
    move_x = 0

#Calculate new position  
x = x + move_x 
y = y + move_y

了解缩进的变化情况?在Python中,缩进非常重要。因为'move_x = 0'行之后的所有代码都没有充分缩进,所以它不是while循环的一部分;因此,它不会在循环中执行。修复你的缩进。

答案 1 :(得分:1)

嗯,这可能是因为gameStart总是False。所以你永远不会进入游戏循环。

你应该了解调试。您可以使用pdb或任何IDE,如Eclipse。重要的是,它可以帮助您了解正在运行的代码。