Single player 'pong' game - python

I am just starting out learning pygame and livewires, and I'm trying to make a single-player pong game, where you just hit the ball, and it bounces around until it passes your paddle (located on the left side of the screen and controlled by the mouse), which makes you lose. I have the basic code, but the ball doesn't stay on the screen, it just flickers and doesn't remain constant. Also, the paddle does not move with the mouse. I'm sure I'm missing something simple, but I just can't figure it out. Help please! Here's what I have:
from livewires import games
import random
games.init(screen_width=640, screen_height=480, fps=50)
class Paddle(games.Sprite):
image=games.load_image("paddle.bmp")
def __init__(self, x=10):
super(Paddle, self).__init__(image=Paddle.image,
y=games.mouse.y,
left=10)
self.score=games.Text(value=0, size=25, top=5, right=games.screen.width - 10)
games.screen.add(self.score)
def update(self):
self.y=games.mouse.y
if self.top<0:
self.top=0
if self.bottom>games.screen.height:
self.bottom=games.screen.height
self.check_collide()
def check_collide(self):
for ball in self.overlapping_sprites:
self.score.value+=1
ball.handle_collide()
class Ball(games.Sprite):
image=games.load_image("ball.bmp")
speed=5
def __init__(self, x=90, y=90):
super(Ball, self).__init__(image=Ball.image,
x=x, y=y,
dx=Ball.speed, dy=Ball.speed)
def update(self):
if self.right>games.screen.width:
self.dx=-self.dx
if self.bottom>games.screen.height or self.top<0:
self.dy=-self.dy
if self.left<0:
self.end_game()
self.destroy()
def handle_collide(self):
self.dx=-self.dx
def end_game(self):
end_message=games.Message(value="Game Over",
size=90,
x=games.screen.width/2,
y=games.screen.height/2,
lifetime=250,
after_death=games.screen.quit)
games.screen.add(end_message)
def main():
background_image=games.load_image("background.bmp", transparent=False)
games.screen.background=background_image
paddle_image=games.load_image("paddle.bmp")
the_paddle=games.Sprite(image=paddle_image,
x=10,
y=games.mouse.y)
games.screen.add(the_paddle)
ball_image=games.load_image("ball.bmp")
the_ball=games.Sprite(image=ball_image,
x=630,
y=200,
dx=2,
dy=2)
games.screen.add(the_ball)
games.mouse.is_visible=False
games.screen.event_grab=True
games.screen.mainloop()
main()

I can't help you because you did not post the complete code here. At least, I do not see where you're updating the positions of the sprites (self.x += self.dx somewhere?) and updating the draw to screen. You're also not utilising your classes in the main() function.
That said, I'm seeing
def __init__(self, x=10):
and inside the constructor you never used the x variable. That worries me, too.
Consider using the Paddle and Ball class as a Sprite, like the following:
if __name__ == '__main__':
background_image = games.load_image("background.bmp", transparent=False)
games.screen.background = background_image
the_paddle = Puddle()
games.screen.add(the_paddle)
the_ball = Ball()
games.screen.add(the_ball)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
Note I've taken the liberty to make your code read more Pythonic. I have never used livewires, however, so my code may not function. But it should point you to the right direction. Good luck!

Why are you using livewires? You can use only pygame for a pong game.
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480)) # window size
pygame.display.set_caption("Simple pong") # window title
# this is a rect that contains the ball
# at the beginning it is set in the center of the screen
ball_rect = pygame.Rect((312, 232), (16, 16))
# speed of the ball (x, y)
ball_speed = [4, 4]
# this contains your paddle
# vertically centered on the left side
paddle_rect = pygame.Rect((8, 200), (8, 80))
# 1 point if you hit the ball
# -5 point if you miss the ball
score = 0
# load the font for displaying the score
font = pygame.font.Font(None, 30)
# mainloop
while True:
# event handler
for event in pygame.event.get():
# quit event => close the game
if event.type == pygame.QUIT:
exit(0)
# control the paddle with the mouse
elif event.type == pygame.MOUSEMOTION:
paddle_rect.centery = event.pos[1]
# correct paddle position if it's going out of window
if paddle_rect.top < 0:
paddle_rect.top = 0
elif paddle_rect.bottom >= 480:
paddle_rect.bottom = 480
# this test if up or down keys are pressed
# if yes move the paddle
if pygame.key.get_pressed()[pygame.K_UP] and paddle_rect.top > 0:
paddle_rect.top -= 5
elif pygame.key.get_pressed()[pygame.K_DOWN] and paddle_rect.bottom < 480:
paddle_rect.top += 5
# update ball position
# this move the ball
ball_rect.left += ball_speed[0]
ball_rect.top += ball_speed[1]
# these two if block control if the ball is going out on the screen
# if it's going it reverse speed to simulate a bounce
if ball_rect.top <= 0 or ball_rect.bottom >= 480:
ball_speed[1] = -ball_speed[1]
if ball_rect.right >= 640:
ball_speed[0] = -ball_speed[0]
# this control if the ball touched the left side
elif ball_rect.left <= 0:
score -= 5
# reset the ball to the center
ball_rect = pygame.Rect((312, 232), (16, 16))
# test if the ball is hit by the paddle
# if yes reverse speed and add a point
if paddle_rect.colliderect(ball_rect):
ball_speed[0] = -ball_speed[0]
score += 1
# clear screen
screen.fill((255, 255, 255))
# draw the ball, the paddle and the score
pygame.draw.rect(screen, (0, 0, 0), paddle_rect) # paddle
pygame.draw.circle(screen, (0, 0, 0), ball_rect.center, ball_rect.width/2) # ball
score_text = font.render(str(score), True, (0, 0, 0))
screen.blit(score_text, (320-font.size(str(score))[0]/2, 5)) # score
# update screen and wait 20 milliseconds
pygame.display.flip()
pygame.time.delay(20)

