How to create a game menu for pong? - PYGAME 3.5

python

#1

I am trying to make a game menu that has a Start button, quit button and an information button. Please may you help me in making this code. I am new to pygame.

This is my pong code:

import pygame
from pygame.locals import *
from sys import exit
import random

pygame.init()



screen=pygame.display.set_mode((640,480),0,32)
pygame.display.set_caption("Pong Pong!")
#Creating 2 bars, a ball and background.
back = pygame.Surface((640,480))
background = back.convert()
background.fill((0,0,0))
bar = pygame.Surface((10,50))
bar1 = bar.convert()
bar1.fill((0,0,255))
bar2 = bar.convert()
bar2.fill((255,0,0))
circ_sur = pygame.Surface((15,15))
circ = pygame.draw.circle(circ_sur,(0,255,0),(7,7),7)
circle = circ_sur.convert()
circle.set_colorkey((255,255,255))

# some definitions
bar1_x, bar2_x = 10. , 620.
bar1_y, bar2_y = 215. , 215.
circle_x, circle_y = 307.5, 232.5
bar1_move, bar2_move = 0. , 0.
speed_x, speed_y, speed_circ = 250., 250., 250.
bar1_score, bar2_score = 0,0
#clock and font objects
clock = pygame.time.Clock()
font = pygame.font.SysFont("calibri",40)

while True:
    
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        if event.type == KEYDOWN:
            if event.key == K_UP:
                bar1_move = -ai_speed
            elif event.key == K_DOWN:
                bar1_move = ai_speed
        elif event.type == KEYUP:
            if event.key == K_UP:
                bar1_move = 0.
            elif event.key == K_DOWN:
                bar1_move = 0.
    
    score1 = font.render(str(bar1_score), True,(255,255,255))
    score2 = font.render(str(bar2_score), True,(255,255,255))

    screen.blit(background,(0,0))
    frame = pygame.draw.rect(screen,(255,255,255),Rect((5,5),(630,470)),2)
    middle_line = pygame.draw.aaline(screen,(255,255,255),(330,5),(330,475))
    screen.blit(bar1,(bar1_x,bar1_y))
    screen.blit(bar2,(bar2_x,bar2_y))
    screen.blit(circle,(circle_x,circle_y))
    screen.blit(score1,(250.,210.))
    screen.blit(score2,(380.,210.))

    bar1_y += bar1_move
    
# movement of circle
    time_passed = clock.tick(30)
    time_sec = time_passed / 1000.0
    
    circle_x += speed_x * time_sec
    circle_y += speed_y * time_sec
    ai_speed = speed_circ * time_sec
#AI of the computer.
    if circle_x >= 305.:
        if not bar2_y == circle_y + 7.5:
            if bar2_y < circle_y + 7.5:
                bar2_y += ai_speed
            if  bar2_y > circle_y - 42.5:
                bar2_y -= ai_speed
        else:
            bar2_y == circle_y + 7.5
    
    if bar1_y >= 420.: bar1_y = 420.
    elif bar1_y <= 10. : bar1_y = 10.
    if bar2_y >= 420.: bar2_y = 420.
    elif bar2_y <= 10.: bar2_y = 10.
#since i don't know anything about collision, ball hitting bars goes like this.
    if circle_x <= bar1_x + 10.:
        if circle_y >= bar1_y - 7.5 and circle_y <= bar1_y + 42.5:
            circle_x = 20.
            speed_x = -speed_x
    if circle_x >= bar2_x - 15.:
        if circle_y >= bar2_y - 7.5 and circle_y <= bar2_y + 42.5:
            circle_x = 605.
            speed_x = -speed_x
    if circle_x < 5.:
        bar2_score += 1
        circle_x, circle_y = 320., 232.5
        bar1_y,bar_2_y = 215., 215.
    elif circle_x > 620.:
        bar1_score += 1
        circle_x, circle_y = 307.5, 232.5
        bar1_y, bar2_y = 215., 215.
    if circle_y <= 10.:
        speed_y = -speed_y
        circle_y = 10.
    elif circle_y >= 457.5:
        speed_y = -speed_y
        circle_y = 457.5

    pygame.display.update()

#2

You can use the python module tkinter which has a buttons feature however you would need to link that button so that in the event that a button is clicked some code runs. :slight_smile:


#3

what? can u show me the code please and guide me through it. Thanks!


#4

Doing it for you would beat the purpose of you learning how to do it. But I can give you good reference resources :smile:,


#5

Currently I'm starting to make a game from scratch. I do not have an idea so could you tell me some good ideas that are like pong or battleship. Also I'm going to make my code neater so teacher will be impressed. Please may you give me the code to a starting menu that has a quit button which quits the game and a start game button which directs you to the game. Thanks, I know this is a lot to ask for but I need help :tired_face:


#6

It's not acceptable to repeatedly ask for code until you have enough pieces to paste them together into an application that you then call yours.

Go do some research, learn the things you need to write that code yourself.

https://www.google.com/search?q=creating+a+menu+in+pygame

Go ahead and ask questions when you're stuck (and motivate why you're unable to continue/explain what you need), but this is more of "I haven't tried, please show me how and give me code"


#7

I am not doing that, I want to know how to make a menu but I still don't understand. The rest of the game will be fully-coded by myself. I'm sorry if I sound petty or desperate but I'm stumped on the menu part. I have decided to make a game called Snake, you are a snake who eats randomly plotted food and you never stop but loose if you hit the side walls (yes like nibbles the old game)


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.