2015-11-04 122 views
0

我正在重写我的问题 Pygame - rect disappears before I reach it因为我想我没有描述清楚。Pygame - rect出现并立即消失

全码:

#! /usr/bin/python 

import pygame, time, sys, random, os 
from pygame.locals import * 
from time import gmtime, strftime 

pygame.init() 

w = 640 
h = 400 

screen = pygame.display.set_mode((w, h),RESIZABLE) 
clock = pygame.time.Clock() 
x = y = 100 

def starting(): 
    basicfont = pygame.font.SysFont(None, 48) 
    text = basicfont.render('Starting...', True, (255, 255, 255), (0, 0, 255)) 
    textrect = text.get_rect() 
    textrect.centerx = screen.get_rect().centerx 
    textrect.centery = screen.get_rect().centery 
    screen.fill((0, 0, 255)) 
    screen.blit(text, textrect) 
    pygame.display.update() 

def taskbar(): 
    basicfont = pygame.font.SysFont(None, 24) 
    pygame.draw.rect(screen, ((0, 255, 0)), (0, h-40, w, 40), 0) 
    text = basicfont.render(strftime("%Y-%m-%d", gmtime()), True, (0, 0, 0)) 
    text2 = basicfont.render(strftime("%H:%M:%S", gmtime()), True, (0, 0, 0)) 
    screen.blit(text, (w - 100, h - 37)) 
    screen.blit(text2, (w - 100, h - 17)) 
    logoimage = pygame.image.load("C:\Users\chef\Desktop\logo.png").convert() 
    logoimage = pygame.transform.scale(logoimage, (40, 40)) 
    screen.blit(logoimage, (0, h-40),) 

def start(): 
    button1, button2, button3 = pygame.mouse.get_pressed() 
    mousex, mousey = pygame.mouse.get_pos() 
    if 0 < mousex < 40 and h-40 < mousey < h: 
     if button1: 
      pygame.draw.rect(screen, ((255, 0, 0)), (0, int(h/2), int(w/6), int(h/2)-40), 0) 
    pygame.display.update() 

starting() 
#time.sleep(2) 

while 1: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      sys.exit() 
     elif event.type==VIDEORESIZE: 
      w = event.dict['size'][0] 
      h = event.dict['size'][1] 
      screen=pygame.display.set_mode(event.dict['size'],RESIZABLE) 
    screen.fill((255, 255, 255)) 
    taskbar() 
    start() 
    pygame.display.update() 
    clock.tick(60) 

就像我原来的问题,当我按下logoimage.png和红色矩形弹出,它显示了第二个,然后立即消失。我怎样才能做到这一点,直到我将它压在外面?

+2

你应该**编辑前面的问题**,不问一个新的。 – jonrsharpe

+1

