Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Tips for adding an image before the game begins ( a title screen ) , which by pressing enter starts the game ( NOTE: I

Tips for adding an image before the game begins (a title screen), which by pressing enter starts the game
(NOTE: I have one background image enabled so I don't want to interrupt that, also this code is shrunk to fit the question.)
Code:
import pygame
SCR_WID, SCR_HEI =640,480
#Background
image = pygame.image.load('synthbg.jpg')
def Background_synth(image):
size = pygame.transform.scale(image,(640,480))
screen.blit(size,(0,0))
class Player():
_counter =0
def __init__(self, x, num):
Player._counter +=1
self.id = Player._counter
self.num = num
self.x, self.y = x, SCR_HEI/2
self.speed =3
self.padWid, self.padHei =8,64
self.score =0
self.score2=0
print (Player._counter)
def movement(self):
keys = pygame.key.get_pressed()
if self.num ==1:
if keys[pygame.K_w]:
self.y -= self.speed
elif keys[pygame.K_s]:
self.y += self.speed
if self.y <=0:
self.y =0
elif self.y >= SCR_HEI-64:
self.y = SCR_HEI-64
if self.num ==2:
if keys[pygame.K_UP]:
self.y -= self.speed
elif keys[pygame.K_DOWN]:
self.y += self.speed
if self.y <=0:
self.y =0
elif self.y >= SCR_HEI-64:
self.y = SCR_HEI-64
def draw(self):
pygame.draw.rect(screen,(255,255,255),(self.x, self.y, self.padWid, self.padHei))
class Ball():
def __init__(self):
self.x, self.y = SCR_WID/2, SCR_HEI/2
self.speed_x =-3
self.speed_y =3
self.size =8
def movement(self):
self.x += self.speed_x
self.y += self.speed_y
if self.y <=0:
self.speed_y *=-1
elif self.y >= SCR_HEI-self.size:
self.speed_y *=-1
if self.x <=0:
self.__init__()
player.score2+=1
elif self.x >= SCR_WID-self.size:
self.__init__()
self.speed_x =3
player.score +=1
for n in range(-self.size, player.padHei):
if self.y == player.y + n:
if self.x <= player.x + player.padWid:
self.speed_x *=-1
break
n +=1
for n in range(-self.size, enemy.padHei):
if self.y == enemy.y + n:
if self.x >= enemy.x - enemy.padWid:
self.speed_x *=-1
break
n +=1
def draw(self):
pygame.draw.rect(screen,(255,255,255),(self.x, self.y,8,8))
SCR_WID, SCR_HEI =640,480
screen = pygame.display.set_mode((SCR_WID, SCR_HEI))
pygame.display.set_caption("Pong")
pygame.font.init()
clock = pygame.time.Clock()
FPS =60
player = Player(16,1)
ball = Ball()
enemy = Player(624,2)
def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print ("Game exited by user")
exit()
ball.movement()
player.movement()
enemy.movement()
Background_synth(load)
screen.fill((0,0,0))
ball.draw()
player.draw()
enemy.draw()
pygame.display.flip()
clock.tick(FPS)
main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions