Home > other >  Pygame: why the if play_button. The rect. Collidepoint (mouse_y mouse_x) : failure
Pygame: why the if play_button. The rect. Collidepoint (mouse_y mouse_x) : failure

Time:10-01

This is a type of small game, click the play button, the top of the screen to start falling random letters, keyboard keys, the letter disappear, have now completed most of the code, but there's a problem: click the play button, the program has not started, I found that the problem may be in game_functions check_play_button of py () method if play_button. The rect. Collidepoint (mouse_y mouse_x) : because commented this way, stats. Game_active become True, but not the conditional statement, click on the screen any place can change the stats. Game_active evaluates to True, the effect of this is not what I want, please have a look at how to solve? Thank you very much!
The HTML code is as follows: (a little longer, but it will be a challenge)
1. Alphaget_zoo. Py
 import pygame 
The import time
The from pygame. Locals import *
The from Settings import Settings
The import game_functions as gf
The from game_stats import GameStats
From the button the import button

Def run_game () :
Pygame. The init ()
Az_settings=Settings ()
Screen=pygame. Display. Set_mode ((0, 0), the RESIZABLE)
Pygame. Display. Set_caption (" Alphabet Zoo ")
Play_button=Button (screen, "Play")
Stats=GameStats (az_settings)
Letters=pygame. Sprite. Group ()
Start=time. Time ()
SleepTime=3


While True:
Now=time. Time ()
Gf. Check_events (letters, stats, play_button)
If stats. Game_active:
Gf. Update_screen (az_settings, stats, screen, letters, play_button)
If now - start & gt; SleepTime:
Gf. Letter_generator (az_settings, screen, letters)
Start=now
The else:
Gf. Update_screen (az_settings, stats, screen, letters, play_button)



Run_game ()

2. The button. Py
 import pygame. The font 


The class Button () :


Def __init__ (self, screen, MSG) :
The self. The screen=screen
Self. Screen_rect=screen. Get_rect ()

Self. The width of the self. The height=200, 50
Self. Button_color=(0, 0, 255)
Self. Text_color=(255, 255, 255)
The self. The font=pygame. The font. SysFont (None, 48)

The self. The rect=pygame. The rect (0, 0, the self. The width, the self. Height)
The self. The rect. Center=self. Screen_rect. Center

Self. Prep_msg (MSG)

Def prep_msg (self, MSG) :
Self. Msg_image=self. The font. Render (MSG, True, the self text_color,
Self. Button_color)
Self. Msg_image_rect=self. Msg_image. Get_rect ()
Self. Msg_image_rect. Center=self. The rect. Center

Def draw_button (self) :
The self. The screen. The fill (self. Button_color, the self. The rect)
The self. The screen. The blit (self. Msg_image, self. Msg_image_rect)

3. Settings. Py
 class Settings () : 
Def __init__ (self) :
Self. Bg_color=(0, 0, 0)
Self. Letter_speed_factor=10
Self. Lives_limit=10

4. Game_stats. Py
 class GameStats () : 
Def __init__ (self, az_settings) :
Self. Az_settings=az_settings
Self. Reset_stats ()
Self. Game_active=False

Def reset_stats (self) :
Self. Lives_left=self. Az_settings. Lives_limit

5. Letter. Py
 import pygame 
Import the random
The from pygame. Sprite import Sprite


The class Letter (Sprite) :

Def __init__ (self, az_settings, screen) :
Super () __init__ ()
The self. The screen=screen
Self. Az_settings=az_settings
A=random. Randint (97, 122)
C=CRH (a)
Self. Image=pygame. Image. The load (' images/+ c.u pper () + 'PNG')
The self. The ASCII=a

The self. The rect=self. Image. Get_rect ()
Self. Screen_rect=screen. Get_rect ()

The self. The rect. Centerx=random. Randint (0, the self. Screen_rect. Right)
The self. The rect. Top=self. Screen_rect. Top
Self center=float (self. The rect. Centerx)



Def update (self) :
If the self. The rect. Bottom & lt; Self. Screen_rect. Bottom:
The self. The rect. Centery +=self. Az_settings. Letter_speed_factor

6. Game_functions. Py
 import sys 
The import pygame
The from letter import letter

Def letter_generator (az_settings, screen, letters) :
New_letter=Letter (az_settings, screen)
Letters. The add (new_letter)

Def check_events (letters, stats, play_button) :
For the event in pygame. Event. The get () :
If the event. The type==pygame. QUIT:
Sys. The exit ()

Elif event. Type==pygame. MOUSEBUTTONDOWN:
Mouse_x, mouse_y=pygame. Mouse. Get_pos ()
Check_play_button (stats, play_button mouse_x, mouse_y)
Print (stats. Game_active) # for test

Elif event. Type==pygame. KEYDOWN:
For LTR in letters:
If LTR. ASCII==event. The key:
Letters. Remove (LTR)

Def check_play_button (stats, play_button mouse_x, mouse_y) :
If play_button. The rect. Collidepoint (mouse_y mouse_x) :
Stats. Game_active=True


Def letter_fallen (stats) :
If stats. Lives_left & gt; 0:
Stats. Lives_left -=1
The else:
Stats. Game_active=False

Def check_letter_bottom (screen, letters, stats) :
Screen_rect=screen. Get_rect ()
For LTR in letters. Sprites () :
If LTR. The rect. Bottom & gt; Screen_rect. Bottom: # s took be some the problems
LTR. The rect. Bottom=screen_rect. Bottom
Letter_fallen (stats)


Def update_screen (az_settings, stats, screen, letters, play_button) :
Screen. The fill (az_settings. Bg_color)
Check_letter_bottom (screen, letters, stats)
nullnullnullnullnullnull
  • Related