[Pygame - rect在我到达之前就消失了]的可能重复(http://stackoverflow.com/questions/33506991/pygame-rect-disappears-before-i-reach-it) –

+0

@JosepValls我知道它是重复的我原来的问题,我只是问了一个新的问题,因为我认为一个新的问题比编辑我的旧问题更好。 –

回答

2

你应该移动screen.fill((255, 255, 255))while循环。这会导致问题,因为每次程序循环时,它都会将显示屏上色为白色,并将之前的任何内容写下来。解决此问题的一种方法是将screen.fill((255, 255, 255))移出循环,并将其放在pygame.draw.rect函数之前。这会在放置矩形之前为屏幕着色,并且不会继续填充它。例如:

if button1: 
      screen.fill((255, 255, 255)) 
      pygame.draw.rect(screen, ((255, 0, 0)), (0, int(h/2), int(w/6), int(h/2)-40), 0) 

你也可以移动到其他位置,或想出一个办法来绘制矩形每个循环您填写的窗口之后。

希望这有助于。

0

您需要标记一个已被点击的布尔值。喜欢的东西:

btnClicked = false 

def start(): 
    button1, button2, button3 = pygame.mouse.get_pressed() 
    mousex, mousey = pygame.mouse.get_pos() 
    if button1: 
     if 0 < mousex < 40 and h-40 < mousey < h: 
      btnClicked = true 
     else: 
      btnClicked = false 

    if btnClicked: 
     pygame.draw.rect(screen, ((255, 0, 0)), (0, int(h/2), int(w/6), int(h/2)-40), 0) 
    pygame.display.update() 
+0

我曾尝试过,但没有任何改变。 –

0

这里是我的代码,我不知道为什么我的文字不断消失。

import pygame 
import sys 
from pygame.locals import * 
import math 
class Pong: 
    def __init__(self): 
     self.screen = pygame.display.set_mode((800, 600)) 
     pygame.font.init() 
     self.font = pygame.font.Font("pongFont.TTF", 30) 
     self.ball = pygame.Rect(400, 300, 10, 10) 
     self.ballAngle = math.radians(0) 
     self.ballSpeed = 10 
     self.playerScore = 0 
     self.opponentScore = 0 
     self.direction = -1 
     self.playerRects = { 
      -60:pygame.Rect(50, 380, 10, 20), # Bottom of paddle 
      -45:pygame.Rect(50, 360, 10, 20), 
      -30:pygame.Rect(50, 340, 10, 20), 
      -0:pygame.Rect(50, 320, 10, 20), 
      30:pygame.Rect(50, 300, 10, 20), 
      45:pygame.Rect(50, 280, 10, 20), 
      60:pygame.Rect(50, 260, 10, 20), # Top of paddle 
     } 

    self.opponentRects = { 
     -60:pygame.Rect(750, 380, 10, 20), # Bottom of paddle 
     -45:pygame.Rect(750, 360, 10, 20), 
     -30:pygame.Rect(750, 340, 10, 20), 
     -0:pygame.Rect(750, 320, 10, 20), 
     30:pygame.Rect(750, 300, 10, 20), 
     45:pygame.Rect(750, 280, 10, 20), 
     60:pygame.Rect(750, 260, 10, 20), # Top of paddle 
    } 

    self.pause = 10 

def drawPlayers(self): 
    for pRect in self.playerRects: 
     pygame.draw.rect(self.screen, (0,0,255), self.playerRects[pRect]) 
    for oRect in self.opponentRects: 
     pygame.draw.rect(self.screen, (255,0,0), self.opponentRects[oRect]) 

def updatePlayer(self): 
    key = pygame.key.get_pressed() 
    if self.ball.y <= 0 or self.ball.y > 595: 
     self.ballAngle *= -1 

    if key[K_UP]: 
     if self.playerRects[60].y > 0: 
      for pRect in self.playerRects: 
       self.playerRects[pRect].y -= 5 

    elif key[K_DOWN]: 
     if self.playerRects[-60].y < 590: 
      for pRect in self.playerRects: 
       self.playerRects[pRect].y += 5 

def updateBall(self): 
    self.ball.x += self.direction * self.ballSpeed * math.cos(self.ballAngle) 
    self.ball.y += self.direction * self.ballSpeed * -math.sin(self.ballAngle) 
    if self.ball.x > 800 or self.ball.x < 0: 
     if self.ball.x > 800: 
      self.playerScore += 1 
      self.ballSpeed += 1 

     elif self.ball.x < 0: 
      self.opponentScore += 1 
      self.ballSpeed -= 0.25 
     self.ball.x = 400 
     self.ball.y = 300 
     self.ballAngle = math.radians(45) 
     self.pause = 10 

     if self.opponentScore >= 10: 
      self.opponentScore = 0 
      self.playerScore = 0 

     if self.opponentScore == 0: 
      clock = pygame.time.Clock() 
      display = pygame.display.set_mode((800, 600)) 
      label = self.font.render("You Lose!", 0, (0,0,255)) 
      display.blit(label, (400, 300)) 
      pygame.display.flip() 
      clock.tick(60) 


     elif self.playerScore >= 10: 
      self.opponentScore = 0 
      self.playerScore = 0 

     if self.playerScore == 0: 
      clock = pygame.time.Clock() 
      display = pygame.display.set_mode((800, 600)) 
      label = self.font.render("You Lose!", 0, (0,0,255)) 
      display.blit(label, (400, 300)) 
      pygame.display.flip() 
      clock.tick(60) 
     return 

    if self.direction < 0: 
     for pRect in self.playerRects: 
      if self.playerRects[pRect].colliderect(self.ball): 
       self.ballAngle = math.radians(pRect) 
       self.direction = 1 
       break 

    else: 
     for oRect in self.opponentRects: 
      if self.opponentRects[oRect].colliderect(self.ball): 
       self.ballAngle = math.radians(oRect) 
       self.direction = -1 

def updateOpponent(self): 
    if self.ball.y > self.opponentRects[0].y: 
     if self.opponentRects[-60].y > 580: 
      return 
     for oRect in self.opponentRects: 
      self.opponentRects[oRect].y += 6 

    elif self.ball.y < self.opponentRects[0].y: 
     if self.opponentRects[60].y <= 0: 
      return 
     for oRect in self.opponentRects: 
      self.opponentRects[oRect].y -= 6 

def run(self): 
    clock = pygame.time.Clock() 
    while True: 
     self.screen.fill((0,0,0)) 
     clock.tick(60) 
     for event in pygame.event.get(): 
      if event.type == QUIT: 
       sys.exit() 


     self.screen.fill((0,0,0)) 
     pygame.draw.rect(self.screen, (255,255,255), self.ball) 
     self.screen.blit(self.font.render(str(self.playerScore), -1, (255,255,255)), (200, 25)) 
     self.screen.blit(self.font.render(str(self.opponentScore), -1, (255,255,255)), (600, 25)) 
     self.drawPlayers() 
     self.updatePlayer() 
     self.updateOpponent() 
     if self.pause: 
      self.pause -= 1 
     else: 
      self.updateBall() 

     pygame.display.flip() 

傍()的run()