Related

How do I restart my python game, or just reset where the player is?

I want to restart where my player is when the player touches a wall, but before I do that, I want to give them a warning then reset position. I can't do either, though. I recently asked a similar question, and although I got a very good and helpful answer, it wouldn't work. Thank you anyway, Rabbid76. This is my code:
def touch_walls(player_pos):
if x-20 < 0:
#give warning not to touch sides
#reset player position
elif x+20 > 800:
#give warning not to touch sides
#reset player position
If anyone finds a solution, please tell me.
You can call a function to draw a "Don't touch walls" text.
`
def touch_walls(player_pos):
global playerPos, blit_font_time
x = player_pos[0]
if x-20 < 0 or x+20 > 800:
#make a text varible
#reset player position
playerPos = (50, 0)
blit_font_time = 60 # set this to as long as you wish
def blit_font():
text = pygame.font.SysFont('Arial', 50)
warning_label = text.render('Don\'t Touch Walls', True, (255, 255, 255)) #You can modify the text or the color anyway you want
center = (screen.get_width()/2 - warning_label.get_width()/2, screen.get_height()/2 - warning_label.get_height()/2)
screen.blit(warning_label, center) #Blit the text at the center of the screen
`
The whole thing will look like this:
`
import pygame
pygame.init()
playerPos = (-60, 50) # Assume playerPos is a varible that contains the player's position
screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
blit_font_time = 0
def touch_walls(player_pos):
global playerPos, blit_font_time
x = player_pos[0]
if x-20 < 0 or x+20 > 800:
#make a text varible
#reset player position
playerPos = (50, 0)
blit_font_time = 60 # set this to as long as you wish
def blit_font():
text = pygame.font.SysFont('Arial', 50)
warning_label = text.render('Don\'t Touch Walls', True, (255, 255, 255)) #You can modify the text or the color anyway you want
center = (screen.get_width()/2 - warning_label.get_width()/2, screen.get_height()/2 - warning_label.get_height()/2)
screen.blit(warning_label, center) #Blit the text at the center of the screen
run = True
while run:
clock.tick(60)
for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False
screen.fill((20, 20, 20))
touch_walls(playerPos)
if blit_font_time > 0:
blit_font_time -= 1
blit_font()
pygame.display.update()
pygame.quit()
`

How to blit a dialogue box in pygame until the player is in a specified vicinity

I'm creating a game in python and want a dialogue box (png file) to be blitted on the screen as long as the player is in a defined vicinity that I set using the rect function. if I load the sprite and try to blit it directly it doesn't show up for some reason, can anyone help with this? ive attached concerned parts of the code below and the entire code is here on my Github:
dialoguebox = pygame.image.load('dialogue box.png')
npc1 = pygame.Rect(192, 226, 26, 32)
player_rect = playerImg.get_rect(topleft=(playerX, playerY))
if player_rect.colliderect(npc1):
screen.blit(dialoguebox, (0, 0))
if playerX_change > 0:
player_rect.right = npc1.left
elif playerX_change < 0:
player_rect.left = npc1.right
playerX = player_rect.x # NPC near patch
player_rect = playerImg.get_rect(topleft=(playerX, playerY))
if player_rect.colliderect(npc1):
screen.blit(dialoguebox, (0, 0))
if playerY_change < 0:
player_rect.top = npc1.bottom
elif playerY_change > 0:
player_rect.bottom = npc1.top
playerY = player_rect.y # NPC near patch 1
The box doesn't show up, because the screen is cleared and redrawn in redrawgamewindow().
Add a variable current_dialogue in global namespace:
current_dialogue = None
Set the variable in the main application loop. current_dialogue is assigned a tuple ((dialoguebox, (0, 0))) which stores the information about the dialog.
def game():
global current_dialogue
# [...]
while running:
current_dialogue = None
# [...]
if player_rect.colliderect(npc1):
current_dialogue = (dialoguebox, (0, 0))
# [...]
# [...]
redrawgamewindow()
# [...]
And draw the dialog at the end of redrawgamewindow(). Use The asterisk(*) operator to unpack the tuple (scr.blit(*current_dialogue)):
def redrawgamewindow():
# [...]
scr.fill((0, 0, 0))
# [...]
if current_dialogue:
scr.blit(*current_dialogue)
Note, the dialog will only show up, if the rectangles are intersecting. If you want the dialog to show up if the player is near the traget then you have to use an increased rectangle for the collision test. Use inflate() to generate a new rectangle with the changed size:
if player_rect.inflate(5, 5).colliderect(npc1):
current_dialogue = (dialoguebox, (0, 0))

PyGame Window not starting

I created this simple snake game a while ago and I wanted to try running it and for some reason the window is not starting on my machine. I am sure the code was working before. I tried debugging for a while but can't seem to figure out why I am stuck on a black screen. It seems to detect a key pressed but nothing is displayed on the screen.
import pygame
from pygame.locals import *
import random
# Global Color Variables
RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
CYAN = (0,255, 255)
# Set the speed of the Snake --> lower = faster
timeDelaySpeed = 0
class App:
def __init__(self):
self._running = True
self._display_surf = None
self.size = self.weight, self.height = 600, 700
# create the boarder
self.boarder = self.generateBoard()
# Initial Snake array with 3 Snake Blocks starting at (50, 50) and going left
self.snake = [Snake(WHITE, 10, 10, 150, 260), Snake(WHITE, 10, 10, 140, 260), Snake(WHITE, 10, 10, 130, 260)]
def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
self._display_surf.fill(BLACK)
self._running = True
# Create Score Board
self.score = 0
self.displayScore(self.score, 45)
# Create Initial Food
self.initFood = Food(RED, 10, 10)
self._display_surf.blit(self.initFood.image, self.initFood.rect)
# display the initial Snake array
for i in range(len(self.snake)):
self._display_surf.blit(self.snake[i].image, self.snake[i].rect)
# display the board
for i in range(len(self.boarder)):
self._display_surf.blit(self.boarder[i].image, self.boarder[i].rect)
pygame.display.update()
"""
Helper Method that will run the events that are clicked on by the user
"""
def on_event(self):
# Checks if Snake crashes with itself - LOSE
for i in range(1, len(self.snake)):
if pygame.sprite.collide_rect(self.snake[0], self.snake[1]):
self.spaceToRestartText(20)
self.gameRestart()
if pygame.sprite.collide_rect(self.snake[0], self.snake[i]):
self.spaceToRestartText(20)
self.gameRestart()
# Check if Snake hits the boarder - LOSE
for i in range(len(self.boarder)):
if pygame.sprite.collide_rect(self.snake[0], self.boarder[i]):
self.spaceToRestartText(20)
self.gameRestart()
# Checks if Snake eats Food
if pygame.sprite.collide_rect(self.snake[0], self.initFood):
self.eatFood()
# set the direction based of key that is pressed
for event in pygame.event.get():
if event.type == pygame.QUIT:
self._running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
# check for self collision before eating any food - weird collision method error fix
if self.move == 'right':
self.spaceToRestartText(20)
self.gameRestart()
break
self.move = 'left'
if event.key == pygame.K_RIGHT:
# check for self collision before eating any food
if self.move == 'left':
self.spaceToRestartText(20)
self.gameRestart()
break
self.move = 'right'
if event.key == pygame.K_UP:
# check for self collision before eating any food
if self.move == 'down':
self.spaceToRestartText(20)
self.gameRestart()
break
self.move = 'up'
if event.key == pygame.K_DOWN:
# check for self collision before eating any food
if self.move == 'up':
self.spaceToRestartText(20)
self.gameRestart()
break
self.move = 'down'
# if stored current direction is right
if self.move == 'right':
print("RIGHT")
# Reset the Board
self.boardReset()
# Store the current head of the snake
snakeHead = self.snake[0]
# remove the last block of the snake
self.snake.pop()
# create a new head for the snake that is shifted toward the right
newHead = snakeHead.moveRight()
# add the newly created head to the front of the list - make head
self.snake.insert(0, newHead)
# displays moved snake
for i in range(len(self.snake)):
self._display_surf.blit(self.snake[i].image, self.snake[i].rect)
pygame.display.update()
pygame.time.delay(timeDelaySpeed)
# if stored current direction is left
if self.move == 'left':
print("LEFT")
# Reset the Board
self.boardReset()
# Store the current head of the snake
snakeHead = self.snake[0]
# remove the last block of the snake
self.snake.pop()
# create a new head for the snake that is shifted toward the right
newHead = snakeHead.moveLeft()
# add the newly created head to the front of the list - make head
self.snake.insert(0, newHead)
# displays moved snake
for i in range(len(self.snake)):
self._display_surf.blit(self.snake[i].image, self.snake[i].rect)
pygame.display.update()
pygame.time.delay(timeDelaySpeed)
# if stored current direction is up
if self.move == 'up':
print("UP")
# Reset the Board
self.boardReset()
# Store the current head of the snake
snakeHead = self.snake[0]
# remove the last block of the snake
self.snake.pop()
# create a new head for the snake that is shifted toward the right
newHead = snakeHead.moveUp()
# add the newly created head to the front of the list - make head
self.snake.insert(0, newHead)
# displays moved snake
for i in range(len(self.snake)):
self._display_surf.blit(self.snake[i].image, self.snake[i].rect)
pygame.display.update()
pygame.time.delay(timeDelaySpeed)
# if stored current direction is down
if self.move == 'down':
print("DOWN")
# Reset the Board
self.boardReset()
# Store the current head of the snake
snakeHead = self.snake[0]
# remove the last block of the snake
self.snake.pop()
# create a new head for the snake that is shifted toward the right
newHead = snakeHead.moveDown()
# add the newly created head to the front of the list - make head
self.snake.insert(0, newHead)
# displays moved snake
for i in range(len(self.snake)):
self._display_surf.blit(self.snake[i].image, self.snake[i].rect)
pygame.display.update()
pygame.time.delay(timeDelaySpeed)
"""
Helper method that displays the current score on the screen.
"""
def displayScore(self, score, size):
font = pygame.font.SysFont("Comic Sans MS", size)
ScoreBoard = font.render("SCORE: {}".format(score), False, (WHITE))
self._display_surf.blit(ScoreBoard, [90, 100])
pygame.display.update()
"""
Helper method that will reset the screen:
Make screen Black
Add the current Food block
Add the current Score
"""
def boardReset(self):
# Erases the current screen
self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
self._display_surf.fill(BLACK)
# Create Score Board
self.displayScore(self.score, 45)
# Add Food
self._display_surf.blit(self.initFood.image, self.initFood.rect)
# Add Boarder
for i in range(len(self.boarder)):
self._display_surf.blit(self.boarder[i].image, self.boarder[i].rect)
"""
Eating food helper method
"""
def eatFood(self):
# Create a new Food at random location and display it
self.initFood = Food(RED, 10, 10)
self._display_surf.blit(self.initFood.image, self.initFood.rect)
# Create Score Board
self.score += 1
self.displayScore(self.score, 45)
# for i in range(len(self.snake)):
# self._display_surf.blit(self.snake[i].image, self.snake[i].rect)
#
# Store the last and second to last blocks of the snake
lastSnakeBlock = self.snake[-1]
secondToLastBlock = self.snake[-2]
# if the last two blocks are on the same horizontal line and the last block is to the left of the
# second to last block, add a block to the left side of the last block
if lastSnakeBlock.rect.y == secondToLastBlock.rect.y and lastSnakeBlock.rect.x < secondToLastBlock.rect.x:
newX = lastSnakeBlock.rect.x - 10
newSnakeBlock = Snake(lastSnakeBlock.color, lastSnakeBlock.width, lastSnakeBlock.height, newX,
lastSnakeBlock.rect.y)
self.snake.append(newSnakeBlock)
# if the last two blocks are on the same horizontal line and the last block is to the right of the
# second to last block, add a block to the right side of the last block
if lastSnakeBlock.rect.y == secondToLastBlock.rect.y and lastSnakeBlock.rect.x > secondToLastBlock.rect.x:
newX = lastSnakeBlock.rect.x + 10
newSnakeBlock = Snake(lastSnakeBlock.color, lastSnakeBlock.width, lastSnakeBlock.height, newX,
lastSnakeBlock.rect.y)
self.snake.append(newSnakeBlock)
# if the last two blocks are on the same vertical line and the last block is above the
# second to last block, add a block above the last block
if lastSnakeBlock.rect.x == secondToLastBlock.rect.x and lastSnakeBlock.rect.y < secondToLastBlock.rect.y:
newY = lastSnakeBlock.rect.y - 10
newSnakeBlock = Snake(lastSnakeBlock.color, lastSnakeBlock.width, lastSnakeBlock.height,
lastSnakeBlock.rect.x, newY)
self.snake.append(newSnakeBlock)
# if the last two blocks are on the same vertical line and the last block is below the
# second to last block, add a block below the last block
if lastSnakeBlock.rect.x == secondToLastBlock.rect.x and lastSnakeBlock.rect.y > secondToLastBlock.rect.y:
newY = lastSnakeBlock.rect.y + 10
newSnakeBlock = Snake(lastSnakeBlock.color, lastSnakeBlock.width, lastSnakeBlock.height,
lastSnakeBlock.rect.x, newY)
self.snake.append(newSnakeBlock)
for i in range(len(self.snake)):
self._display_surf.blit(self.snake[i].image, self.snake[i].rect)
"""
Takes the player back to initial start state
"""
def gameRestart(self):
# Erase the Board
self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
self._display_surf.fill(BLACK)
self._running = True
# Recreate the Snake
self.snake = [Snake(WHITE, 10, 10, 150, 260), Snake(WHITE, 10, 10, 140, 260), Snake(WHITE, 10, 10, 130, 260)]
# Create Score Board
self.score = 0
self.displayScore(self.score, 45)
# Create Initial Food
self.initFood = Food(RED, 10, 10)
self._display_surf.blit(self.initFood.image, self.initFood.rect)
# set current move to nothing
self.move = ''
# draw in the boarder
for i in range(len(self.boarder)):
self._display_surf.blit(self.boarder[i].image, self.boarder[i].rect)
# display the initial Snake array
for i in range(len(self.snake)):
self._display_surf.blit(self.snake[i].image, self.snake[i].rect)
pygame.display.update()
"""
Creates a List of Blocks that outline the Boarder of the snake game
"""
def generateBoard(self):
boardCorners = []
boardTop = []
boardSide1 = []
boardSide2 = []
boardBottom = []
# Makes (0,0) of board = (100, 210)
# top left corner
boardCorners.append(Snake(CYAN, 10, 10, 90, 200))
# top right corner
boardCorners.append(Snake(CYAN, 10, 10, 500, 200))
# bottom left corner
boardCorners.append(Snake(CYAN, 10, 10, 90, 610))
# bottom right corner
boardCorners.append(Snake(CYAN, 10, 10, 500, 610))
# top and bottom sides
topCoord = 100
for i in range(40):
boardTop.append(Snake(CYAN, 10, 10, topCoord, 200))
boardBottom.append(Snake(CYAN, 10, 10, topCoord, 610))
topCoord += 10
# sides of board
sideCoord = 210
for i in range(40):
boardSide1.append(Snake(CYAN, 10, 10, 90, sideCoord))
boardSide2.append(Snake(CYAN, 10, 10, 500, sideCoord))
sideCoord += 10
# combine all parts
allBoarder = boardCorners + boardTop + boardSide1 + boardSide2 + boardBottom
# return list of blocks
return allBoarder
"""
Allows player to restart a game by pressing space bar - displays losing screen
"""
def spaceToRestartText(self, size):
self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
self._display_surf.fill(BLACK)
self.youLoseText(50)
self.yourScoreText(25)
font = pygame.font.SysFont("Comic Sans MS", size)
text_surface = font.render("Press space bar to play again", True, WHITE)
text_rect = text_surface.get_rect(center=(self.weight / 2, self.height / 2))
self._display_surf.blit(text_surface, text_rect)
pygame.display.flip()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
done = True
"""
Helper function that prints 'YOU LOSE!'
"""
def youLoseText(self, size):
font = pygame.font.SysFont("Comic Sans MS", size)
text_surface = font.render("YOU LOSE!", True, WHITE)
# Shift height up so no collision with space bar text
text_rect = text_surface.get_rect(center=(self.weight / 2, (self.height / 2) - 75))
self._display_surf.blit(text_surface, text_rect)
pygame.display.flip()
"""
Helper function that prints your score at loss
"""
def yourScoreText(self, size):
font = pygame.font.SysFont("Comic Sans MS", size)
text_surface = font.render("Your Score was: " + str(self.score), True, WHITE)
# Shift height up so no collision with space bar text
text_rect = text_surface.get_rect(center=(self.weight / 2, (self.height / 2) - 35))
self._display_surf.blit(text_surface, text_rect)
pygame.display.flip()
def on_loop(self):
pass
def on_render(self):
pass
def on_cleanup(self):
pygame.quit()
"""
Game Loop
"""
def on_execute(self):
if self.on_init() == False:
self._running = False
self.move = ''
while (self._running):
self.on_event()
self.on_loop()
self.on_render()
self.on_cleanup()
"""
Class to create a Food at a random coordinate
"""
class Food(pygame.sprite.Sprite):
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
# set the position of the Food
# TODO: change values in randint to use the input width and height
randX = random.randint(10,49) * 10
randY = random.randint(21,60) * 10
self.rect.x = randX
self.rect.y = randY
class Snake(pygame.sprite.Sprite):
# TODO: Make Snake a chain of Blocks
def __init__(self, color, width, height, positionX, positionY):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
# set the position of the snake
self.rect.x = positionX
self.rect.y = positionY
# set the inputs the usable variables for later code
self.color = color
self.width = width
self.height = height
"""
Method the will change the direction of the Snake towards the left
"""
def moveLeft(self):
newX = self.rect.x - 10
return Snake(self.color, self.width, self.height, newX, self.rect.y)
"""
Method that will change the direction of the Snake toward the right
"""
def moveRight(self):
newX = self.rect.x + 10
return Snake(self.color, self.width, self.height, newX, self.rect.y)
"""
Method that will change the direction of the Snake to go upward
"""
def moveUp(self):
newY = self.rect.y - 10
return Snake(self.color, self.width, self.height, self.rect.x, newY)
"""
Method that will change the direction of the Snake to go downward
"""
def moveDown(self):
newY = self.rect.y + 10
return Snake(self.color, self.width, self.height, self.rect.x, newY)
if __name__ == "__main__":
theApp = App()
theApp.on_execute()
I can agree with the answers above. The only problem appears to be that you did not set the FPS i.e. clock.tick(fps) and the game is too fast so it is unplayable. 30 and under will do in my opinion. Please provide more details.
Code is okay, worked on my machine.
Can u add a screenshot of your command prompt?

Implementing a collision detect feature between a Rect object and a ball in pygame [duplicate]

This question already has an answer here:
Sometimes the ball doesn't bounce off the paddle in pong game
(1 answer)
Closed 2 years ago.
import pygame, random, time
# main function where we call all other functions, start the game loop, quit pygame and clean up the window. Inside we create a game object, display surface, and start the game loop by calling the play method on the game object. There is also a set caption with the title of the game.
def main():
pygame.init()
size =(500,400)
surface=pygame.display.set_mode(size)
pygame.display.set_caption('Pong v2')
game = Game(surface)
game.play()
pygame.quit()
# This is where we define the class game
class Game():
# an object in this class represents a complete game
# here we initialize a game. self is the game that is initialized surface is the display window surface object we also set default values for continuing the game and closing the window. we also define what fps we are running the game at, and define the velocity color position and radius of the ball
def __init__(self,surface):
# defining surface, fps, background color
self.surface=surface
self.FPS=120
self.bg_color=pygame.Color('black')
screen_width = surface.get_width()
screen_height = surface.get_height()
# defining ball attributes
ball_radius=10
ball_pos = [random.randint(ball_radius, screen_width-ball_radius),
random.randint(ball_radius, screen_height-ball_radius)]
ball_color=pygame.Color('white')
ball_velocity=[2,1]
self.ball=Ball(ball_pos,ball_radius,ball_color,ball_velocity,surface)
# defining paddle attributes
rect_left=[50,450]
rect_top=225
rect_height=60
rect_width=10
self.Paddle1=Rect(rect_left[0],rect_top,rect_width,rect_height,surface)
self.Paddle2=Rect(rect_left[1],rect_top,rect_width,rect_height,surface)
self.game_Clock=pygame.time.Clock()
self.close_clicked=False
self.continue_game=True
self.score1=0
self.score2=0
self.frame_counter=0
def play(self):
# game is played until player clicks close
while not self.close_clicked:
self.handle_events()
self.draw()
# if nothing sets this to false the game will continue to update
if self.continue_game:
self.update()
self.game_Clock.tick(self.FPS)
# score is drawn onto the screen (unimportant this is just playing with a feature for the next version), we define color font background etc of the score message and update score upon points being scored
def draw_score(self):
font_color = pygame.Color("white")
font_bg = pygame.Color("black")
font = pygame.font.SysFont("arial", 18)
text_img = font.render("Score for Player 1: " + str(self.score1) + ' Score for Player 2: ' + str(self.score2), True, font_color, font_bg)
text_pos = (0,0)
self.surface.blit(text_img, text_pos)
# ball, surface, score, and two paddles are drawn, pygame also updates this drawing once per frame
def draw(self):
self.surface.fill(self.bg_color)
self.draw_score()
#pygame.draw.rect(self.surface,pygame.Color('blue'),(50,225,10,50))
#pygame.draw.rect(self.surface,pygame.Color('red'),(450,225,10,50))
self.Paddle1.draw()
self.Paddle2.draw()
self.ball.draw()
pygame.display.update()
# score value set to default of 0 we tell ball to move and add 1 to frame counter upon each update. update game object for the next frame
def update(self):
self.ball.move()
self.score=0
self.frame_counter+=self.frame_counter+1
# here we setup an event loop and figure out if the action to end the game has been done
def handle_events(self):
events=pygame.event.get()
for event in events:
if event.type== pygame.QUIT:
self.close_clicked=True
# user defined class ball
class Ball:
# self is the ball to intialize. color/center/radius are defined for the ball that is initialized
def __init__(self,center,radius,color,velocity,surface):
self.center=center
self.radius=radius
self.color=color
self.velocity=velocity
self.surface=surface
# screen size is determined and edge of ball is checked that it is not touching the edge. if it is touching the edge it bounces and reverses velocity
def move(self):
screen_width=self.surface.get_width()
screen_height=self.surface.get_height()
screen_size=(screen_width,screen_height)
for i in range(0,len(self.center)):
self.center[i]+=self.velocity[i]
if (self.center[i]<=0 + self.radius or self.center[i]>=screen_size[i] - self.radius):
self.velocity[i]=-self.velocity[i]
# ball is drawn
def draw(self):
pygame.draw.circle(self.surface,self.color,self.center,self.radius)
class Rect:
def __init__(self,left,top,width,height,surface):
#self.left=left
#self.top=top
#self.width=width
#self.height=height
self.surface=surface
self.rect=pygame.Rect(left,top,width,height)
def draw(self):
pygame.draw.rect(self.surface,pygame.Color('red'),self.rect)
def collide(self):
if pygame.Rect.collide(x,y) == True:
return True
else:
return False
main()
Above is my work so far basically its supposed to be the retro arcade game pong where the ball bounces off of the edges of the paddles and if it doesn't the opposite side scores a point upon it hitting the edge of the window. So specifically this part of the project requires me to make the ball bounce off of the front of the paddles and I'm confused as to how to do this. My idea originally was to use the collidepoint method inside of the class Rect that if it returns true would reverse the balls velocity. However, I don't have access to the centre coordinates of the ball inside of the class or inside of the method play in the class game where I intended to make this work on the specific instances of ball and paddle1,paddle2 so I don't know how to do this.
Evaluate if the ball hits the left paddle at the right, respectively the right paddle at the left in Game.update.
If the ball hits the paddle the the score can be incremented:
class Game():
# [...]
def update(self):
self.ball.move()
# evaluate if Ball hits the left paddle (Paddle1) at the right
if self.ball.velocity[0] < 0 and self.Paddle1.rect.top <= self.ball.center[1] <= self.Paddle1.rect.bottom:
if self.Paddle1.rect.right <= self.ball.center[0] <= self.Paddle1.rect.right + self.ball.radius:
self.ball.velocity[0] = -self.ball.velocity[0]
self.ball.center[0] = self.Paddle1.rect.right + self.ball.radius
self.score1 += 1
# evaluate if Ball hits the right paddle (Paddle2) at the left
if self.ball.velocity[0] > 0 and self.Paddle2.rect.top <= self.ball.center[1] <= self.Paddle2.rect.bottom:
if self.Paddle2.rect.left >= self.ball.center[0] >= self.Paddle2.rect.left - self.ball.radius:
self.ball.velocity[0] = -self.ball.velocity[0]
self.ball.center[0] = self.Paddle2.rect.left - self.ball.radius
self.score2 += 1
Get the state of the keys by pygame.key.get_pressed() and change the position of the paddles is Game.handle_events.
e.g. Move the left paddle by w / s and the right paddle by UP / DOWN:
class Game():
# [...]
def handle_events(self):
events=pygame.event.get()
for event in events:
if event.type== pygame.QUIT:
self.close_clicked=True
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.Paddle1.rect.top = max(0, self.Paddle1.rect.top - 3)
if keys[pygame.K_s]:
self.Paddle1.rect.bottom = min(self.surface.get_height(), self.Paddle1.rect.bottom + 3)
if keys[pygame.K_UP]:
self.Paddle2.rect.top = max(0, self.Paddle2.rect.top - 3)
if keys[pygame.K_DOWN]:
self.Paddle2.rect.bottom = min(self.surface.get_height(), self.Paddle2.rect.bottom + 3)

