ich habe es zum einen geschafft, drei Buttons zu erstellen und sie in einem Fenster zu öffnen, so das die Buttons konstant auf dem Bildschirm bleiben. Und zum anderen habe ich es geschafft einen Text zu schreiben und ihn auf dem Bildschirm relativ in der Mitte zu zentrieren.
Jetzt ist aber mein Problem das sich auf dem Bildschirm nur der Text aber keine Buttons anzeigen lassen obwohl die Buttons sich ja ohne Probleme im anderen Fenster öffnen lässen.
Code: Alles auswählen
import pygame
import time
import sys
pygame.init()
pygame.display.set_caption('Test')
size = [800, 600]
screen = pygame.display.set_mode(size)
def draw_text(text):
font = pygame.font.SysFont("arial", 35)
y_pos = 75
x_pos = 250
for line in text.splitlines():
for pos in range(1, len(line)+1):
text = font.render(line[:pos], 1, (255, 255, 255))
screen.blit(text, (x_pos, y_pos))
pygame.display.update()
time.sleep(0.1)
y_pos += 35
draw_text('This is a text\nwith more than\njust one line')
time.sleep(3)
pygame.MOUSEBUTTONDOWN
def button(bx,by,width,height,color_normal,farbe_aktiv, text= 'd'):
if mouse[0] > bx and mouse[0] < bx+width and mouse[1] > by and mouse[1] < by+height:
pygame.draw.rect(screen, farbe_aktiv, (bx,by,width,height))
else:
pygame.draw.rect(screen, color_normal, (bx,by,width,height))
def button_():
button(80, 375, 160, 60, (250, 250, 250), (250, 0, 0)) # rot
button(315, 375, 160, 60, (250, 250, 250), (250, 0, 0)) # blau
button(550, 375, 160, 60, (250, 250, 250), (250, 0, 0)) # grün
#Loop
game = True
while game:
draw_text()
button()
button_()
#close window
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
mouse = pygame.mouse.get_pos()
button(80, 375, 160, 60, (250, 250, 250), (250, 0, 0)) # rot
button(315, 375, 160, 60, (250, 250, 250), (250, 0, 0)) # blau
button(550, 375, 160, 60, (250, 250, 250), (250, 0, 0)) # grün
pygame.display.flip()
pygame.display.update()
Code: Alles auswählen
import random
tiere = ['Schaf','Kuh','Hase','Elefant']
r_tiere = random.choice(tiere)
Vielen Dank im vorraus