Python3 PyGame: Yazı ve Buton Oluşturmak, Create Text and Interactive Button

Oyun yapmak için kullanılan Python kütüphanelerinden biri olan PyGame' de yazı yazacak ve çalışır bir buton yapacağız.

Projenin Sonunda Elde Edeceğimiz Görüntü


Pencere Oluşturmak

import pygame
SIZE = WIDTH, HEIGHT = 640, 480
BLACK = 0, 0, 0
BGCOLOR = 65, 157, 120
WHITE = 255, 255, 255
FONTDIR = 'gravity.otf'

pygame.init()
pencere = pygame.display.set_mode(SIZE)
pygame.display.set_caption('Yazı Yazmak ve Buton Oluşturmak')

Burada pygame kütüphanesini import ettik ve penceremizin boyutlarını SIZE constant veri tipinde sakladık. 

Ayrıca daha sonra işimize yarayacak bazı renk kodlarını da en başta yazdık.
BGCOLOR yukarıdaki resimde de gördüğünüz pencerenin arka planının kodu. Siz isterseniz değiştirebilirsiniz.

Yazı yazacağımız için bir font'a ihtiyacımız olacak. İlla ki bu fontu kullanmak zorunda değilsiniz. İnternetten bedava font dosyaları bulabilirsiniz. Ve kodun içinde bu dosyanın yerini belirtmeniz gerekiyor. Eğer font dosyası, python dosyanızla aynı klasörde bulunursa FONTDIR verisine sadece font dosyasının ismini yazsanız yeterli.

pygame.init() fonksiyonuyla pygame'i başlatıyoruz.
Yukarıda belirlediğimiz pencere boyutlarını pygame.display.set_mode() içine yerleştiriyoruz ve en son satırda da penceremizin başlığı yer alıyor.

Arka Plan Rengi

calisiyor = True
while  calisiyor:
 for e in pygame.event.get():
  if e.type == pygame.QUIT: 
   calisiyor = False
 pencere.fill(BGCOLOR)
 pygame.display.flip()

pencere.fill(BGCOLOR) ile arka plan rengimizi belirledik.
üstte tanımladığımız calisiyor boolean verisiyle oyunumuzun ne zaman kapanacağını kontrol altına aldık.

pygame.event ile eğer pencerenin sağ üstündeki çarpıya tıklandıysa calisiyor verisini false'a çevirerek programı sonlandırmış oluruz.

pygame.display.flip() fonksiyonu penceremizi yenilemeye yarıyor. bunun yerine pygame.display.update() fonksiyonu da kullanılabilir.


Yazıyı Oluşturmak

font = pygame.font.Font(FONTDIR, 32)

textContent = 'KODLARDAN1BLOG'
calisiyor = True
while  calisiyor:
 for e in pygame.event.get():
  if e.type == pygame.QUIT: 
   calisiyor = False
 
 pencere.fill(BGCOLOR)

 text = font.render(textContent, True, WHITE)
 textRect = text.get_rect()
 textRect.center = (WIDTH / 2, HEIGHT / 2 - 60)
 pencere.blit(text, textRect)

 pygame.display.flip()

pygame.font.Font(fontAddress, fontSize)
font.render(text, antialiase, color, backgroundcolor)
pencere.blit(surface, points)


Butonu Oluşturmak

buttonIdleColor = 225, 199, 141
buttonHoverColor = 224, 164, 88
buttonPressedColor = 45, 48, 71
buttonWidth, buttonHeight = 200, 60
buttonX = WIDTH / 2 - buttonWidth / 2
buttonY = HEIGHT / 2 
buttonRect = pygame.Rect(buttonX, buttonY, buttonWidth, buttonHeight)

buttonColor = buttonIdleColor
textContent = 'KODLARDAN1BLOG'
calisiyor = True
while  calisiyor:
 for e in pygame.event.get():
  if e.type == pygame.QUIT: 
   calisiyor = False

 pencere.fill(BGCOLOR)

 text = font.render(textContent, True, WHITE)
 textRect = text.get_rect()
 textRect.center = (WIDTH / 2, HEIGHT / 2 - 60)
 pencere.blit(text, textRect)

 pygame.draw.rect(pencere, buttonColor, buttonRect)

 pygame.display.flip()

Burada butonumuzun pozisyonu belirlerken, yazıdaki gibi orta noktasını değil, butonun sol üst köşesini belirliyoruz. 
Bu sefer pygame.draw.rect(surface, color, rect) fonksiyonunu kullandık. Bununla aslında buton değil bir dikdörtgen oluşturduk.
buttonIdleColor : fare butonun üstünde değilken.
buttonHoverColor : fare butonun üstündeyken.
buttonPressedColor : fare butonun üstünde ve basılı iken.


Butona Ruhunu Üflemek

