-
May 11th, 2022, 05:21 AM
#1
[RESOLVED] Python game
Hi everybody,
Starting to self learn Python by writing a game but I need help with error checking since the code I had only gives be a black form now.
My code
Code:
import pygame, sys
# initializing the constructor
pygame.init()
# screen resolution
res = (720, 720)
# opens up a window
screen = pygame.display.set_mode(res)
# white color
color = (255, 255, 255)
# light shade of the button
color_light = (170, 170, 170)
# dark shade of the button
color_dark = (100, 100, 100)
# stores the width of the
# screen into a variable
width = screen.get_width()
# stores the height of the
# screen into a variable
height = screen.get_height()
# defining a font
smallfont = pygame.font.SysFont('Corbel', 35)
# rendering a text written in
# this font
text = smallfont.render('quit', True, color)
# stores the (x,y) coordinates into
# the variable as a tuple
mouse = pygame.mouse.get_pos()
# fills the screen with a color
screen.fill((60, 25, 60))
while True:
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
pygame.quit()
# checks if a mouse is clicked
if ev.type == pygame.MOUSEBUTTONDOWN:
# if the mouse is clicked on the
# button the game is terminated
if width / 2 <= mouse[0] <= width / 2 + 140 and height / 1.25 <= mouse[1] <= height / 1.25 + 40:
pygame.quit()
sys.exit()
# if mouse is hovered on a button it
# changes to lighter shade
#Quit Button
if width / 2 <= mouse[0] <= width / 2 + 140 and height / 1.25 <= mouse[1] <= height/1.25 + 40:
pygame.draw.rect(screen, color_light, [width /2, height/1.25, 100, 40])
else:
pygame.draw.rect(screen, color_dark, [width / 2, height/1.25 , 100, 40])
#New Button
if width / 2 <= mouse[0] <= width / 2 + 140 and height /8 <= mouse[1] <= height/8 + 40:
pygame.draw.rect(screen, color_light, [width / 2, height / 8 , 100, 40])
else:
pygame.draw.rect(screen, color_dark, [width / 2, height / 8, 100, 40])
# Settings Button
if width / 2 <= mouse[0] <= width / 2 + 140 and height /4 <= mouse[1] <= height /4+ 40:
pygame.draw.rect(screen, color_light, [width / 2, height / 4, 100, 40])
else:
pygame.draw.rect(screen, color_dark, [width / 2, height / 4, 100, 40])
#About Button
if width / 2 <= mouse[0] <= width / 2 + 140 and height / 2 <= mouse[1] <= height / 2 + 40:
pygame.draw.rect(screen, color_light, [width / 2, height / 2, 100, 40])
else:
pygame.draw.rect(screen, color_dark, [width / 2, height / 2, 100, 40])
# superimposing the text onto our buttons
screen.blit(quitText, (width / 2, height / 1.25))
screen.blit(newText, (width / 2, height / 8))
screen.blit(aboutText, (width / 2, height / 2))
screen.blit(settingsText, (width / 2, height / 4))
# updates the frames of the game
pygame.display.update()
original code:
https://www.geeksforgeeks.org/creati...enu-in-pygame/
Can someone see that is wrong with my code?
Thanks,
Nightwalker
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
May 11th, 2022, 07:12 AM
#2
Re: Python game
Nevermind I manage to fix it.
Code:
import pygame, sys
# initializing the constructor
pygame.init()
# screen resolution
res = (360, 360)
# opens up a window
screen = pygame.display.set_mode(res)
# white color
color = (255, 255, 255)
# light shade of the button
color_light = (170, 170, 170)
# dark shade of the button
color_dark = (100, 100, 100)
# stores the width of the
# screen into a variable
width = screen.get_width()
# stores the height of the
# screen into a variable
height = screen.get_height()
# defining a font
smallfont = pygame.font.SysFont('Corbel', 35)
# rendering a text written in
# this font
aboutText = smallfont.render('About', True, color)
newText = smallfont.render('New', True, color)
settingsText = smallfont.render('Settings', True, color)
quitText = smallfont.render('Quit', True, color)
while True:
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
pygame.quit()
# checks if a mouse is clicked
if ev.type == pygame.MOUSEBUTTONDOWN:
# if the mouse is clicked on the
# button the game is terminated
if width / 2 <= mouse[0] <= width / 2 + 140 and height / 2 <= mouse[1] <= height / 2 + 40:
pygame.quit()
sys.exit
# fills the screen with a color
screen.fill((60, 25, 60))
# stores the (x,y) coordinates into
# the variable as a tuple
mouse = pygame.mouse.get_pos()
# if mouse is hovered on a button it
# changes to lighter shade
# if mouse is hovered on a button it
# changes to lighter shade
# Quit Button
if width / 2 <= mouse[0] <= width / 2 + 140 and height / 1.25 <= mouse[1] <= height / 1.25 + 40:
pygame.draw.rect(screen, color_light, [width / 2, height / 1.25, 100, 40])
else:
pygame.draw.rect(screen, color_dark, [width / 2, height / 1.25, 100, 40])
# New Button
if width / 2 <= mouse[0] <= width / 2 + 140 and height / 8 <= mouse[1] <= height / 8 + 40:
pygame.draw.rect(screen, color_light, [width / 2, height / 8, 100, 40])
else:
pygame.draw.rect(screen, color_dark, [width / 2, height / 8, 100, 40])
# Settings Button
if width / 2 <= mouse[0] <= width / 2 + 140 and height / 4 <= mouse[1] <= height / 4 + 40:
pygame.draw.rect(screen, color_light, [width / 2, height / 4, 100, 40])
else:
pygame.draw.rect(screen, color_dark, [width / 2, height / 4, 100, 40])
# About Button
if width / 2 <= mouse[0] <= width / 2 + 140 and height / 2 <= mouse[1] <= height / 2 + 40:
pygame.draw.rect(screen, color_light, [width / 2, height / 2, 100, 40])
else:
pygame.draw.rect(screen, color_dark, [width / 2, height / 2, 100, 40])
# superimposing the text onto our buttons
screen.blit(quitText, (width / 2, height / 1.25))
screen.blit(newText, (width / 2, height / 8))
screen.blit(aboutText, (width / 2, height / 2))
screen.blit(settingsText, (width / 2, height / 4))
# updates the frames of the game
pygame.display.update()
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|