Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python类有问题吗_Python_Class_Pygame - Fatal编程技术网

Python类有问题吗

Python类有问题吗,python,class,pygame,Python,Class,Pygame,所以我用Python27和Pygame制作了一个简单的“躲避流星游戏”。所以一切都很顺利,直到我想要创建类,这样我就可以在不重新键入相同代码的情况下创建多个流星。在我这样做之后,当我运行它时,它停止响应,没有错误消息。这是我的密码: import pygame from pygame.locals import * import sys import random pygame.init() width,height = 800,600 gameDisplay = pygame.displ

所以我用Python27和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()
导入pygame
从pygame.locals导入*
导入系统
随机输入
pygame.init()
宽度、高度=800600
gameDisplay=pygame.display.set_模式((宽度、高度))
pygame.display.set_标题(“十五分钟游戏”)
gameStart=False
bg=pygame.image.load(“C:\Users\DEREK\Desktop\Python\\space.jpg”)
bg=pygame.transform.scale(bg,(900600))
x=300
y=300
移动x=0
移动y=0
球员速度=3
小球x=random.randint(0800)
小球y=random.randint(0550)
player=pygame.draw.rect(游戏显示,(255255255),(x,y,30,30))
pellet=pygame.draw.rect(游戏显示,(255255255),(pellet_x,pellet_y,15,15))
计数=0
#功能
def pelletxy():
全球颗粒x,颗粒y
小球_x=random.randint(0770)
小球y=random.randint(0570)
def冲突(rect1、rect2):
全球玩家、计数、小球
如果rect1.collide rect(rect2):
如果rect2==颗粒:
pelletxy()
计数+=1
类Meteor():
定义初始化(自):
self.meteor_x=random.randint(0800)
self.meteor_y=0
自我流星雨=3
self.meteor=pygame.draw.rect(游戏显示,(255255255),(self.meteor_x,self.meteor_y,35,35))
def碰撞(自身、rect1、rect2):
如果rect1.collide rect(rect2):
如果rect2==self.meteor:
打印“好游戏”
打印“哈哈哈”
打印“”
打印“您的分数:”+str(计数)
pygame.quit()
sys.exit()
def流星XY(自身):
self.meteor_x=random.randint(0800)
self.meteor_y=0
def渲染(自):
self.meteor_y+=self.meteofall
自我流星
如果流星>600:
流星
m1=流星()
#游戏循环
游戏开始时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
#键盘运动
如果event.type==pygame.KEYDOWN:
如果event.key==K_UP:
move_y-=玩家速度
如果event.key==K_DOWN:
移动y+=玩家速度
如果event.key==K_LEFT:
move_x-=玩家速度
如果event.key==K_RIGHT:
move_x+=玩家速度
如果event.type==pygame.KEYUP:
如果event.key==K_UP:
移动y=0
如果event.key==K_DOWN:
移动y=0
如果event.key==K_LEFT:
移动x=0
如果event.key==K_RIGHT:
移动x=0
#计算新位置
x=x+移动x
y=y+移动y
#停止边界上的移动
如果x>830:
x=-30
elif x<-30:
x=830
elif y<-30:
y=630
elif y>630:
y=-30
#检查不同的碰撞场景
碰撞(玩家、小球)
m1.碰撞(玩家,m1.流星)
#把东西画到屏幕上
游戏显示.blit(背景,(0,0))
player=pygame.draw.rect(游戏显示,(255255255),(x,y,30,30))
pellet_outline=pygame.draw.rect(游戏显示,(255255255),((pellet_x-1),(pellet_y-1),17,17))
小球=pygame.draw.rect(游戏显示,(0,0255),(小球x,小球y,15,15))
m1.渲染
pygame.display.update()
我不知道我做错了什么,但我知道这是在课堂上。提前谢谢

爱好程序员,德里克

if event.key == K_RIGHT:
    move_x = 0

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

看看缩进是如何变化的?在Python中,缩进非常重要。因为“move_x=0”行之后的所有代码没有充分缩进,所以它不是while循环的一部分;因此,它不会在循环中执行。修正你的缩进。

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


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

您如何“知道类是这样的”?它“在没有错误消息的情况下停止响应”这一事实向我暗示,您可能陷入了一个无限循环。如果在
while
/
循环中的不同位置放置一些打印语句,会发生什么?谢谢,这是while循环。我不小心将gamestart变量设置为False。当我运行它时,一切都正常,但没有流星出现。我已经找到了解决我自己问题的方法。这是一个粗心的错误,因为我忘了在m1.render()函数后面加上()。但是谢谢你的支持!我在复制代码时忘了缩进,但我确实在原稿上有缩进。但它似乎仍然不起作用。