Hey!
It’s me again, yes I’ve had a lot of help with my past work ect. and I’ve tried to go solo on this project but I need help! I have been making a pong game and a gamemenu for it. (Go to bottom for all the files, Im using dropbox)
1) Now firstly let’s start on the game menu. I’ve been taught by my teacher and siblings to use functions to make buttons. Ive made a start_Button
and a about_Button
. Here is the function code;
def start_Button():
button = fontLargeTitle.render_to(screen,(25,39), "Start", BLUE2)
pygame.display.flip()
## loop to check for mouse action and its position ##
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
## if mouse is pressed get position of cursor ##
pos = pygame.mouse.get_pos()
## check if cursor is on button ##
if button.collidepoint(pos):
fade()
import pong
return
def about_Button():
button2 = fontSmallTitle.render_to(screen,(25,150), "About", WHITE)
pygame.display.flip()
## loop to check for mouse action and its position ##
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
## if mouse is pressed get position of cursor ##
pos = pygame.mouse.get_pos()
## check if cursor is on button ##
if button2.collidepoint(pos):
quit()
return
Im using freetypes by the way (import pygame.freetype
). I don’t like using tk or any other add on.
When I try and call these two functions on my screen, it only shows one of the buttons (The first function called). You have to click around to make it appear then it works. Also the about button’s hitbox is all over the place. Please may you help me fix the hitbox (collision point thing) and make it so both buttons appear!
2) Lets get onto the second part, I’ve made a pong game and I’ve made it so when you loose, it comes up with a system window (Using os & sys modules). When you click ‘OK’ or ‘CANCEL’ it should send you back to main.py, it does but says 'Video system not initialized'
and it says that sometimes when replaying pong etc.
Please may you see why it is not working and help me? These are two big problems in my way of making a good game. I have googled for many hours and even asked my siblings for help but to my avail, I have had no success. Hopefully the people of codecacademy forums will help me out. Here is my pong code:
import pygame, random, ctypes, os
from pygame.locals import *
import pygame.freetype
def main():
pygame.init()
fontLargeTitle = pygame.freetype.Font("earthorbiter.ttf", 40)
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,(192,192,192),(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., 400
bar1_score, bar2_score = 0,0
#clock and font objects
clock = pygame.time.Clock()
font = pygame.font.Font("earthorbiter.ttf",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.
# Circle hits bar
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
#Winning thing]
if bar1_score > 3 and bar2_score < bar1_score:
ctypes.windll.user32.MessageBoxW(0, "Congratulations!", "YOU WON!", 1)
pygame.quit()
os.system('main.py')
elif bar2_score > 3 and bar1_score < bar2_score:
ctypes.windll.user32.MessageBoxW(0, "Too bad! Better luck next time.", "YOU LOST!", 1)
pygame.quit()
os.system('main.py')
pygame.display.update()
main()
Here is my dropbox link for all needing all the files;
Thanks!
- CyanDeathReaper