while  calisiyor:
 for e in pygame.event.get():
  if e.type == pygame.QUIT: 
   calisiyor = False

 mouseX, mouseY = pygame.mouse.get_pos()
 click = pygame.mouse.get_pressed()
 if buttonX + buttonWidth > mouseX > buttonX and buttonY + buttonHeight > mouseY > buttonY:
  buttonColor = buttonHoverColor
  buttonTextColor = BLACK
  if click[0] == 1:
   buttonColor = buttonPressedColor
   buttonTextColor = WHITE
   textContent = 'KODLARDAN1BLOG.com'
 else:
  buttonColor = buttonIdleColor
  buttonTextColor = BLACK

 pencere.fill(BGCOLOR)

 text = font.render(textContent, True, WHITE)
 textRect = text.get_rect()
 textRect.center = (WIDTH / 2, HEIGHT / 2 - 60)
 pencere.blit(text, textRect)

 pygame.draw.rect(pencere, buttonColor, buttonRect)

 pygame.display.flip()

mouse.get_pos() ile mouse imlecinin bulunduğu x ve y koordinatlarını alıyoruz ve onları mouseX ve mouseY değişkenlerine atadık.
mouse.get_pressed() ile hangi butonların basılı olup olmadığını anlayabiliyoruz. [sol tuş, kaydırma tuşu, sağ tuş] formatında bir veri veriyor. Ve sol tuşun basılı olup olmadığını anlamak için mouse.get_pressed()[0] kodunu kullanıyoruz.


Butonun Üstüne Yazı Yazmak

Burada önemli olan pozisyonu ayarlayabilmek yoksa normal yazı yazmaktan hiçbir farkı yok. Ve tabii yazıyı butonun üstüne yazdıracağımız için ekrana ilk önce butonu basıp daha sonra yazıyı yazmalıyız.

while  calisiyor:
 for e in pygame.event.get():
  if e.type == pygame.QUIT: 
   calisiyor = False

 mouseX, mouseY = pygame.mouse.get_pos()
 click = pygame.mouse.get_pressed()
 if buttonX + buttonWidth > mouseX > buttonX and buttonY + buttonHeight > mouseY > buttonY:
  buttonColor = buttonHoverColor
  buttonTextColor = BLACK
  if click[0] == 1:
   buttonColor = buttonPressedColor
   buttonTextColor = WHITE
   textContent = 'KODLARDAN1BLOG.com'
 else:
  buttonColor = buttonIdleColor
  buttonTextColor = BLACK

 pencere.fill(BGCOLOR)
 
 text = font.render(textContent, True, WHITE)
 textRect = text.get_rect()
 textRect.center = (WIDTH / 2, HEIGHT / 2 - 60)
 pencere.blit(text, textRect)
 
 pygame.draw.rect(pencere, buttonColor, buttonRect)

 buttonText = font.render('I LOVE', True, buttonTextColor)
 buttonTextRect = buttonText.get_rect()
 buttonTextRect.center = buttonRect.center
 pencere.blit(buttonText, buttonTextRect)
 
 pygame.display.flip()

Buton oluştururken pygame.Rect(x, y, width, height) kullanmanın avantajı bu dörtlünün tam ortasını pygame.Rect().center ile alabiliyor olmamız. Bu yüzden butonun ağırlık merkezini bulmak zor olmadı.


Projenin Son Hali ve Kodlar


import pygame
SIZE = WIDTH, HEIGHT = 640, 480
BLACK = 0, 0, 0
BGCOLOR = 65, 157, 120
WHITE = 255, 255, 255
FONTDIR = 'gravity.otf'

pygame.init()
pencere = pygame.display.set_mode(SIZE)
pygame.display.set_caption('Yazı Yazmak ve Buton Oluşturmak')

font = pygame.font.Font(FONTDIR, 32)

buttonIdleColor = 225, 199, 141
buttonHoverColor = 224, 164, 88
buttonPressedColor = 45, 48, 71
buttonWidth, buttonHeight = 200, 60
buttonX = WIDTH / 2 - buttonWidth / 2
buttonY = HEIGHT / 2 
buttonRect = pygame.Rect(buttonX, buttonY, buttonWidth, buttonHeight)

buttonColor = buttonIdleColor
textContent = 'KODLARDAN1BLOG'
calisiyor = True
while  calisiyor:
 for e in pygame.event.get():
  if e.type == pygame.QUIT: 
   calisiyor = False

 mouseX, mouseY = pygame.mouse.get_pos()
 click = pygame.mouse.get_pressed()
 if buttonX + buttonWidth > mouseX > buttonX and buttonY + buttonHeight > mouseY > buttonY:
  buttonColor = buttonHoverColor
  buttonTextColor = BLACK
  if click[0] == 1:
   buttonColor = buttonPressedColor
   buttonTextColor = WHITE
   textContent = 'KODLARDAN1BLOG.com'
 else:
  buttonColor = buttonIdleColor
  buttonTextColor = BLACK

 pencere.fill(BGCOLOR)
 
 text = font.render(textContent, True, WHITE)
 textRect = text.get_rect()
 textRect.center = (WIDTH / 2, HEIGHT / 2 - 60)
 pencere.blit(text, textRect)
 
 pygame.draw.rect(pencere, buttonColor, buttonRect)

 buttonText = font.render('I LOVE', True, buttonTextColor)
 buttonTextRect = buttonText.get_rect()
 buttonTextRect.center = buttonRect.center
 pencere.blit(buttonText, buttonTextRect)
 
 pygame.display.flip()

Yorum Gönder

0 Yorumlar