简体   繁体   English

Python类出现问题

[英]Having Trouble with Python Classes

So I am making a simple "Dodge the Meteor Game" with Python 27 and Pygame. 因此,我使用Python 27和Pygame制作了一个简单的“道奇流星游戏”。 So everything ran smoothly, until I wanted to make classes, so I could make multiple meteors without retyping the same code. 因此一切运行顺利,直到我想创建类,这样我才可以制作多个流星而无需重新键入相同的代码。 After I did this, when I run it, it stops responding with no error message. 完成此操作后,当我运行它时,它停止响应且没有错误消息。 Here is my code: 这是我的代码:

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()

I don't know what I'm doing wrong, but I know it is with the classes. 我不知道我在做什么错,但是我知道这是与类有关的。 Thanks in advance 提前致谢

Hobby Programmer, Derek 业余程序员,Derek

if event.key == K_RIGHT:
    move_x = 0

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

See how the indentation changes? 看到缩进如何变化? In Python, indentation is very important. 在Python中,缩进非常重要。 Because all of your code after the line 'move_x = 0' is not indented adequately, it is not part of your while loop; 由于行'move_x = 0'之后的所有代码都没有适当缩进,因此它不是while循环的一部分; therefore, it does not get executed in the loop. 因此,它不会在循环中执行。 Fix your indentation. 修正缩进。

Well, it's probably because gameStart is always False . 好吧,可能是因为gameStart始终为False So you're never getting into the game loop. 因此,您永远不会进入游戏循环。

You should get to know debugging. 您应该了解调试。 You can use pdb or any IDE like Eclipse. 您可以使用pdb或任何类似Eclipse的IDE。 The important thing is that it can help you understand what code is being running. 重要的是它可以帮助您了解正在运行的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM