Blank window while running a game - python

I'm using pygame for my game, but when I start it, I can see a black window. When you click on the close button, you can see all the objects of the game for a short while.
import pygame
pygame.init()
win = pygame.display.set_mode((800, 600))
pygame.display.set_caption('World of Fighters')
walkRight = [pygame.image.load('g_right1.png'),
pygame.image.load('g_right2.png'), pygame.image.load('g_right3.png'),
pygame.image.load('g_right4.png')]
walkLeft = [pygame.image.load('g_left1.png'),
pygame.image.load('g_left2.png'), pygame.image.load('g_left3.png'),
pygame.image.load('g_left4.png')]
bg = pygame.image.load('bg.jpg')
playerStand = pygame.image.load('g_stand.png')
clock = pygame.time.Clock()
#player 2
x2 = 720
y2 = 500
width2 = 35
height2 = 50
speed2 = 15
left2 = False
right2 = False
animCount2 = 0
isJump2 = False
jumpCount2 = 10
#player 1
x = 50
y = 500
width = 35
height = 50
speed = 15
left = False
right = False
animCount = 0
isJump = False
jumpCount = 10
#blue
color = (0, 0, 255)
run = True
def drawWindow():
global animCount
win.blit(bg, (0, 0))
if animCount + 1 >= 30:
animCount = 0
if left:
win.blit(walkLeft[animCount // 5], (x, y))
animCount += 1
elif right:
win.blit(walkRight[animCount // 5], (x, y))
animCount += 1
else:
win.blit(playerStand, (x, y))
pygame.display.update()
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
#p1
if keys[pygame.K_a] and x > 5:
x -= speed
left = True
right = False
elif keys[pygame.K_d] and x < 800 - width - 5:
x += speed
left = False
right = True
else:
right = False
left = False
animCount = 0
if not(isJump):
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
if jumpCount < 0:
y += (jumpCount ** 2) / 2
else:
y -= (jumpCount ** 2) / 2
jumpCount -= 1
else:
isJump = False
jumpCount = 10
#p2
if keys[pygame.K_LEFT] and x2 > 5:
x2 -= speed2
left2 = True
right2 = False
elif keys[pygame.K_RIGHT] and x2 < 800 - width2 - 5:
x2 += speed2
left2 = False
right2 = True
if not(isJump2):
if keys[pygame.K_RCTRL]:
isJump2 = True
else:
right2 = False
left2 = False
animCount2 = 0
if jumpCount2 >= -10:
if jumpCount2 < 0:
y2 += (jumpCount2 ** 2) / 2
else:
y2 -= (jumpCount2 ** 2) / 2
jumpCount2 -= 1
else:
isJump2 = False
jumpCount2 = 10
drawWindow()
pygame.quit()
Also, this game will be a dynamic fighter for 2 players in pixel art style, but I can't find the answer for this bug, because I watched a tutorial and I have the same code like on video.

It is a matter Indentation.
drawWindow() has to be called in the main application loop rather than after the loop.
What you do is:
while run:
clock.tick(30)
# [...]
drawWindow()
pygame.quit()
What you have to do is
while run:
clock.tick(30)
# [...]
drawWindow()
pygame.quit()

Related

Side Scrolling Pygame Problme

I was trying to make a side scroller game with pygame ( i am quite new to pygame ) and my game works quite fine but once it turns fast it just gets a weird kind of glitch for a few seconds and it happens occasionally i think its some kind of background error of size not being enough but my screen size is 800, 447 but the background png i use is 1920, 1080 ( the black screen is my background ) Here is my code
from pygame.locals import *
import os
import sys
import math
pygame.init()
W, H = 800, 447
win = pygame.display.set_mode((W,H))
pygame.display.set_caption('Side Scroller')
x = 200
y = 200
height = 30
width = 30
bg = pygame.image.load(os.path.join('im','bg.png')).convert()
bgX = 0
bgX2 = bg.get_width()
clock = pygame.time.Clock()
class player(object):
run = [pygame.image.load(os.path.join('im', str(x) + '.png')) for x in range(8,16)]
jump = [pygame.image.load(os.path.join('im', str(x) + '.png')) for x in range(1,8)]
slide = [pygame.image.load(os.path.join('images', 'S1.png')),pygame.image.load(os.path.join('images', 'S2.png')),pygame.image.load(os.path.join('images', 'S2.png')),pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')),pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S3.png')), pygame.image.load(os.path.join('images', 'S4.png')), pygame.image.load(os.path.join('images', 'S5.png'))]
jumpList = [![enter image description here][1]][1][1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4]
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.jumping = False
self.sliding = False
self.slideCount = 0
self.jumpCount = 0
self.runCount = 0
self.slideUp = False
def draw(self, win):
if self.jumping:
self.y -= self.jumpList[self.jumpCount] * 1.2
win.blit(self.jump[self.jumpCount//18], (self.x,self.y))
self.jumpCount += 1
if self.jumpCount > 108:
self.jumpCount = 0
self.jumping = False
self.runCount = 0
elif self.sliding or self.slideUp:
if self.slideCount < 20:
self.y += 1
elif self.slideCount == 80:
self.y -= 19
self.sliding = False
self.slideUp = True
if self.slideCount >= 110:
self.slideCount = 0
self.slideUp = False
self.runCount = 0
win.blit(self.slide[self.slideCount//10], (self.x,self.y))
self.slideCount += 1
else:
if self.runCount > 42:
self.runCount = 0
win.blit(self.run[self.runCount//6], (self.x,self.y))
self.runCount += 1
def redrawwindow():
win.blit(bg, (bgX, 0))
win.blit(bg, (bgX2, 0))
runner.draw(win)
pygame.display.update()
run = True
speed = 30[![enter image description here][1]][1]
pygame.time.set_timer(USEREVENT+1, 500)
runner = player(200, 313, 80, 64)
while run:
bgX -= 1.4
bgX2 -= 1.4
if bgX < bg.get_width() * -1:
bgX = bg.get_width()
if bgX2 < bg.get_width() * -1:
bg2 = bg.get_width()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == USEREVENT+1:
speed +=1
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE] or keys[pygame.K_UP]:
if not (runner.jumping):
runner.jumping = True
if keys[pygame.K_DOWN]:
if not (runner.sliding):
runner.sliding = True
redrawwindow()
clock.tick(speed)
pygame.quit()
looks like you just need to update your screen so the pixels will update properly.
you can add
pygame.display.update()
to the end of your game loop
if this still don't work then try using this command to fill in the background with just the color black just and clearing everything
win.fill((0, 0, 0))
you should put it at the start of the game loop

how to replace picture and check for collision with new image and and a moving image in pygame [duplicate]

This question already has answers here:
Animated sprite from few images
(4 answers)
how to make image/images disappear in pygame?
(1 answer)
Closed 2 years ago.
I'm making a pygame game where a player is moving around on a screen and can buy items from a shop(bombs). I'm able to drop the bombs, now I need to replace the bomb image with an explosion image after 3 secs and check for collision with the enemy. I'm attaching my whole code along with some screenshots for reference. Any help is appreciated, thank you
import pygame
import random
pygame.font.init()
width = 900
height = 600
screen = pygame.display.set_mode([width, height])
walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'),
pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'),
pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'),
pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'),
pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
char = pygame.image.load('standing.png')
bomb_pic = pygame.transform.scale(pygame.image.load('bomb.png'), (20,20))
bomb_explosion = pygame.transform.scale(pygame.image.load('explosion1.png'), (40,40))
# char_rect = char.get_rect()
enemy_Left = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'),
pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'),
pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png')]
x = 50
y = 50
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
left = False
right = False
down = False
up = False
walkCount = 0
enemy_vel = 2
enemy_list = []
shop = pygame.transform.scale(pygame.image.load("shop.png"), (60, 60))
clock = pygame.time.Clock()
FPS = 60
font = pygame.font.Font('freesansbold.ttf', 32)
items_font = pygame.font.Font('freesansbold.ttf', 16)
bombs =[]
bag = {'bomb': 0}
print(bag["bomb"])
class Button():
def __init__(self, color, x, y, width, height, text=''):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
def draw(self, win, outline=None):
# Call this method to draw the button on the screen
if outline:
pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)
if self.text != '':
font = pygame.font.SysFont('comicsans', 20)
text = font.render(self.text, 1, (0, 0, 0))
win.blit(text, (
self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))
def shop_run():
shop_bomb = Button((0, 200, 0), 820, 150, 60, 20, text="Bomb_b")
bright_green = (0, 255, 0)
green = (0, 200, 0)
shop_bomb.draw(screen)
def redrawGameWindow():
global walkCount
global font
global bag
global items_font
global enemy_list
screen.fill([166, 166, 166])
for five_enemies in range(6):
random_enemy_location_y = random.randrange(100, 400)
random_enemy_location_x = random.randrange(800, 840)
enemy_list.append([random_enemy_location_x, random_enemy_location_y])
for enemies in range(6):
screen.blit(enemy_Left[enemies], enemy_list[enemies])
enemy_list[enemies][0] -= 0.3
pygame.draw.rect(screen, (0, 0, 0), (800, 0, 100, 600))
if x + char.get_width() < 60 and y + char.get_height() < 60:
shop_run()
screen.blit(shop, (0, 0))
screen.blit(font.render("Menu", True, (255,255,255)),(805, 10))
screen.blit(items_font.render("Bombs: "+ str(bag["bomb"]), True, (255, 255, 255)), (805, 550))
# screen.blit(bomb_explosion, (450, 300))
if walkCount + 1 >= 27:
walkCount = 0
if left:
screen.blit(walkLeft[walkCount // 3], (x, y))
walkCount += 1
elif right:
screen.blit(walkRight[walkCount // 3], (x, y))
walkCount += 1
elif down:
screen.blit(char, (x, y))
walkcount = 0
elif up:
screen.blit(char, (x, y))
walkcount = 0
else:
screen.blit(char, (x, y))
walkCount = 0
for pos in bombs:
screen.blit(bomb_pic, pos)
pygame.display.update()
def main():
run = True
# shopper()
pygame.display.set_caption("bomb-mania")
global x
global y
global width
global height
global vel
global isJump
global jumpCount
global left
global right
global down
global up
global walkCount
global bomb_pic
global font
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if x + char.get_width() < 60 and y + char.get_height() < 60:
buy = pygame.key.get_pressed()
if buy[pygame.K_b]:
bag["bomb"] += 1
print(bag["bomb"])
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and bag["bomb"] >= 1:
bombs.append(((x + (char.get_width()/2)),( y + (char.get_height() - 20))))
bag["bomb"] -= 1
redrawGameWindow()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel - 15:
x -= vel
left = True
right = False
down = False
up = False
elif keys[pygame.K_RIGHT] and x < 800 - vel - width:
x += vel
left = False
right = True
down = False
up = False
elif keys[pygame.K_DOWN] and y < 600 - height:
y += vel
left = False
right = False
down = True
up = False
elif keys[pygame.K_UP] and y > vel - 15:
y -= vel
left = False
right = False
down = False
up = True
else:
left = False
right = False
down = False
up = False
walkCount = 0
clock.tick(FPS)
pygame.display.flip()
main()
enemies are moving towards the left
able to drop bombs
able to buy bombs from the shop
I'm adding a code below please help me troubleshoot and debug this
import pygame
import random
pygame.font.init()
width = 900
height = 600
screen = pygame.display.set_mode([width, height])
walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'),
pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'),
pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'),
pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'),
pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
char = pygame.image.load('standing.png')
bomb_pic = pygame.transform.scale(pygame.image.load('bomb.png'), (20,20))
bomb_explosion = pygame.transform.scale(pygame.image.load('explosion1.png'), (40,40))
# char_rect = char.get_rect()
enemy_Left = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'),
pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'),
pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png')]
x = 50
y = 50
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
left = False
right = False
down = False
up = False
walkCount = 0
enemy_vel = 2
enemy_list = []
shop = pygame.transform.scale(pygame.image.load("shop.png"), (60, 60))
clock = pygame.time.Clock()
FPS = 60
font = pygame.font.Font('freesansbold.ttf', 32)
items_font = pygame.font.Font('freesansbold.ttf', 16)
bombs =[]
bag = {'bomb': 0}
bomb_timer = {"bomb0":0}
bomb_placed = False
bombCount = 1
bombs_dict = {"bomb0": False}
bombTimers = []
explosion_dict = {"explosion0": False}
print(bag["bomb"])
class Button():
def __init__(self, color, x, y, width, height, text=''):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
def draw(self, win, outline=None):
# Call this method to draw the button on the screen
if outline:
pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)
if self.text != '':
font = pygame.font.SysFont('comicsans', 20)
text = font.render(self.text, 1, (0, 0, 0))
win.blit(text, (
self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))
def shop_run():
shop_bomb = Button((0, 200, 0), 820, 150, 60, 20, text="Bomb_b")
bright_green = (0, 255, 0)
green = (0, 200, 0)
shop_bomb.draw(screen)
def redrawGameWindow():
global walkCount
global font
global bag
global items_font
global enemy_list
global bomb_timer
global bomb_placed
global explosion_dict
screen.fill([166, 166, 166])
for five_enemies in range(6):
random_enemy_location_y = random.randrange(100, 400)
random_enemy_location_x = random.randrange(800, 840)
enemy_list.append([random_enemy_location_x, random_enemy_location_y])
for enemies in range(6):
screen.blit(enemy_Left[enemies], enemy_list[enemies])
enemy_list[enemies][0] -= 0.3
pygame.draw.rect(screen, (0, 0, 0), (800, 0, 100, 600))
if x + char.get_width() < 60 and y + char.get_height() < 60:
shop_run()
screen.blit(shop, (0, 0))
screen.blit(font.render("Menu", True, (255,255,255)),(805, 10))
screen.blit(items_font.render("Bombs: "+ str(bag["bomb"]), True, (255, 255, 255)), (805, 550))
# screen.blit(bomb_explosion, (450, 300))
if walkCount + 1 >= 27:
walkCount = 0
if left:
screen.blit(walkLeft[walkCount // 3], (x, y))
walkCount += 1
elif right:
screen.blit(walkRight[walkCount // 3], (x, y))
walkCount += 1
elif down:
screen.blit(char, (x, y))
walkcount = 0
elif up:
screen.blit(char, (x, y))
walkcount = 0
else:
screen.blit(char, (x, y))
walkCount = 0
for pos in bombs:
for i in bombs_dict:
if bombs_dict["bomb"+str(bag["bomb"])]:
screen.blit(bomb_pic, pos)
bomb_timer["bomb"+str(bag["bomb"])] += clock.get_time()
if bomb_timer["bomb"+str(bag["bomb"])] >= 3000:
bombs_dict["bomb"+str(bag["bomb"])] = False
explosion_dict["explosion" + str(bag["bomb"])] = True
del bombs_dict["bomb"+str(bag["bomb"])]
else:
if explosion_dict["explosion" + str(bag["bomb"])]:
screen.blit(bomb_explosion, (pos))
if bomb_timer["bomb"+str(bag["bomb"])] >= 5000:
explosion_dict["explosion" + str(bag["bomb"])] = False
del explosion_dict["explosion" + str(bag["bomb"])]
pygame.display.update()
def main():
run = True
pygame.display.set_caption("bomb-mania")
global x
global y
global width
global height
global vel
global isJump
global jumpCount
global left
global right
global down
global up
global walkCount
global bomb_pic
global font
global bombs_dict
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if x + char.get_width() < 60 and y + char.get_height() < 60:
buy = pygame.key.get_pressed()
if buy[pygame.K_b]:
bag["bomb"] += 1
bombs_dict["bomb"+str(bag["bomb"])] = False
print(bag["bomb"])
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and bag["bomb"] >= 1:
bombs.append(((x + (char.get_width()/2)),( y + (char.get_height() - 20))))
bombs_dict["bomb"+str(bag["bomb"])] = True
bag["bomb"] -= 1
redrawGameWindow()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel - 15:
x -= vel
left = True
right = False
down = False
up = False
elif keys[pygame.K_RIGHT] and x < 800 - vel - width:
x += vel
left = False
right = True
down = False
up = False
elif keys[pygame.K_DOWN] and y < 600 - height:
y += vel
left = False
right = False
down = True
up = False
elif keys[pygame.K_UP] and y > vel - 15:
y -= vel
left = False
right = False
down = False
up = True
else:
left = False
right = False
down = False
up = False
walkCount = 0
clock.tick(FPS)
pygame.display.flip()
main()
Look at pygame.time.Clock(). If you set an FPS rate (say 20), All you have to do is:
myClock = pg.time.Clock()
FPS = 20
bomb_placed = True
bomb_timer = 0
main_loop():
myClock.tick(FPS) # This will keep your FPS at 20 or below so you can estimate time
if bomb_placed:
bomb_timer += myClock.get_time()
if bomb_timer >= 3000:
# Draw the bomb explosion to the screen
if bomb_timer >= 5000:
bomb_timer = 0
bomb_placed = False # Stops displaying the bomb explosion
Hope that helped. Feel free to check out the pygame docs on the subject:
https://www.pygame.org/docs/ref/time.html#pygame.time.Clock.get_time
Example for multiple bombs:
myClock = pg.time.Clock()
FPS = 20
bombA, bombB, bombC = False, False, False
bombA_timer, bombB_timer, bombC_timer = 0, 0, 0
bombs = [bombA, bombB, bombC]
bomb_timers = [bombA_timer, bombB_timer, bombC_timer]
main_loop():
myClock.tick(FPS)
for i in len(bombs):
if bombs[i]:
bomb_timers[i] += myClock.get_time()
if bomb_timers[i] >= 3000:
draw_explosion()
if bomb_timer >= 5000:
bomb_timers[i] = 0
bomb_placed[i] = False
This example simply loops through each of the bomb variables that represent the possible bombs on the screen- in this case a max of 3.
For lots of bombs, first create two dictionaries to hold the values of the bombs (active or not active) and their respective timers. Then loop through each of the bombs and, if the bomb is active, update the timer accordingly:
bombs = {"bomb0": False} # Create dictionaries for the bombs and their timers, with 0-1 entries. (I did one to show you what it looks like).
bombTimers = {bombTimer0: 0}
main_loop():
myClock.tick(FPS)
for i in len(bombs): # For each bomb you have created
bombName = "bomb" + str(i) # get the name of the bomb and corresponding timer
timerName = "bombTimer" + str(i)
if bombs[bombName]: # If the bomg has a value of True:
bombTimers[timerName] += myClock.get_time() # Update the timer
if bombTimers[timerName] >= 3000: # Check if you need to draw the explosion
draw_explosion()
if bomb_timer >= 5000: # Check if the bomb animation is over, and reset the bomb so that it can be used again in the future
bombTimers[timerName] = 0
bombs[bombName] = False
This code works exactly the same as the previous example, but it is much easier to work with. You can easily add more bombs while not starting with an unnecessary amount, like so:
bombCount = 1
bombs = {"bomb0": False}
bombTimers = {"bombTimer0": 0}
main_loop():
if player_placed_bomb(): # When the player drops a bomb in your game:
placed = False # Make a variable saying that you have not activaded the bomb and timer yet
for key, val in bombs: # For each bomb in the bombs dictionary:
if not val: # If the value of that bomb is False (Not being used):
val = True # Activates the bomb that was already created
placed = True # Updates the placed variable indicating that you set a bomb to active
break
if not placed: # After looping through the dictionary, if there were no inactive bomb variables:
bombs["bomb" + str(bombCounter)] = True # Create a new bomb in your dictionary, with a unique name.
bombTimers["bombTimer" + str(bombCounter)] = 0 # Created a new corresponding timer
bombCounter += 1 # Add 1 to the bombCounter, which is used to create the unique name of the next bomb you create.
# Rest of main_loop()
This code ensures that you are working with the smallest dictionaries possible to reduce unneeded computation power. When a bomb explodes, it will become false and it's matching timer reset. The first if loop in the code above ensures that all existing bombs are active, and if not reuses the old bomb and timer. If all bombs are being used, It creates new entries in the dictionaries to allow for more bombs.
I added an unnecesarry amount of comments, but I hope it makes it more understandable for you. Let me know if you have more questions on how this works. If you are unfamiliar with python dict() objects, there are lots of resources on the internet that explain them thoughroughly.
Disclaimer: I haven't actually ran the code, but it should function how it is supposed to. Let me know if it doesn't.
Here are some thoughts on the reformed code:
You use "str(bag["bombs"])" quite a bit in your code- consider defining a variable: b = str(bag["bombs"]), and using that. It would make it much simpler.
I don't know why you delete the "bombs_dict" and "explosions_dict" entries after you are finished with them. The way your loop is set up, I will result in an error. I would suggest rather than deleting the entries, you keep them and attempt to reuse them, as shown in the code snippet above.
Or, if you like deleting them, you need to figure out a way to rename certain keys in your dictionaries so that the names are in order. (if you delete bomb2 from bombs_dict, and you have bomb1 and bomb3, you need to change bomb3 to bomb2 or the loop wont work as intended). You will also need to alter your bombCount variable accordingly.

I am having lag problems with this simple game I made in Pygame, Python 3. How can I fix this?

I was programming my game, with no lag and no problems. Suddenly, when I added my intro the game started lagging.
I have tried making a lot of functions, and replacing a lot of numbers with variables.
I removed the intro, and the game still lagged.
I just need help finding out why my game lags so much, even if it is as simple as this.
import pygame
import sys
import random
pygame.init()
pygame.display.set_caption("Game Draft")
clock = pygame.time.Clock()
# Variables
width = 500
height = 500
LightBlue = (0, 150, 215)
Pink = (215, 0, 100)
Green = (51, 204, 51)
Black = (0, 0, 0)
Blue = (0,0,255)
Yellow = (255, 255, 0)
White = (255, 255, 255)
background_color = (102, 204, 255)
scoreboard_color = (255,255,255)
plataform_color = (153, 102, 51)
intro_background = (128, 128, 128)
player_name = "Neme"
player_color = (0, 0, 0)
player_size = 20
player_vel_x = 20
player_vel_y = 10
player_pos = [width/2, height - (2 * player_size)]
enemy_color = (102, 51, 0)
enemy_size = 20
enemy_vel_x = 20
enemy_vel_y = 5
enemy_pos = [random.randint(0, width-enemy_size), (24 / 25 * height)]
enemy_list = [enemy_pos]
enemy_dif = 4
enemy_dif_increase = 2
enemy_amount = 30
prop_color = (0, 51, 204)
prop_size = 1
prop_vel_y = 5
prop_vel_x = 5
prop_pos = [random.randint(0, width - prop_size), (21 / 25 * height)]
prop_list = [prop_pos]
prop_dif = 4
prop_dif_increase = 2
prop_amount = 50
intro_size1 = 50
intro_size2 = 40
intro_sentence1 = "Welcome to Game Draft"
intro_sentence2 = "Press key to start!"
scoreboard_font = pygame.font.SysFont("monoface", 50)
ign_font = pygame.font.SysFont("monoface", player_size)
intro_font1 = pygame.font.SysFont("monoface", intro_size1)
intro_font2 = pygame.font.SysFont("monoface", intro_size2)
score = 0
#Velocity Functions
def player_level_y(score, player_vel_y):
pvy = player_vel_y
sc = score
if sc < 100:
pvy = player_size*.25
elif sc < 200:
pvy = player_size*.5
elif sc < 300:
pvy = player_size*.75
elif sc < 400:
pvy = player_size
elif sc < 500:
pvy = player_size*1.25
else:
pvy = player_size * 1.25
return pvy
def player_level_x(score, player_vel_x):
sc = score
pvx = player_vel_x
if sc < 100:
pvx = player_size/2
elif sc < 200:
pvx = player_size*.75
elif sc < 300:
pvx = player_size*1
elif sc < 400:
pvx = player_size*1.15
elif sc < 500:
pvx = player_size*1.25
else:
pvx = player_size * 1.25
return pvx
def enemy_level_y(score, enemy_vel_y):
sc = score
evy = enemy_vel_y
if sc < 100:
evy = enemy_dif + enemy_dif_increase*1
elif sc < 300:
evy = enemy_dif + enemy_dif_increase*2
elif sc < 500:
evy = enemy_dif + enemy_dif_increase*3
elif sc < 700:
evy = enemy_dif + enemy_dif_increase*4
elif sc < 1500:
evy = enemy_dif + enemy_dif_increase*5
else:
evy = enemy_dif + enemy_dif_increase*6
return enemy_vel_y
#Enemey Functions
def drop_enemies(enemy_list):
delay = random.random()
if len(enemy_list) < enemy_amount and delay < 0.1:
x_pos = random.randint(0, width - enemy_size)
y_pos = enemy_size
enemy_list.append([x_pos, y_pos])
def draw_enemies(enemy_list):
for enemy_pos in enemy_list:
pygame.draw.rect(screen, enemy_color, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size))
def update_enemy_pos(enemy_list, score):
for idx, enemy_pos in enumerate(enemy_list):
if enemy_pos[1] >= 0 and enemy_pos[1] <= height:
enemy_pos[1] += enemy_vel_y
else:
enemy_list.pop(idx)
score += 5
return score
# Prop Functions
def drop_props(prop_list):
delay = random.random()
if len(prop_list) < prop_amount and delay < 0.1:
x_pos = random.randint(0, width - prop_size)
y_pos = prop_size
prop_list.append([x_pos, y_pos])
def draw_props(prop_list):
for prop_pos in prop_list:
pygame.draw.rect(screen, prop_color, (prop_pos[0], prop_pos[1], width/20, prop_size))
def update_prop_pos(prop_list):
for idx, prop_pos in enumerate(prop_list):
if prop_pos[1] >= 0 and prop_pos[1] <= height:
prop_pos[1] += prop_vel_y
else:
prop_list.pop(idx)
# Boarder Functions
def boarder_left(player_pos):
if player_pos[0] <= 0 - player_size:
return True
return False
def boarder_right(player_pos):
if player_pos[0] >= width:
return True
return False
def boarder_down(player_pos):
if player_pos[1] >= height - player_size:
return True
return False
# Game_Over Functions
def collision_check(enemy_list, player_pos):
for enemy_pos in enemy_list:
if detect_collision(enemy_pos, player_pos):
return True
return False
def detect_collision(player_pos, enemy_pos):
p_x = player_pos[0]
p_y = player_pos[1]
e_x = enemy_pos[0]
e_y = enemy_pos[1]
if (e_x >= p_x and e_x < (p_x + enemy_size)) or (p_x >= e_x and p_x < (e_x + player_size)):
if (e_y >= p_y and e_y < (p_y + enemy_size)) or (p_y >= e_y and p_y < (e_y + player_size)):
return True
return False
#Winning Function
def winning(player_pos):
if player_pos[1] <= 0 - player_size:
return True
return False
# Intro Screen
screen = pygame.display.set_mode((width, height))
intro = True
while intro:
pygame.time.delay(100)
for event in pygame.event.get():
keys = pygame.key.get_pressed()
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
intro = False
screen.fill(intro_background)
welcome = str(intro_sentence1)
tfs1 = intro_font1.render(welcome, 1, White)
screen.blit(tfs1, (width/4 - intro_size1, height/3))
welcome = str(intro_sentence2)
tfs1 = intro_font2.render(welcome, 1, White)
screen.blit(tfs1, (width/4 - intro_size2, height/2))
clock.tick(60)
pygame.display.update()
# Game Screen
screen = pygame.display.set_mode((width, height))
game_over = False
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
keys = pygame.key.get_pressed()
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
x = player_pos[0]
y = player_pos[1]
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
x -= player_vel_x
elif keys[pygame.K_d] or keys[pygame.K_RIGHT]:
x += player_vel_x
elif keys[pygame.K_s] or keys[pygame.K_DOWN]:
y += player_vel_y
elif keys[pygame.K_w] or keys[pygame.K_UP]:
y -= player_vel_y
elif keys[pygame.K_PERIOD]:
enemy_dif += 1
elif keys[pygame.K_COMMA]:
enemy_dif -= 1
player_pos = [x,y]
screen.fill(background_color)
drop_enemies(enemy_list)
drop_props(prop_list)
score = update_enemy_pos(enemy_list, score)
player_vel_y = player_level_y(score, player_vel_y)
player_vel_x = player_level_x(score, player_vel_x)
enemy_vel_y = enemy_level_y(score, enemy_vel_y)
update_prop_pos(prop_list)
if boarder_left(player_pos):
player_pos[0] = width - player_size/2
if boarder_right(player_pos):
player_pos[0] = 0 - player_size/2
if boarder_down(player_pos):
player_pos[1] = height - player_size
if winning(player_pos):
enemy_amount = 0
enemy_vel_y = 0
player_pos[1] = 0
if collision_check(enemy_list, player_pos):
game_over = True
break
pygame.draw.rect(screen, plataform_color, (0, 0, width, height - (23 / 25 * height) ) )
pygame.draw.rect(screen, player_color, (player_pos[0], player_pos[1], player_size, player_size))
draw_enemies(enemy_list)
draw_props(prop_list)
scoreboard = str(score)
tfs2 = scoreboard_font.render(scoreboard, 1, scoreboard_color)
screen.blit(tfs2, (width - 125, height - ( .98 * height)))
ign = str(player_name)
tfs3 = ign_font.render(ign, 1, White)
screen.blit(tfs3, (player_pos[0] - player_size/4, player_pos[1] + player_size))
clock.tick(60)
pygame.display.update()
No error, just random lag.
[...] need help finding out why my game lags so much [...]
Of course, what do you expect? You've a delay of a 0.1 seconds in the game loop:
pygame.time.delay(100)
Delete the delay. Use pygame.time.Clock to control the frames per second and thus the game speed.
Further move the code which moves the player out of the event loop in the main application loop for a smooth movement:
while run:
#pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# <--
keys = pygame.key.get_pressed()
x = player_pos[0]
y = player_pos[1]
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
x -= player_vel_x
elif keys[pygame.K_d] or keys[pygame.K_RIGHT]:
x += player_vel_x
elif keys[pygame.K_s] or keys[pygame.K_DOWN]:
y += player_vel_y
elif keys[pygame.K_w] or keys[pygame.K_UP]:
y -= player_vel_y
elif keys[pygame.K_PERIOD]:
enemy_dif += 1
elif keys[pygame.K_COMMA]:
enemy_dif -= 1
player_pos = [x,y]
# [...]
clock.tick(30)

How to fix 'Couldn't open C:\GAME\R1.png' in pygame?

I'm trying to learn how to make a game using python and pygame, but I'm pretty new to this, so I can't understand the jargon that comes with the answers that are similar to my own question.
I'm following a video playlist on YouTube called Tech With Tim and I need to use some images for the character in the game, but the images simply won't load and keeps coming up with 'Couldn't open C:\GAME\R1.png'.
They're already in the same folder why is what makes this so confusing. All the images are also named correctly to the best of my understanding (i.e. they all have the names used in the code below:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 480))
pygame.display.set_caption("First Game")
walkRight = [pygame.image.load('C:\GAME\R1.png'), pygame.image.load('C:\GAME\R2.png'), pygame.image.load('C:\GAME\R3.png'),
pygame.image.load('C:\GAME\R4.png'), pygame.image.load('C:\GAME\R5.png'), pygame.image.load('C:\GAME\R6.png'),
pygame.image.load('C:\GAME\R7.png'), pygame.image.load('C:\GAME\R8.png'), pygame.image.load('C:\GAME\R9.png')]
walkLeft = [pygame.image.load('C:\GAME\L1.png'), pygame.image.load('C:\GAME\L2.png'), pygame.image.load('C:\GAME\L3.png'),
pygame.image.load('C:\GAME\L4.png'), pygame.image.load('C:\GAME\L5.png'), pygame.image.load('C:\GAME\L6.png'),
pygame.image.load('C:\GAME\L7.png'), pygame.image.load('C:\GAME\L8.png'), pygame.image.load('C:\GAME\L9.png')]
bg = pygame.image.load('C:\GAME\Bg.jpg')
char = pygame.image.load('C:\GAME\standing.png')
x = 50
y = 425
width = 64
height = 64
vel = 5
clock = pygame.time.Clock()
isJump = False
jumpCount = 10
left = False
right = False
walkCount = 0
def redrawGameWindow():
global walkCount
win.blit(bg, (0, 0))
if walkCount + 1 >= 27:
walkCount = 0
if left:
win.blit(walkLeft[walkCount // 3], (x, y))
walkCount += 1
elif right:
win.blit(walkRight[walkCount // 3], (x, y))
walkCount += 1
else:
win.blit(char, (x, y))
walkCount = 0
pygame.display.update()
run = True
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
left = True
right = False
elif keys[pygame.K_RIGHT] and x < 500 - vel - width:
x += vel
left = False
right = True
else:
left = False
right = False
walkCount = 0
if not (isJump):
if keys[pygame.K_SPACE]:
isJump = True
left = False
right = False
walkCount = 0
else:
if jumpCount >= -10:
y -= (jumpCount * abs(jumpCount)) * 0.5
jumpCount -= 1
else:
jumpCount = 10
isJump = False
redrawGameWindow()
pygame.quit()
I expect to see the game load and work, but instead, the error 'Couldn't open C:\GAME\R1.png' comes up.
I'd appreciate any help!
First, be sure that is the good path to your file.
Also, you should use '/' instead of '\'. Your path should be : 'C:/GAME/R1.png'

Ping Pong Pygame collision with side of pad

I am trying to program a ping pong game in python, and I am able to make the ball bounce off the side of the pad. However, I am unable to make the ball bounce off the top and bottom of the pad. I am currently only doing the left pad (pad1). Here is my code:
import sys, pygame, random, math
pygame.init()
width, height = 1200, 675
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
clock = pygame.time.Clock()
FPS = 120
xmb1 = False
xmf1 = False
ymb1 = False
ymf1 = False
xmb2 = False
xmf2 = False
ymb2 = False
ymf2 = False
squareh = 275
squarew = 35
squares = 3
x1 = 100
y1 = (height / 2) - (squareh / 2)
x2 = width - 100 - squarew
y2 = (height / 2) - (squareh / 2)
BLACK = (0,0,0)
WHITE = (255,255,255)
font = pygame.font.SysFont("arial", 30)
text = font.render("Press Space to start", True, WHITE)
text3 = font.render("3", True, WHITE)
text2 = font.render("2", True, WHITE)
text1 = font.render("1", True, WHITE)
startt = font.render("Start!", True, WHITE)
text3b = False
text2b = False
text1b = False
starttb = False
start = False
startballmove = False
bx = width / 2
by = height / 2
br = 40
bms = 6
bxm = random.randint(-bms, bms)
bym = random.randint(-bms, bms)
btc = by
blc = bx
bbc = by + br + br
brc = bx + br + br
circle=pygame.Surface((br * 2, br * 2))
circle.fill((0, 0, 0))
pygame.draw.circle(circle, WHITE , (br, br), br, 0)
circle.set_colorkey((0, 0, 0))
pad1 = pygame.Rect(x1, y1, squarew, squareh)
pad2 = pygame.Rect(x2, y2, squarew, squareh)
while 1:
if start and not text1b and not text2b and not text3b and not starttb and not startballmove:
text3b = True
pygame.time.set_timer(pygame.USEREVENT, 1000)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
# pad 2 check if key is down
if event.key == pygame.K_UP:
ymb2 = True
if event.key == pygame.K_DOWN:
ymf2 = True
# pad 1 check if key is down
if event.key == pygame.K_w:
ymb1 = True
if event.key == pygame.K_s:
ymf1 = True
if event.key == pygame.K_SPACE:
start = True
elif event.type == pygame.KEYUP:
#pad 2 check if key goes up
if event.key == pygame.K_UP:
ymb2 = False
if event.key == pygame.K_DOWN:
ymf2 = False
# pad 1 check if key goes up
if event.key == pygame.K_w:
ymb1 = False
if event.key == pygame.K_s:
ymf1 = False
# check if window has been resized
if event.type == pygame.VIDEORESIZE:
width = event.dict['size'][0]
height = event.dict['size'][1]
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
if event.type == pygame.USEREVENT:
# check if start should be hidden
if starttb:
starttb = False
startballmove = True
# check if start should be showed
if text1b and not text2b and not text3b:
text1b = False
text2b = False
text3b = False
starttb = True
# check if 1 should be showed
if text2b and not text3b and not text1b:
text3b = False
text2b = False
text1b = True
# check if 2 should be showed
if text3b and not text2b and not text1b:
text3b = False
text2b = True
text1b = False
# check if pad 1 is out of bounds and move it
if ymb1 and not (y1 <= 0): y1 -= squares
if ymf1 and not (y1 + squareh >= height): y1 += squares
if y1 > (height - squareh) + 1: y1 -= squares
# check if pad 2 is out of bounds and move it
if ymb2 and not (y2 <= 0): y2 -= squares
if ymf2 and not (y2 + squareh >= height): y2 += squares
if y2 > (height - squareh) + 1: y2 -= squares
# put pads in center if game has not started
if not start:
# pad 1
x1 = 75
y1 = (height / 2) - (squareh / 2)
# pad 2
x2 = width - 75 - squarew
y2 = (height / 2) - (squareh / 2)
#ball
bx = width / 2 - br
by = height / 2 - br
# put pads in center in x if game has started
else:
# pad 1
x1 = 75
# pad 2
x2 = width - 75 - squarew
# if ball has not started moving center it
if not startballmove:
bx = width / 2 - br
by = height / 2 - br
# check if movement variables are 0
while bxm == 0 or bym == 0:
if bxm == 0:
bxm = random.randint(-6, 6)
if bym == 0:
bym = random.randint(-6, 6)
screen.fill(BLACK)
# draw starting text if game has not started
if not start:
screen.blit(text,((width / 2) - text.get_width() // 2, (height / 4) - text.get_height() // 2))
# put 3 on screen
if start and text3b:
screen.blit(text3,((width / 2) - 15, (height / 4) - (text.get_height() / 2)))
# put 2 on screen
if start and text2b:
screen.blit(text2,((width / 2) - 15, (height / 4) - (text.get_height() / 2)))
# put 1 on screen
if start and text1b:
screen.blit(text1,((width / 2) - 15, (height / 4) - (text.get_height() / 2)))
# put start on screen
if start and starttb:
screen.blit(startt,((width / 2) - (text.get_width() / 8), (height / 4) - (text.get_height() / 2)))
# check if ball is out of bounds
btc = by
blc = bx
bbc = by + br + br
brc = bx + br + br
if start and startballmove:
# screen
if btc <= 0:
bym = bym * -1
print("top side")
if bbc >= height:
bym = bym * -1
print("bottom side")
if blc <= 0:
bxm = bxm * -1
print("left side")
if brc >= width:
bxm = bxm * -1
print("right side")
# left pad
pad1 = pygame.Rect(x1, y1, squarew, squareh)
if pad1.collidepoint(int(bx), int(by)):
bxm = bxm * -1
bx += abs(bxm)
print("Collision")
# move ball
if start and startballmove:
bx += bxm
by += bym
# draw circle if game start
if start:
screen.blit(circle, (int(bx), int(by)))
# draw pad 1
pad1 = pygame.Rect(x1, y1, squarew, squareh)
pygame.draw.rect(screen, WHITE, pad1, 0)
# draw pad 2
pad2 = pygame.Rect(x2, y2, squarew, squareh)
pygame.draw.rect(screen, WHITE, pad2, 0)
pygame.display.flip()
clock.tick(FPS)
Does anyone know how to do this? I have only recently learned to program in python and pygame.
You have to split move and cheking collision on two steps. First move only in X and then you can recognize front collision. Later move in Y and you can recogize side collision.
Your code is big mess so it may need a lot changes. You should organize code and use better names for variables to make it more readable.
And you could use elif and nested if so it will be cleaner.
import sys
import pygame
import random
import math
# --- constants --- (UPPER_CASE_NAMES)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
FPS = 60
SQUARE_HEIGHT = 275
SQUARE_WIDTH = 35
SQUARE_SPEED = 3
BALL_R = 40
# --- main ---
window_width = 1200
window_height = 675
# - init -
pygame.init()
screen = pygame.display.set_mode((window_width, window_height), pygame.RESIZABLE)
screen_rect = screen.get_rect()
# - text -
font = pygame.font.SysFont("arial", 30)
# - text - press space -
text_press_space = font.render("Press Space to start", True, WHITE)
text_press_space_rect = text_press_space.get_rect()
text_press_space_rect.centerx = screen_rect.centerx
text_press_space_rect.centery = screen_rect.height // 4
# - text - count down -
text_1 = font.render("1", True, WHITE)
text_1_rect = text_1.get_rect()
text_1_rect.centerx = screen_rect.centerx
text_1_rect.centery = screen_rect.height // 4
text_2 = font.render("2", True, WHITE)
text_2_rect = text_2.get_rect()
text_2_rect.centerx = screen_rect.centerx
text_2_rect.centery = screen_rect.height // 4
text_3 = font.render("3", True, WHITE)
text_3_rect = text_3.get_rect()
text_3_rect.centerx = screen_rect.centerx
text_3_rect.centery = screen_rect.height // 4
text_start = font.render("Start!", True, WHITE)
text_start_rect = text_start.get_rect()
text_start_rect.centerx = screen_rect.centerx
text_start_rect.centery = screen_rect.height // 4
display_text_3 = False
display_text_2 = False
display_text_1 = False
display_text_start = False
# - ball -
ball = pygame.Surface((BALL_R * 2, BALL_R * 2))
# circle.fill(BLACK) # not need it because surface is black as default
ball.set_colorkey(BLACK)
pygame.draw.circle(ball, WHITE , (BALL_R, BALL_R), BALL_R, 0)
ball_rect = ball.get_rect()
ball_rect.center = screen_rect.center
ball_speed = 6
ball_speed_x = ball_speed * random.choice([1, -1])
ball_speed_y = ball_speed * random.choice([1, -1])
ball_move = False
# - pads -
pad1_rect = pygame.Rect(0, 0, SQUARE_WIDTH, SQUARE_HEIGHT)
pad1_rect.left = 100
pad1_rect.centery = screen_rect.centery
pad1_move_up = False
pad1_move_down = False
pad2_rect = pygame.Rect(0, 0, SQUARE_WIDTH, SQUARE_HEIGHT)
pad2_rect.right = screen_rect.right - 100
pad2_rect.centery = screen_rect.centery
pad2_move_up = False
pad2_move_down = False
# --- mainloop ---
play_start = False
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
# pad 2 check if key is down
if event.key == pygame.K_UP:
pad2_move_up = True
elif event.key == pygame.K_DOWN:
pad2_move_down = True
# pad 1 check if key is down
elif event.key == pygame.K_w:
pad1_move_up = True
elif event.key == pygame.K_s:
pad1_move_down = True
elif event.key == pygame.K_SPACE:
if not play_start:
play_start = True
pygame.time.set_timer(pygame.USEREVENT, 1000)
display_text_3 = True
elif event.type == pygame.KEYUP:
#pad 2 check if key goes up
if event.key == pygame.K_UP:
pad2_move_up = False
elif event.key == pygame.K_DOWN:
pad2_move_down = False
# pad 1 check if key goes up
elif event.key == pygame.K_w:
pad1_move_up = False
elif event.key == pygame.K_s:
pad1_move_down = False
# check if window has been resized
if event.type == pygame.VIDEORESIZE:
window_width = event.dict['size'][0]
window_height = event.dict['size'][1]
screen = pygame.display.set_mode((window_width, window_height), pygame.RESIZABLE)
screen_rect = screen.get_rect()
if event.type == pygame.USEREVENT:
if display_text_3:
display_text_3 = False
display_text_2 = True
elif display_text_2:
display_text_2 = False
display_text_1 = True
elif display_text_1:
display_text_1 = False
display_text_start = True
elif display_text_start:
display_text_start = False
ball_move = True
# - moves (without draws) -
# move paddle 1
if pad1_move_up and pad1_rect.top > 0: # > screen_rect.top:
pad1_rect.y -= SQUARE_SPEED
if pad1_rect.top < 0:
pad1_rect.top = 0
if pad1_move_down and pad1_rect.bottom < screen_rect.bottom:
pad1_rect.y += SQUARE_SPEED
if pad1_rect.bottom > screen_rect.bottom:
pad1_rect.bottom = screen_rect.bottom
# move paddle 2
if pad2_move_up and pad2_rect.top > 0: # > screen_rect.top:
pad2_rect.y -= SQUARE_SPEED
if pad2_rect.top < 0:
pad2_rect.top = 0
if pad2_move_down and pad2_rect.bottom < screen_rect.bottom:
pad2_rect.y += SQUARE_SPEED
if pad2_rect.bottom > screen_rect.bottom:
pad2_rect.bottom = screen_rect.bottom
# check if ball is out of bounds
if play_start:
# move ball
if ball_move:
ball_rect.x += ball_speed_x
# check FRONT collision with pads
if pad1_rect.colliderect(ball_rect):
#if
ball_rect.left = pad1_rect.right
ball_speed_x = -ball_speed_x
print("Front collision right pad")
if pad2_rect.colliderect(ball_rect):
ball_rect.right = pad2_rect.left
ball_speed_x = -ball_speed_x
print("Front collision left pad")
ball_rect.y += ball_speed_y
# check SIDE collision with pads
if pad1_rect.colliderect(ball_rect):
# move from top
if ball_speed_y > 0:
ball_rect.bottom = pad1_rect.top
print("Top collision right pad")
# move from bottom
else: # elif ball_speed_y < 0:
ball_rect.top = pad1_rect.bottom
print("Bottom collision right pad")
# change both or only Y (depends on what effect you need)
ball_speed_x = -ball_speed_x
ball_speed_y = -ball_speed_y
if pad2_rect.colliderect(ball_rect):
# move from top
if ball_speed_y > 0:
ball_rect.bottom = pad2_rect.top
print("Top collision left pad")
# move from bottom
else: # if ball_speed_y < 0:
ball_rect.top = pad2_rect.bottom
print("Bottom collision left pad")
# change both or only Y (depends on what effect you need)
ball_speed_x = -ball_speed_x
ball_speed_y = -ball_speed_y
# check collision with border
if ball_rect.left <= 0: # <= screen_rect.left:
ball_speed_x = -ball_speed_x
print("left side")
print("point for left player")
# move ball to center
ball_rect.center = screen_rect.center
ball_speed_x = ball_speed * random.choice([1, -1])
ball_speed_y = ball_speed * random.choice([1, -1])
if ball_rect.right >= screen_rect.right:
ball_speed_x = -ball_speed_x
print("right side")
print("point for right player")
# move ball to center
ball_rect.center = screen_rect.center
ball_speed_x = ball_speed * random.choice([1, -1])
ball_speed_y = ball_speed * random.choice([1, -1])
if ball_rect.top <= 0: # <= screen_rect.top:
ball_speed_y = -ball_speed_y
print("top side")
if ball_rect.bottom >= screen_rect.bottom:
ball_speed_y = -ball_speed_y
print("bottom side")
# - draws (without moves) -
screen.fill(BLACK)
if not play_start:
screen.blit(text_press_space, text_press_space_rect)
else: # if play_start:
if display_text_3:
screen.blit(text_3, text_3_rect)
if display_text_2:
screen.blit(text_2, text_2_rect)
if display_text_1:
screen.blit(text_1, text_1_rect)
if display_text_start:
screen.blit(text_start, text_start_rect)
screen.blit(ball, ball_rect)
# draw pads
pygame.draw.rect(screen, WHITE, pad1_rect, 0)
pygame.draw.rect(screen, WHITE, pad2_rect, 0)
pygame.display.flip()
clock.tick(FPS)
# --- end ---
pygame.quit()

Categories

Resources