Answered step by step
Verified Expert Solution
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
SCRWID, SCRHEI
#Background
image pygame.image.loadsynthbgjpg
def Backgroundsynthimage:
size pygame.transform.scaleimage
screen.blitsize
class Player:
counter
def initself x num:
Player.counter
self.id Player.counter
self.num num
self.x self.y x SCRHEI
self.speed
self.padWid, self.padHei
self.score
self.score
print Playercounter
def movementself:
keys pygame.key.getpressed
if self.num :
if keyspygameKw:
self.y self.speed
elif keyspygameKs:
self.y self.speed
if self.y :
self.y
elif self.y SCRHEI:
self.y SCRHEI
if self.num :
if keyspygameKUP:
self.y self.speed
elif keyspygameKDOWN:
self.y self.speed
if self.y :
self.y
elif self.y SCRHEI:
self.y SCRHEI
def drawself:
pygame.draw.rectscreenselfx self.y self.padWid, self.padHei
class Ball:
def initself:
self.x self.y SCRWID SCRHEI
self.speedx
self.speedy
self.size
def movementself:
self.x self.speedx
self.y self.speedy
if self.y :
self.speedy
elif self.y SCRHEIself.size:
self.speedy
if self.x :
self.init
player.score
elif self.x SCRWIDself.size:
self.init
self.speedx
player.score
for n in rangeself.size, player.padHei:
if self.y player.y n:
if self.x player.x player.padWid:
self.speedx
break
n
for n in rangeself.size, enemy.padHei:
if self.y enemy.y n:
if self.x enemy.x enemy.padWid:
self.speedx
break
n
def drawself:
pygame.draw.rectscreenselfx self.y
SCRWID, SCRHEI
screen pygame.display.setmodeSCRWID, SCRHEI
pygame.display.setcaptionPong
pygame.font.init
clock pygame.time.Clock
FPS
player Player
ball Ball
enemy Player
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
Backgroundsynthload
screen.fill
ball.draw
player.draw
enemy.draw
pygame.display.flip
clock.tickFPS
main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started