首页 > 解决方案 > 播放与矩形碰撞时如何使随机文本出现

问题描述

正如标题所说,我正在努力做到这一点,以便当我的玩家与我的矩形/点类碰撞时,它会使我的 5 个单词中的一个看起来像“酷”或“好”。我遇到的问题是我试图让它随机出现一个单词,但我不知道如何让它随机绘制其中一个。我曾尝试使用随机发生器让它绘制我的一个词,但我不确定如何制作它,以便它随机化我的词,然后在每次与我的矩形/点类碰撞时选择其中一个来绘制。

这是我正在使用的词

font = pygame.font.Font("img/Snow.ttf",60)
text3 = font.render("Cool",True,(0,0,220))
textRect3 = text.get_rect()
textRect3.center = ((250,120))

font = pygame.font.Font("img/Bubble.ttf",60)
text4 = font.render("AWESOME",True,(145,0,145))
textRect4 = text.get_rect()
textRect4.center = ((250,120))

font = pygame.font.Font("img/Bubble.ttf",60)
text5 = font.render("AMAZING",True,(200,200,0))
textRect5 = text.get_rect()
textRect5.center = ((250,120))

font = pygame.font.Font("img/Bubble.ttf",60)
text6 = font.render("UNIMAGINABLE",True,(250,165,0))
textRect6 = text.get_rect()
textRect6.center = ((250,120))

font = pygame.font.Font("img/Blazed.ttf",60)
text7 = font.render("FIRE",True,(220,0,0))
textRect7 = text.get_rect()
textRect7.center = ((250,120))

这是我的完整代码

import pygame,random
pygame.init()

width = 500
height = 600
# Screen width and height
window = pygame.display.set_mode((width,height))
# Name of the Screen
pygame.display.set_caption("Game")
# The background


# player class
class Player:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 5
        self.JumpCount = False
        self.isJump = 10
        self.fall = 0
        self.rect = pygame.Rect(x,y,width,height)
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)
        
# Point class
class Point:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 3
        self.rect = pygame.Rect(x,y,width,height)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)


# Color
white = (255,255,255)
green = (0,255,0)
blue = (0,0,255)
# Calss's cords,size, and color
playerman = Player(200,250,40,40,white)

point1 = Point(400,300,40,70,white)
point2 = Point(770,300,40,70,white)

# All my list
points = [point1,point2]

# Point system
font = pygame.font.Font("img/CAT.ttf",60)
score = 0
text = font.render(""+str(score), True,(255,255,255))
textRect = text.get_rect()
textRect.center = ((250,60))

font2 = pygame.font.Font("img/Bubble.ttf",60)
text2 = font2.render("NICE",True,(0,200,0))
textRect2 = text.get_rect()
textRect2.center = ((250,120))

font3 = pygame.font.Font("img/Snow.ttf",60)
text3 = font3.render("Cool",True,(0,0,220))
textRect3 = text.get_rect()
textRect3.center = ((250,120))

font4 = pygame.font.Font("img/Bubble.ttf",60)
text4 = font4.render("AWESOME",True,(145,0,145))
textRect4 = text.get_rect()
textRect4.center = ((250,120))

font5 = pygame.font.Font("img/Bubble.ttf",60)
text5 = font5.render("AMAZING",True,(200,200,0))
textRect5 = text.get_rect()
textRect5.center = ((250,120))

font6 = pygame.font.Font("img/Bubble.ttf",60)
text6 = font6.render("UNIMAGINABLE",True,(250,165,0))
textRect6 = text.get_rect()
textRect6.center = ((250,120))

font7 = pygame.font.Font("img/Blazed.ttf",60)
text7 = font7.render("FIRE",True,(220,0,0))
textRect7 = text.get_rect()
textRect7.center = ((250,120))


words = [text5,text7]

# Displaying class's in main loop
def redrawwindow():
    window.fill((0,0,0))

    # Drawing my classes and other things
    playerman.draw()

    for Point in points:
        Point.draw()
    window.blit(text,textRect)



# Making point get off screen
Ptimer = 0
Ptimer2 = 0
# for playing sprite when player jumps
Jumping = 0
spcdown = False
fps = 35
clock = pygame.time.Clock()
run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False


    # Making the rectangles get off the screen for a short amount of time
    if Ptimer > 0:
        Ptimer += 1
        point1.y = -200
        point1.rect.topleft = (point1.x, point1.y)
    if Ptimer >= 15:
        Ptimer = 0
        

    if Ptimer2 > 0:
        Ptimer2 += 1
        point2.y = -200
        point2.rect.topleft = (point2.x, point2.y)
    if Ptimer2 >= 20:
        Ptimer2 = 0
        
    for Point in points:
        Point.x -= Point.speed
    for Point in points:
        if Point.x <= -200:
            Point.x = 550
            Point.y = 300


    #The player collideing with the point's
    if playerman.rect.colliderect(point1.rect):
        Ptimer += 1
        score += 1
        text = font.render(""+str(score), True,(255,255,255))
        

    if playerman.rect.colliderect(point2.rect):
        Ptimer2 += 1
        score += 1
        text = font.render(""+str(score), True,(255,255,255))
        
    keys = pygame.key.get_pressed()


    if not keys[pygame.K_SPACE]:
        spcdown = False  # space released
    
    if keys[pygame.K_SPACE]:
        Jumping = 1
        if not spcdown:
            spcdown = True
        
        

    collide = False

    playerman.y += playerman.speed
    # bird moving
    if not playerman.isJump:
        # [...]

        # the bird is allowed to jump even if it is not colliding:
        if keys[pygame.K_SPACE]:
            playerman.isJump = True

        if collide:
            playerman.fall = 0

    else:
        if playerman.JumpCount > 0:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.JumpCount = 10
            
            # if K_SPACE is pressed, then the bird keeps jumping
            if not keys[pygame.K_SPACE]:
                playerman.isJump = False


    
    
    redrawwindow()
    pygame.display.update()
pygame.quit()
        


标签: pythonfontspygame

解决方案


使用random模块choice方法。您将所需单词的列表传递给方法,就像这样

import random

words = ["Nice", "Cool", ..more words] 
random_word = random.choice(words)  # Will return a random element from the list

推荐阅读