How to increase points when a rect collides with another rect and the left mouse button is clicked?

So I am making a game where the player has to click on a moving rect (a tennis ball) to get points. So far I have created the tennis ball and some other balls, and I have got them moving randomly round the screen and bouncing off the edges. I have also made a rect called target which moves with the mouse. I would like the game to add 10 points every time the player clicks on the tennis ball (with the target) and deduct a life if they click elsewhere on the screen. Any help would be greatly appreciated!
Thanks :)
import pygame
import sys
from random import *
from pygame.locals import *
#game
pygame.init()
running=True
screen_width=800
screen_height=600
screen=pygame.display.set_mode([screen_width,screen_height])
background_colour = (0,0,150)
screen.fill(background_colour)
clock=pygame.time.Clock()
game_sound='pumpup.wav'
pygame.mixer.init()
points=0
lives=3
#balls
x= randint(20,40)
y= randint(20,40)
ball=pygame.image.load ('ball_t.png')
ball_movement=[x,y]#amount ball moves each time (x,y)
ball_location=[100,100]#x,y co-ordinates for start position
ball_rect=ball.get_rect()#gets rectangle for ball
x2= randint(5,15)
y2= randint(5,15)
ball2_movement=[x2,y2]#amount ball moves each time (x,y)
ball2_location=[10,10]#x,y co-ordinates for start position
ball2=pygame.image.load ('ball_tt.png')
ball2_rect=ball2.get_rect()#gets rectangle for ball
x3= randint(10,40)
y3= randint(10,30)
ball3_movement=[x3,y3]#amount ball moves each time (x,y)
ball3_location=[10,100]#x,y co-ordinates for start position
ball3=pygame.image.load ('ball_b.png')
ball3_rect=ball3.get_rect()#gets rectangle for ball
x4= randint(10,30)
y4= randint(10,30)
ball4_movement=[x4,y4]#amount ball moves each time (x,y)
ball4_location=[200,100]#x,y co-ordinates for start position
ball4=pygame.image.load ('ball_bs.png')
ball4_rect=ball4.get_rect()#gets rectangle for ball
#target
target_location=[200,100]#x,y co-ordinates for start position
target=pygame.image.load ('Target.png')
target_rect=target.get_rect()#gets rectangle for ball
target_rect.center=target_location
#score
font=pygame.font.Font(None, 50)#default font
score="Score: {}".format(points)
score_text=font.render(score,1,(0,200,250))
text_pos=[10,10] #position on screen
#lives
font=pygame.font.Font(None, 50)#default font
livest="Lives: {}".format(lives)
lives_text=font.render(livest,1,(0,200,250))
textl_pos=[650,10]#position on screen
#show initial screen
screen.blit(ball, ball_rect)#draws ball on its rectangle
screen.blit(ball2, ball2_rect)
screen.blit(ball3, ball3_rect)
screen.blit(ball4, ball4_rect)
screen.blit(target, target_rect)
screen.blit(score_text, text_pos)
screen.blit(lives_text, textl_pos)
pygame.display.flip() #displays screen
while running: #event loop
clock.tick(50) #won't run at more than 60 frames per second
screen.fill([0,0,150])#clears screen
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
elif event.type==pygame.MOUSEMOTION:
target_rect.centerx=event.pos[0] #moves target with mouse
target_rect.centery=event.pos[1]
if event.type == pygame.MOUSEBUTTONDOWN and target_rect.colliderect(ball2_rect):
points=100
if ball_rect.left<0 or ball_rect.right>screen_width: #check whether ball is off screen (x)
ball_movement[0]=-ball_movement[0]#change direction on x axis
if ball_rect.top<0 or ball_rect.bottom>screen_height: #check whether ball is off screen (y)
ball_movement[1]=-ball_movement[1] #change direction on y axis
if ball2_rect.left<0 or ball2_rect.right>screen_width: #check whether ball is off screen (x)
ball2_movement[0]=-ball2_movement[0]#change direction on x axis
if ball2_rect.top<0 or ball2_rect.bottom>screen_height: #check whether ball is off screen (y)
ball2_movement[1]=-ball2_movement[1] #change direction on y axis
if ball3_rect.left<0 or ball3_rect.right>screen_width: #check whether ball is off screen (x)
ball3_movement[0]=-ball3_movement[0]
if ball3_rect.top<0 or ball3_rect.bottom>screen_height: #check whether ball is off screen (y)
ball3_movement[1]=-ball3_movement[1] #change direction on y axis
if ball4_rect.left<0 or ball4_rect.right>screen_width: #check whether ball is off screen (x)
ball4_movement[0]=-ball4_movement[0]
if ball4_rect.top<0 or ball4_rect.bottom>screen_height: #check whether ball is off screen (y)
ball4_movement[1]=-ball4_movement[1] #change direction on y axis
if ball_rect.top > screen_height and lives >0:
lives=lives-1
livest="Lives: {}".format(lives)
lives_text=font.render(livest,1,(0,0,0))
pygame.time.delay(500)#pause
ball_rect.topleft=[10,0] #reinstate ball
if lives ==0:
pygame.display.flip()
pygame.time.delay(2500)#pause
running=False
ball_rect=ball_rect.move(ball_movement)#move ball
ball2_rect=ball2_rect.move(ball2_movement)
ball3_rect=ball3_rect.move(ball3_movement)
ball4_rect=ball4_rect.move(ball4_movement)
screen.blit(ball, ball_rect)#redraws ball
screen.blit(ball2, ball2_rect)
screen.blit(ball3, ball3_rect)
screen.blit(ball4, ball4_rect)
screen.blit(target, target_rect)
screen.blit(score_text, text_pos)
screen.blit(lives_text, textl_pos)
pygame.display.flip() #displays screen
pygame.quit() #quits game
I would recommend not creating a ball 4 different times. Instead, create a ball class and make 4 ball objects. Try this
class ball:
def __init__(self, x, y, image, location):
self.x = x
self.y = y
self.img = image
self.movement = [x, y]
self.rect = self.img.get_rect()
self.rect.topleft = location
Then, you can get rid of all of this:
x= randint(20,40)
y= randint(20,40)
ball=pygame.image.load ('ball_t.png')
ball_movement=[x,y]#amount ball moves each time (x,y)
ball_location=[100,100]#x,y co-ordinates for start position
ball_rect=ball.get_rect()#gets rectangle for ball
x2= randint(5,15)... etc.
and replace it all with this:
balls = []
balls.append(ball(randint(20, 40), randint(20, 40), 'ball_t.png', [100, 100])
balls.append(ball(randint(5, 15), randint(5, 15), 'ball_tt.png', [10, 10])
balls.append(ball(randint(10, 40), randint(10, 30), 'ball_b.png', [10, 100])
balls.append(ball(randint(10, 30), randint(10, 30), 'ball_bs.png', [200, 100])
Then, take this part:
if ball_rect.left<0 or ball_rect.right>screen_width: #check whether ball is off screen (x)
ball_movement[0]=-ball_movement[0]#change direction on x axis
if ball_rect.top<0 or ball_rect.bottom>screen_height: #check whether ball is off screen (y)
ball_movement[1]=-ball_movement[1] #change direction on y axis
if ball2... etc.
and replace it with a method of ball.
class ball:
def __init__():
blah blah blah init
def is_touching_wall(self):
if self.rect.left < 0 or self.rect.right > screen_width:
self.movement[0] -= self.movement[0]
if self.rect.top < 0 or self.rect.bottom > screen_height:
self.movement[1] -= self.movement[1]
and then in the main function:
for ball in balls:
ball.is_touching_wall()
And then lastly, add one more method to the ball class:
def update(self):
self.rect = self.rect.move(self.movement)
screen.blit(self.img, self.rect)
and replace this part:
ball_rect=ball_rect.move(ball_movement)#move ball
ball2_rect=ball2_rect.move(ball2_movement)
ball3_rect=ball3_rect.move(ball3_movement)
ball4_rect=ball4_rect.move(ball4_movement)
screen.blit(ball, ball_rect)#redraws ball
screen.blit(ball2, ball2_rect)
screen.blit(ball3, ball3_rect)
screen.blit(ball4, ball4_rect)
with this:
for ball in balls:
ball.update()
Lastly, two more recommendations.
Don't post questions on SE saying, "Here's my code. Fix it." You never specified what you tried to fix the problem, where in your code the problem is, or even exactly what your question was. Also, try to only post the smallest part of your code that will replicate your problem. Fix your code, and add more information to your question, and then people will be a lot more willing to help you with the target.
Pay attention to formatting. Read PEP-8. Add spaces inbetween arguments and inbetween comparisons. Indent better. Import this. etc...

Categories

Resources