【问题标题】:python if statement fails to compute compound logicpython if语句无法计算复合逻辑
【发布时间】:2014-11-24 22:59:05
【问题描述】:

这是我程序中的所有代码,但请查看 titleScreen() 类中的 get_click() 方法。通过测试,我得出结论,该方法确实记录了我的鼠标点击,但不会处理 if 语句中返回的坐标以产生结果(在这种情况下,只需打印按下了哪个按钮)。为什么它不起作用?

import pygame
from pygame import *
import math
import sys
#Presets for window
size=width,height=500,500
Ag=-9.80665
clock = pygame.time.Clock()
white=(255,255,255)
blue=(0,0,255)
red=(255,0,0)
gray_bgColor=(190,193,212)
#Initialise pygame Surface as screen
pygame.init()
pygame.font.init()
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Flappy Kid")
#Game Presets
vY=0
xPos,yPos=200,100
score=0

on_title_screen=True

class graphics():
    #Holds the methods for loading/displaying graphics
    def load_images(self):
        #Loads the background and sprite images
        self.background_image=pygame.image.load("flappy_background.png").convert()
        self.bird_image=pygame.image.load("flappy_sprite.jpg").convert()
        screen.set_colorkey(white)
        self.birdHitBox=self.bird_image.get_rect()
    def show_background(self):
        #blits the background
        screen.blit(self.background_image,[0,0])

class titleScreen():
    #Holds the methods for the title screen/menu
    def title(self):
        #Sets up the title
        titleText="Flappy Game"
        titlePos=(0,0)
        currentFont=pygame.font.SysFont("arialms",30,bold=True,italic=True)
        renderTitle=currentFont.render(titleText,1,blue,gray_bgColor)
        self.titlex,self.titley=currentFont.size(titleText)
        screen.blit(renderTitle,titlePos)
    def start(self):
        #Sets up the start Button
        startText="Start Game"
        self.startPos=(0,self.titley)
        currentFont=pygame.font.SysFont("arialms",25,bold=False,italic=False)
        renderStart=currentFont.render(startText,1,blue,gray_bgColor)
        self.startx,self.starty=currentFont.size(startText)
        screen.blit(renderStart,self.startPos)
    def quit(self):
        #Sets up the quit button
        quitText="Quit"
        self.quitPos=(0,self.starty+self.titley)
        currentFont=pygame.font.SysFont("arialms",25,bold=False,italic=False)
        renderQuit=currentFont.render(quitText,1,red,gray_bgColor)
        self.quitx,self.quity=currentFont.size(quitText)
        screen.blit(renderQuit,self.quitPos)
    def get_click(self):
        #Gets mouse click and processes outcomes
        for event in pygame.event.get():
            if event.type==pygame.MOUSEBUTTONDOWN:
                x,y=pygame.mouse.get_pos()
                #Tests for start:
                if (x>self.startPos[0] and x<self.startx) and (y>self.startPos[1] and y<self.starty):
                    #on_title_screen=False
                    print("start")
                #Tests for quit:
                elif (x>self.quitPos[0] and x<self.quitx) and (y>self.quitPos[1] and y<self.quity):
                    #sys.exit()
                    print("quit")

titleC=titleScreen()
graphicsC=graphics()
def setupTitle():
    #bundles all title_screen functions
    titleC.title()
    titleC.start()
    titleC.quit()

def main():
    graphicsC.load_images()
    graphicsC.show_background()
    setupTitle()
    while True:
        clock.tick(30)
        if not on_title_screen:
            for event in pygame.event.get():
                if event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_UP:
                        vY=-10
            if y>height-50:
                y=100
            vY+=1
            y+=vY
        elif on_title_screen:
            titleC.get_click()
        pygame.display.flip()

main()

【问题讨论】:

    标签: python if-statement methods pygame


    【解决方案1】:

    您的条件不满足产生输出。

    如果我这样做并点击“开始游戏”

    for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                x, y = pygame.mouse.get_pos()
                print x,y, self.startPos[0], self.startx, self.startPos[1], self.starty
    

    我得到'39 31 0 100 21 18'

    x = 39
    y = 31
    self.startPos[0] = 0
    self.startx = 100
    self.startPos[1] = 21
    self.starty = 18
    

    “x”条件很好(据我所知)。虽然'y'搞砸了

    (x > self.startPos[0] and x < self.startx): 
    ( 39 > 0 and 39 < 100): 
    (true and true) = True
    
    (y > self.startPos[1] and y < self.starty): 
    (31 > 21 and 31 < 18): 
    (true and false) = False
    

    'y'[31] 不能小于 18 且大于 21,因此永远不会满足条件。

    self.startPos1 应该是 21,self.starty 应该是 32。

    你应该更加小心你的变量命名,它们会产生误导,而且,pygame 有一个 rectangle 属性,非常适合你所针对的这种行为。

    【讨论】:

    • 谢谢。你知道它具体是什么矩形属性吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多