Python variables change in every object - python

I'm playing around with pygame for the first time, trying to make multiple rectangles move across the screen. Here's my code:
import pygame
pygame.init()
scrWidth = 1200
scrHeigth = 900
done = False
rectangles = []
screen = pygame.display.set_mode((scrWidth, scrHeigth))
clock = pygame.time.Clock()
class Rectangle:
speed = [1,1]
colourSpeed = 300
colourID = 0
colour = (0, 0, 255)
size = 60
def __init__(self, name = "", x=0, y=0, speed=False, colour=False, size=False):
self.name = name
self.x = x
self.y = y
self.doesSpeed = speed
self.doesColour = colour
self.doesSize = size
def draw(self):
pygame.draw.rect(screen, self.colour, pygame.Rect(self.x, self.y, self.size, self.size))
def checkCollision(self):
if self.x < 0 or self.x > scrWidth-self.size:
self.speed[0] = -self.speed[0]
if self.y < 0 or self.y > scrHeigth-self.size:
self.speed[1] = -self.speed[1]
def move(self):
self.x += self.speed[0]
self.y += self.speed[1]
def changeColour(self):
self.colourID = (self.colourID+1)%self.colourSpeed
if 0 <= self.colourID < (self.colourSpeed/3):
self.colour = (0, 0, 255)
elif (self.colourSpeed/3) <= self.colourID < (2*self.colourSpeed/3):
self.colour = (0, 255, 0)
elif (2*self.colourSpeed/3) <= self.colourID < self.colourSpeed:
self.colour = (255, 0, 0)
rect1 = Rectangle("rect1", 30, 30, False, True, False)
rectangles.append(rect1)
rect2 = Rectangle("rect2", 300, 300)
rectangles.append(rect2)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
screen.fill((0, 0, 0))
for obj in rectangles:
obj.checkCollision()
if obj.doesColour: obj.changeColour()
obj.move()
obj.draw()
pygame.display.flip()
clock.tick(60)
The problem is that when a rectangle collides with the edge of the screen all rectangles change direction(speed). However if I make rectangles with different speeds this doesn't happen.
ex: rect1 has speed [1,1] and rect2 has speed [2,2].
What's causing this and how can I fix it?

Move the speed, colourSpeed, colourID, colour, size to the init method.
At that moment that fields are defined as class fields, so they are changed globally for all rectangles.
Solution:
def __init__(self, name = "", x=0, y=0, speed=False, colour=False, size=False):
self.name = name
self.x = x
self.y = y
self.doesSpeed = speed
self.doesColour = colour
self.doesSize = size
# here it comes
self.speed = [1,1]
self.colourSpeed = 300
self.colourID = 0
self.colour = (0, 0, 255)
self.size = 60

Related

how do i move obstacles faster for dino game

I'm trying my best to make a dino game replica and I'm stuck at increasing the speed of the obstacles that comes towards the character. The speed of 5 just seems to be the magic number but when I change it to six the rock doesn't get detected and ultimately doesn't spawn the next rock.
I don't know why.
there are no errors except for when the rock doesn't get spawned and I get an index out of range:
Traceback (most recent call last):
File "C:\Users\austi\OneDrive\Desktop\scroller game\main.py", line 109, in <module>
main()
File "C:\Users\austi\OneDrive\Desktop\scroller game\main.py", line 102, in main
if game.collide():
File "C:\Users\austi\OneDrive\Desktop\scroller game\main.py", line 71, in collide
olh = self.obstacles[0].hitBox[0]
IndexError: list index out of range
here is what I got:
main.py
import pygame
import math
import random
from player import Player
from objects import Obstacle
WIDTH, HEIGHT = 700, 400
class Game:
def __init__(self, playerSprite=None, obSprite= None):
self.playerSprite = playerSprite
self.obSprite = obSprite
self.player = Player(50, 50, 50, 50, 8, (0, 0, 0), None)
self.obstacle = Obstacle(1, (800, 350, 50, 50), 5, None)
self.obstacles = [self.obstacle]
self.spawnGap = -100
self.speed = 5
def spawn(self):
for obstacle in self.obstacles:
#if its at the specific spot than spawn a rock
if obstacle.pos.x + obstacle.w == WIDTH/2 + self.spawnGap:
#get shape of rock
type = round(random.randint(0, 3))
rect = (700, 350, 50, 50)
if self.obSprite != None:
self.obSprite = pygame.transform.scale(self.obSprite, (50, 50))
if type == 1:
rect = (700, 350, 25, 50)
if self.obSprite != None:
self.obSprite = pygame.transform.scale(self.obSprite, (25, 50))
if type == 2 :
rect = (700, 375, 50, 25)
if self.obSprite != None:
self.obSprite = pygame.transform.scale(self.obSprite, (50, 25))
if type == 3:
rect = (700, 350, 75, 50)
if self.obSprite != None:
self.obSprite = pygame.transform.scale(self.obSprite, (75, 50))
#increase the place in which the rock spawns
self.spawnGap += 10
#create a new obstacle and append it to the obstacle array
self.obstacle = Obstacle(type, rect, 5, None)
self.obstacles.append(self.obstacle)
#delete obstacle when its at the end
if obstacle.pos.x < -obstacle.w:
index = self.obstacles.index(obstacle)
del self.obstacles[index]
def draw(self, win):
self.spawn()
self.hitBoxes()
self.player.draw(window)
for obstacle in self.obstacles:
obstacle.draw(window)
def collide(self):
plh = self.player.hitBox[0]
prh = self.player.hitBox[0] + self.player.hitBox[2]
pth = self.player.hitBox[1]
pbh = self.player.hitBox[1] + self.player.hitBox[3]
olh = self.obstacles[0].hitBox[0]
orh = self.obstacles[0].hitBox[0] + self.obstacles[0].hitBox[2]
oth = self.obstacles[0].hitBox[1]
obh = self.obstacles[0].hitBox[1] + self.obstacles[0].hitBox[3]
if prh >= olh and plh <= orh:
if pbh >= oth:
return True
else:
return False
def hitBoxes(self):
key = pygame.key.get_pressed()
if key[pygame.K_a]:
self.player.showHitBoxes = True
for obstacle in self.obstacles:
obstacle.showHitBoxes = True
if key[pygame.K_s]:
self.player.showHitBoxes = False
for obstacle in self.obstacles:
obstacle.showHitBoxes = False
def main():
done = False
pressed = False
game = Game()
time = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
window.fill((255, 255, 255))
game.draw(window)
if game.collide():
done = True
pygame.draw.line(window, (255, 0, 0), (WIDTH/2, 0), (WIDTH/2, HEIGHT))
pygame.draw.line(window, (0, 0, 255), (WIDTH/2 + game.spawnGap, 0), (WIDTH/2 + game.spawnGap, HEIGHT))
pygame.display.update()
time.tick(60)
pygame.quit()
main()
objects.py
import pygame
from pygame.math import Vector2
class Obstacle:
def __init__(self, type, rect, speed, rockSprite=None):
self.obSprite = rockSprite
self.xSpawn = rect[0]
self.ySpawn = rect[1]
self.pos = Vector2(self.xSpawn, self.ySpawn)
self.w = rect[2]
self.h = rect[3]
self.type = type
self.speed = speed
self.hitBox = ()
self.showHitBoxes = False
def draw(self, win):
self.hitBox = (self.pos.x, self.pos.y, self.w, self.h)
if self.showHitBoxes:
pygame.draw.rect(win, (255, 0, 0), self.hitBox, 2)
if self.obSprite != None:
win.blit(self.obSprite, self.pos)
else:
pygame.draw.rect(win, (0, 0, 0), (self.pos.x, self.pos.y, self.w, self.h))
self.update()
def update(self):
self.pos.x -= self.speed
player.py
import pygame
from pygame.math import Vector2
WIDTH, HEIGHT = 700, 400
class Player:
def __init__(self, x, y, w, h, jumpVel, color, sprite=None):
self.playerSprite = sprite
self.x = x
self.y = y
self.w = w
self.h = h
self.pos = Vector2(self.x, self.y)
self.color = color
self.gravity = 0.3
self.vel = self.gravity
self.jVel = jumpVel
self.touching_surface = False
self.canJump = True
self.jumping = False
self.hitBox = ()
self.showHitBoxes = False
def draw(self, win):
self.hitBox = (self.pos.x + 0, self.pos.y + 10, 50, 30)
if self.showHitBoxes:
pygame.draw.rect(win, (255, 0, 0), self.hitBox, 2)
if self.playerSprite != None:
win.blit(self.playerSprite, self.pos)
else:
pygame.draw.rect(win, (255, 0, 0), (self.pos.x, self.pos.y, 50, 50))
self.update()
def update(self):
mouse = pygame.mouse.get_pressed()
if self.pos.y + self.h >= HEIGHT:
self.touching_surface = True
self.vel = 0
self.pos.y = HEIGHT - self.h
else:
self.touching_surface = False
self.vel += self.gravity
if self.pos.x <= 0:
self.pos.x = 0
elif self.pos.x + self.w >= WIDTH:
self.pos.x = WIDTH - self.w
if self.touching_surface:
self.canJump = True
else:
self.canJump = False
if mouse[0] == 1 and self.canJump and not self.jumping:
self.jumping = True
self.canJump = False
self.vel = -self.jVel
if mouse[0] == 0:
self.jumping = False
self.pos.y += self.vel
and I suspect the issue is in the spawn function in the Game class in main.py
I've been trying to work on this for a couple days now and I still cannot solve my issue.
The problem is the condition
if obstacle.pos.x + obstacle.w == WIDTH/2 + self.spawnGap:
# [...]
This condition is only True when the obstacle is exactly at a certain position. If the speed changes, the obstacle does not exactly hit the position.
Test on a small range instead of a position. e.g.:
right = obstacle.pos.x + obstacle.w
target = WIDTH/2 + self.spawnGap
if traget <= right < traget + speed:
# [...]

How to code bounce movement in pong using pygame

I'm a noob in python and I'm trying to recreate the Pong game, and I'm trying to do it by myself as much as possible.
These are the issues I currently have with my code:
I basically coded every possible movement of the ball when it bounces on the edge. I spent hours working on it, and I got it to work, I was just wondering if there was a more efficient way to produce a similar output with my code (coz I know this is supposed to be a beginner project)?
Every time the ball bounces past the edge, the score increments for a split second, and it goes back to 0 every time the ball respawns.
How can I make the ball spawn at random directions? at the start (and restart) of every round?
Here's my code for the class and the main function of the program:
import pygame
import random
from rect_button import Button
pygame.init()
WIDTH = 800
HEIGHT = 500
window = pygame.display.set_mode((WIDTH, HEIGHT))
TITLE = "Pong"
pygame.display.set_caption(TITLE)
# COLORS
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
# FONTS
small_font = pygame.font.SysFont("courier", 20)
large_font = pygame.font.SysFont("courier", 60, True)
class Paddle:
def __init__(self, x, y, width, height, vel, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = vel
self.color = color
def draw(self, window):
pygame.draw.rect(window, self.color, (self.x, self.y, self.width, self.height), 0)
class Ball:
def __init__(self, x, y, side, vel, color):
self.x = x
self.y = y
self.side = side
self.vel = vel
self.color = color
self.lower_right = False
self.lower_left = True
self.upper_right = False
self.upper_left = False
self.ball_bag = []
self.last_movement = 'ball.lower_right'
def draw(self, window):
pygame.draw.rect(window, self.color, (self.x, self.y, self.side, self.side), 0)
def move_lower_right(self):
self.x += self.vel
self.y += self.vel
def move_upper_right(self):
self.x += self.vel
self.y -= self.vel
def move_upper_left(self):
self.x -= self.vel
self.y -= self.vel
def move_lower_left(self):
self.x -= self.vel
self.y += self.vel
def start(self):
self.lower_right = True
self.lower_left = False
self.upper_right = False
self.upper_left = False
self.last_movement = 'ball.lower_left'
# return random.choice([self.lower_right, self.lower_left, self.upper_left, self.upper_right]) is True
def main():
run = True
fps = 60
clock = pygame.time.Clock()
# Initializing Paddles
left_paddle = Paddle(20, 100, 10, 50, 5, white)
right_paddle = Paddle(770, 350, 10, 50, 5, white)
balls = []
def redraw_window():
window.fill(black)
left_paddle.draw(window)
right_paddle.draw(window)
for ball in balls:
ball.draw(window)
player_A_text = small_font.render("Player A: " + str(score_A), 1, white)
player_B_text = small_font.render("Player B: " + str(score_B), 1, white)
window.blit(player_A_text, (320 - int(player_A_text.get_width() / 2), 10))
window.blit(player_B_text, (480 - int(player_B_text.get_width() / 2), 10))
pygame.draw.rect(window, white, (20, 450, 760, 1), 0)
pygame.draw.rect(window, white, (20, 49, 760, 1), 0)
pygame.draw.rect(window, white, (19, 50, 1, 400), 0)
pygame.draw.rect(window, white, (780, 50, 1, 400), 0)
pygame.display.update()
while run:
score_A = 0
score_B = 0
clock.tick(fps)
if len(balls) == 0:
ball = Ball(random.randrange(320, 465), random.randrange(200, 285), 15, 3, white)
balls.append(ball)
if ball.lower_left:
ball.move_lower_left()
if ball.last_movement == 'ball.lower_right':
if ball.y + ball.side > HEIGHT - 50:
ball.lower_left = False
ball.last_movement = 'ball.lower_left'
ball.upper_left = True
if ball.last_movement == 'ball.upper_left':
if ball.x < 30:
if left_paddle.x < ball.x < left_paddle.x + left_paddle.width:
if left_paddle.y < ball.y + ball.side < left_paddle.y + left_paddle.height:
ball.lower_left = False
ball.last_movement = 'ball.lower_left'
ball.lower_right = True
else:
score_B += 1
balls.remove(ball)
#ball.start()
if ball.y + ball.side > HEIGHT - 50:
ball.lower_left = False
ball.last_movement = 'ball.lower_left'
ball.upper_left = True
if ball.upper_left:
ball.move_upper_left()
if ball.last_movement == 'ball.lower_left':
if ball.x < 30:
if left_paddle.x < ball.x < left_paddle.x + left_paddle.width:
if left_paddle.y < ball.y + ball.side < left_paddle.y + left_paddle.height:
ball.upper_left = False
ball.last_movement = 'ball.upper_left'
ball.upper_right = True
else:
score_B += 1
balls.remove(ball)
#ball.start()
if ball.y < 50:
ball.upper_left = False
ball.last_movement = 'ball.upper_left'
ball.lower_left = True
if ball.last_movement == 'ball.upper_right':
if ball.y < 50:
ball.upper_left = False
ball.last_movement = 'ball.upper_left'
ball.lower_left = True
if ball.upper_right:
ball.move_upper_right()
if ball.last_movement == 'ball.upper_left':
if ball.y < 50:
ball.upper_right = False
ball.last_movement = 'ball.upper_right'
ball.lower_right = True
if ball.last_movement == 'ball.lower_right':
if ball.x + ball.side > WIDTH - 30:
if right_paddle.x + right_paddle.width > ball.x + ball.side > right_paddle.x:
if right_paddle.y < ball.y + ball.side < right_paddle.y + right_paddle.height:
ball.upper_right = False
ball.last_movement = 'ball.upper_right'
ball.upper_left = True
else:
score_A += 1
balls.remove(ball)
#ball.start()
if ball.y < 50:
ball.upper_right = False
ball.last_movement = 'ball.upper_right'
ball.lower_right = True
if ball.lower_right:
ball.move_lower_right()
if ball.last_movement == 'ball.upper_right':
if ball.y + ball.side > HEIGHT - 50:
ball.lower_right = False
ball.last_movement = 'ball.lower_right'
ball.upper_right = True
if ball.x + ball.side > WIDTH - 30:
if right_paddle.x + right_paddle.width > ball.x + ball.side > right_paddle.x:
if right_paddle.y < ball.y + ball.side < right_paddle.y + right_paddle.height:
ball.lower_right = False
ball.last_movement = 'ball.lower_right'
ball.lower_left = True
else:
score_A += 1
balls.remove(ball)
#ball.start()
if ball.last_movement == 'ball.lower_left':
if ball.y + ball.side > HEIGHT - 50:
ball.lower_right = False
ball.last_movement = 'ball.lower_right'
ball.upper_right = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and right_paddle.y > 50:
right_paddle.y -= right_paddle.vel
if keys[pygame.K_w] and left_paddle.y > 50:
left_paddle.y -= left_paddle.vel
if keys[pygame.K_DOWN] and right_paddle.y + right_paddle.height < HEIGHT - 50:
right_paddle.y += right_paddle.vel
if keys[pygame.K_s] and left_paddle.y + left_paddle.height < HEIGHT - 50:
left_paddle.y += left_paddle.vel
if keys[pygame.K_SPACE]:
pass
redraw_window()
quit()
def main_menu():
run = True
play_button = Button(green, 100, 350, 150, 75, "Play Pong")
quit_button = Button(red, 550, 350, 150, 75, "Quit")
pong_text = large_font.render("Let's Play Pong!!!", 1, black)
while run:
window.fill(white)
play_button.draw(window, black)
quit_button.draw(window, black)
window.blit(pong_text, (int(WIDTH / 2 - pong_text.get_width() / 2), 100))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
quit()
if event.type == pygame.MOUSEMOTION:
if play_button.hover(pygame.mouse.get_pos()):
play_button.color = (0, 200, 0)
else:
play_button.color = green
if quit_button.hover(pygame.mouse.get_pos()):
quit_button.color = (200, 0, 0)
else:
quit_button.color = red
if event.type == pygame.MOUSEBUTTONDOWN:
if play_button.hover(pygame.mouse.get_pos()):
main()
if quit_button.hover(pygame.mouse.get_pos()):
run = False
quit()
main_menu()
Thanks!!!
Answer to
Every time the ball bounces past the edge, the score increments for a split second, and it goes back to 0 every time the ball respawns.
The score is continuously initialized in the main loop. You have to initialize the score before the loop:
def main():
# [...]
score_A = 0 # <--- INSERT
score_B = 0
while run:
# score_A = 0 <--- DELETE
# score_B = 0
Answer to question 1:
Yes. There is definitely a lot more effective way to do things. There is no need to make so many variables for direction. Simply say direction = [True, False], where direction[0] represents left and not direction[0] represents right in x-axis. Similarly, direction[1] represents y axis. This also solves your problem of randomizing the direction at start. You can simply do direction = [random.randint(0, 1), random.randint(0, 1)] in your init method to randomize the direction. The same way, make a list for speed as well. self.speed = [0.5, random.uniform(0.1, 1)]. This way, speed in left and right will always be same, but y will vary depending on the random number chosen, so there is proper randomness and you also don't have to hardcode random directions in. With this, movement becomes really simple.
class Ball:
def __init__(self, x, y, color, size):
self.x = x
self.y = y
self.color = color
self.size = size
self.direction = [random.randint(0, 1), random.randint(0, 1)]
self.speed = [0.5, random.uniform(0.1, 1)]
def draw(self, display):
pygame.draw.rect(display, self.color, (self.x, self.y, self.size, self.size))
def move(self):
if self.direction[0]:
self.x += self.speed[0]
else:
self.x -= self.speed[0]
if self.direction[1]:
self.y += self.speed[0]
else:
self.y -= self.speed[0]
Because we have defined direction directions are Booleans, changing the state also becomes really easy. If the ball hits the paddle, you can simply toggle the bool direction[0] = not direction[0] in x and pick a new random number for y, instead of manually assigning bools.
def switchDirection(self):
self.direction = not self.direction
self.speed[1] = random.uniform(0.1, 1)
Paddle can be improved slightly as well, by giving Paddle class a move function instead of moving in the main loop. It just means you have to write less code.
def move(self, vel, up=pygame.K_UP, down=pygame.K_DOWN):
keys = pygame.key.get_perssed()
if keys[up]:
self.y -= vel
if keys[down]:
self.y += vel
For collisions, I recommend using pygame.Rect() and colliderect since it's a lot more robust and probably more efficient as well.
Example:
import random
import pygame
WIN = pygame.display
D = WIN.set_mode((800, 500))
class Paddle:
def __init__(self, x, y, width, height, vel, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = vel
self.color = color
def move(self, vel, up=pygame.K_UP, down=pygame.K_DOWN):
keys = pygame.key.get_pressed()
if keys[up]:
self.y -= vel
if keys[down]:
self.y += vel
def draw(self, window):
pygame.draw.rect(window, self.color, (self.x, self.y, self.width, self.height), 0)
def getRect(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
left_paddle = Paddle(20, 100, 10, 50, 5, (0, 0, 0))
right_paddle = Paddle(770, 350, 10, 50, 5, (0, 0, 0))
class Ball:
def __init__(self, x, y, color, size):
self.x = x
self.y = y
self.color = color
self.size = size
self.direction = [random.randint(0, 1), random.randint(0, 1)]
self.speed = [0.3, random.uniform(0.2, 0.2)]
def draw(self, window):
pygame.draw.rect(window, self.color, (self.x, self.y, self.size, self.size))
def switchDirection(self):
self.direction[0] = not self.direction[0]
self.direction[1] = not self.direction[1]
self.speed = [0.2, random.uniform(0.1, 0.5)]
def bounce(self):
self.direction[1] = not self.direction[1]
self.speed = [0.2, random.uniform(0.01, 0.2)]
def move(self):
if self.direction[0]:
self.x += self.speed[0]
else:
self.x -= self.speed[0]
if self.direction[1]:
self.y += self.speed[1]
else:
self.y -= self.speed[1]
def getRect(self):
return pygame.Rect(self.x, self.y, self.size, self.size)
def boundaries(self):
if ball.x <= 10:
self.switchDirection()
if ball.x + self.size >= 800:
self.switchDirection()
if ball.y + self.size >= 490:
self.bounce()
if ball.y <= 0:
self.bounce()
ball = Ball(400, 250, (255, 0, 0), 20)
while True:
pygame.event.get()
D.fill((255, 255, 255))
ball.draw(D)
ball.boundaries()
ball.move()
#print(ball.x, ball.y)
left_paddle.draw(D)
right_paddle.draw(D)
right_paddle.move(0.4)
left_paddle.move(0.4, down=pygame.K_s, up=pygame.K_w)
if left_paddle.getRect().colliderect(ball.getRect()):
ball.switchDirection()
if right_paddle.getRect().colliderect(ball.getRect()):
ball.switchDirection()
WIN.flip()
I basically coded every possible movement of the ball when it bounces on the edge. I spent hours working on it, and I got it to work, I was just wondering if there was a more efficient way to produce a similar output with my code (coz I know this is supposed to be a beginner project)?
and
How can I make the ball spawn at random directions? at the start (and restart) of every round?
Rather than implementing "every" direction of the ball you can use float coordinates. these variables are usualy called dx and dy.
This way getting a random or reversed direction for your ball is simple, just use random or inverse values for dx and dy.
Th update for your ball should then look like:
def update(self. dt):
self.x += self.dx * self.speed * time_elapsed
self.y += self.dy * self.speed * time_elapsed # Time elasped is often called dt.
Every time the ball bounces past the edge, the score increments for a split second, and it goes back to 0 every time the ball respawns.
See Rabid76's answer. Ideally you should have a GameState object with scores, lives and other stuff as attributes.
Some food for thought on how to reduce all that ball movement/collision code, and make things a bit more reusable:
import pygame
class Ball:
def __init__(self, bounds, color):
from random import randint, choice
self.bounds = bounds
self.position = pygame.math.Vector2(
randint(self.bounds.left, self.bounds.left+self.bounds.width),
randint(self.bounds.top, self.bounds.top+self.bounds.height)
)
self.velocity = pygame.math.Vector2(choice((-1, 1)), choice((-1, 1)))
self.color = color
self.size = 8
def draw(self, window):
pygame.draw.rect(
window,
self.color,
(
self.position.x-self.size,
self.position.y-self.size,
self.size*2,
self.size*2
),
0
)
def update(self):
self.position.x += self.velocity.x
self.position.y += self.velocity.y
if not self.bounds.left+self.size < self.position.x < self.bounds.left+self.bounds.width-self.size:
self.velocity.x *= -1
if not self.bounds.top+self.size < self.position.y < self.bounds.top+self.bounds.height-self.size:
self.velocity.y *= -1
def main():
from random import randint
window_width, window_height = 800, 500
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Pong")
clock = pygame.time.Clock()
black = (0, 0, 0)
white = (255, 255, 255)
padding = 20
bounds = pygame.Rect(padding, padding, window_width-(padding*2), window_height-(padding*2))
ball = Ball(bounds, white)
def redraw_window():
window.fill(black)
pygame.draw.rect(
window,
white,
(
padding,
padding,
window_width-(padding*2),
window_height-(padding*2)
),
1
)
ball.draw(window)
pygame.display.update()
while True:
clock.tick(60)
ball.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
break
else:
redraw_window()
continue
break
pygame.quit()
return 0
if __name__ == "__main__":
import sys
sys.exit(main())
This isn't a complete drop-in replacement, I've just reimplemented the Ball class. There are no paddles. Essentially, when instantiating a ball, you pass in a pygame.Rect, which describes the bounds in which the ball is allowed to bounce around in. You also pass in a color tuple. The ball then picks a random position within the bounds (the position is a pygame.math.Vector2, as opposed to storing x and y as separate instance variables). A ball also has a velocity, which is also a pygame.math.Vector2, so that you may have independent velocity components - one for x (horizontal velocity) and one for y (vertical velocity). The size of a ball simply describes the dimensions of the ball. If the size is set to 8, for example, then a ball will be 16x16 pixels.
The Ball class also has an update method, which is invoked once per game-loop iteration. It moves the ball to the next position dictated by the velocity, and checks to see if the ball is colliding with the bounds. If it is, reverse the corresponding velocity component.

Why is the program not stopping after the main loop is stopped?

I am trying to make my own version of Snake. I'm trying to set it up so that when a wall is hit, the main while loop stops running by setting run = False. This works when I close the program with the quit button, but for some reason is not working when a collision is detected. Any help?
Note: I did try moving the gameOver() function out of the updateScreen() function and putting it separately at the end of the loop, but that didn't work either.
import pygame
import time
import os
pygame.init()
width, height = (500, 500)
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake")
win.fill((0, 0, 0))
# Snake Class
class Snake:
def __init__(self, x, y, width, height, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.len = []
self.xvel = 0
self.yvel = 0
def draw(self, window):
win.fill((0, 0, 0))
pygame.draw.rect(window, self.color, (self.x, self.y, self.width, self.height))
def move(self):
self.x += self.xvel
self.y += self.yvel
def checkCol(self):
# Check if edge is hit
if self.x <= 0 or self.x + self.width >= width:
return True
elif self.y <= 0 or self.y + self.height >= height:
return True
else:
return False
sn = Snake(100, 100, 15, 15, (0, 255, 0))
# MAIN LOOP
def main():
run = True
FPS = 15
clock = pygame.time.Clock()
fontStyle = pygame.font.SysFont(None, 50)
def updateScreen():
sn.move()
sn.draw(win)
gameOver()
pygame.display.update()
def gameOver():
if sn.checkCol():
run = False
print("check")
def message(msg, color):
m = fontStyle.render(msg, True, color)
win.blit(m, [width / 2, height / 2])
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
sn.yvel = -10
sn.xvel = 0
if keys[pygame.K_s]:
sn.yvel = 10
sn.xvel = 0
if keys[pygame.K_a]:
sn.xvel = -10
sn.yvel = 0
if keys[pygame.K_d]:
sn.xvel = 10
sn.yvel = 0
updateScreen()
message("Game Over!", (255, 0, 0))
main()
use breakwhen your condition is met
if keys[pygame.K_d]:
sn.xvel = 10
sn.yvel = 0
break

Pygame Class Variables

Im making a flappy bird clone game in pygame and im using classes, because I want to learn how to use classes.
I created 2 classes a "FlappyBird" and "Pipes" class.
The problem is with the collision. The variable BIRD_Y which is passed into the Pipes class from the MainWindow class and the variable BIRD_Y is chaning its value, but the problem here is that the variable does not change in the Pipes class! I tried with inheritance, still no luck.
I'm sorry if this is like the easy question but I don't have the practice and experience in pygame classes.
import pygame
pygame.init()
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("FlapPY Bird")
clock = pygame.time.Clock()
# colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# --> variables
FPS = 60
# classes
class MainWindow(object):
def __init__(self, w, h):
self.width = w
self.height = h
self.Main()
def Main(self):
loop = True
bird_width = 25
bird_height = 25
bird_x = 150
bird_y = HEIGHT/2 - int(bird_height/2)
bird_x_move = 0
bird_y_move = 0
bird = FlappyBird(bird_x, bird_y, bird_width, bird_height)
pipe_spacing = 350
pipe_speed = 3
space = 100
p1_x = 300
p1_y = 250
p1_w = 50
p1_h = HEIGHT
p2_x = p1_x + pipe_spacing
p2_y = 250
p2_w = 50
p2_h = HEIGHT
p3_x = p2_x + pipe_spacing
p3_y = 250
p3_w = 50
p3_h = HEIGHT
pipe1 = Pipes(p1_x, p1_y, p1_w, p1_h, space)
pipe2 = Pipes(p2_x, p2_y, p2_w, p2_h, space)
pipe3 = Pipes(p3_x, p3_y, p3_w, p3_h, space)
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird_y_move = -7
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
bird_y_move = 3
screen.fill(white)
bird.draw()
bird.move(bird_x_move, bird_y_move)
pipe1.draw_pipes()
pipe2.draw_pipes()
pipe3.draw_pipes()
pipe1.pipe_move(pipe_speed)
pipe2.pipe_move(pipe_speed)
pipe3.pipe_move(pipe_speed)
pipe1.check_if(bird_y)
pipe2.check_if(bird_y)
pipe3.check_if(bird_y)
pygame.display.update()
clock.tick(FPS)
class FlappyBird(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
def draw(self):
pygame.draw.rect(screen, red, (self.x, self.y, self.width, self.height))
def move(self, dx, dy):
self.y += dy
self.x += dx
class Pipes(object):
def __init__(self, x, y, width, height, space):
self.x = x
self.y = y
self.width = width
self.height = height
self.space = space
def draw_pipes(self):
pygame.draw.rect(screen, black, (self.x, self.y, self.width, self.height))
pygame.draw.rect(screen, black, (self.x, self.y-self.space-self.height, self.width, self.height))
def pipe_move(self, speed):
self.x -= speed
def check_if(self, bird_y):
if self.x < 0:
self.x = 1000
print(bird_y)
MainWindow(WIDTH, HEIGHT)
The variable you are giving to Pipe.check_if() is not updated.
You should get the real coordinate used by the bird, you can do this by calculate the new value before update the bird position (and give directly the new position, new_x and new_y).
The other solution is to add a return statement in the method FlappyBird.move() with the new coordinates, so when you call bird.move(bird_x_move, bird_y_move), you can assign the returned value to new_x and new_y.
# class bird
def move(self, dx, dy):
self.y += dy
self.x += dx
return(self.x, self.y)
# class MainWindow
new_x, new_y = bird.move(bird_x_move, bird_y_move)
#[...]
pipe1.check_if(new_y)
pipe2.check_if(new_y)
pipe3.check_if(new_y)

Drawing images onto Pygame's background or screen

I am writing a game with pygame which involves two player-controlled tanks which go around a brick map with their own health bars and shoot each other with one kind of bullet. From what I can tell, everything else is working, but I am having trouble determining which surface to draw the bricks, tanks, and bullets on: the "background" or the "screen" (the bricks do not change during the game, only the tanks and bullets). I tried drawing everything but the health bars on the background, but that just resulted in a black screen which only displayed the health bars. I then drew everything directly onto the screen and that displays everything correctly at first, but when the tanks move around, the screen doesn't refresh (I get many many tank images overlapping each other as the tank moves) and the bullets do not fire properly as a result. The code is below but the main() method is at the very bottom and that is what is causing errors. The "effects.py" file that is imported at the very top contains just the bullets and "booms" and "bombs" classes which are just special effects but aren't used so far in the main() method so they can be ignored.
from __future__ import print_function, division
import pygame, os, sys
from pygame.locals import *
import random
import effects
from effects import *
import math
GRAD = math.pi / 180
class Brick(pygame.sprite.Sprite):
def __init__(self, pos, image, top, bottom, right, left, bricks):
pygame.sprite.Sprite.__init__(self)
self.rect = image.get_rect(topleft = pos)
self.image = image
self.pos = pos
self.top = top
self.bottom = bottom
self.right = right
self.left = left
self.health = 30
bricks.add(self)
class City(object):
def __init__(self, bricks, level):
self.level = level
self.city = self.level.convert_alpha()
self.brick = pygame.image.load("brick.png").convert_alpha()
self.bricks = bricks
self.x = self.y = 0
collidable = (255, 0, 0, 255)
self.height = self.city.get_height()
self.width = self.city.get_width()
self.vehicle_pos = (0,0)
while self.y < self.height:
color = self.city.get_at((self.x, self.y))
collidable = (255, 0, 0, 255), (0,0,0,255)
top = False
bottom = False
right = False
left = False
if color in collidable:
self.bricks.add(Brick((self.x*30, self.y*30), self.brick, top, bottom, right, left, self.bricks))
print ("brick added!")
print (self.x, self.y)
self.x += 1
if self.x >= self.width:
self.x = 0
self.y += 1
def get_size(self):
return [self.city.get_size()[0]*30, self.city.get_size()[1]*30]
class Tank(pygame.sprite.Sprite):
book = {} # a book of tanks to store all tanks
number = 0 # each tank gets his own number
firekey = (pygame.K_SPACE, pygame.K_RETURN)
forwardkey = (pygame.K_w, pygame.K_i)
backwardkey = (pygame.K_s, pygame.K_k)
tankLeftkey = (pygame.K_a, pygame.K_j)
tankRightkey = (pygame.K_d, pygame.K_l)
color = ((200,200,0), (0,0,200))
def __init__(self, pos, angle, health):
self.number = Tank.number
Tank.number += 1
Tank.book[self.number] = self
pygame.sprite.Sprite.__init__(self)
self.tank_pic = pygame.image.load("tank.png").convert_alpha()
self.image = self.tank_pic
self.image_type = self.tank_pic
self.tank1_pic = pygame.image.load("tank1.png").convert_alpha()
self._image = self.image
self.rect = self.image.get_rect()
self.rect = self.rect.move(pos)
self.tankAngle = angle # tank facing
#---handles controls---#
self.firekey = Tank.firekey[self.number] # main gun
self.forwardkey = Tank.forwardkey[self.number] # move tank
self.backwardkey = Tank.backwardkey[self.number] # reverse tank
self.tankLeftkey = Tank.tankLeftkey[self.number] # rotate tank
self.tankRightkey = Tank.tankRightkey[self.number] # rotat tank
self.health = health
self.alive = True
self.speed = 5
self.angle = angle
self.timer = 3
self.timerstart = 0
self.x, self.y = self.rect.center
self.bullet_s = pygame.mixer.Sound("bullet.wav")
self.bullet_s.set_volume(.25)
def rotate(self):
center = self.rect.center
self.image = pygame.transform.rotozoom(self._image, self.angle, 1.0)
self.rect = self.image.get_rect(center = center)
def update(self, keys, bricks, bullets, booms, bombs):
self.bricks = bricks
self.t = True
self._rect = Rect(self.rect)
self._rect.center = self.x, self.y
self.rotate()
turn_speed = 3
pressedkeys = pygame.key.get_pressed()
if pressedkeys[self.forwardkey]:
self.x += sin(radians(self.angle))*-self.speed
self.y += cos(radians(self.angle))*-self.speed
if pressedkeys[self.backwardkey]:
self.x += sin(radians(self.angle))*self.speed
self.y += cos(radians(self.angle))*self.speed
if pressedkeys[self.tankLeftkey]:
self.angle += turn_speed
if pressedkeys[self.tankRightkey]:
self.angle -= turn_speed
if keys[self.firekey]:
if self.timer >= 3:
self.timer = self.timerstart
self.b_size = "small"
bullets.add(Bullet(self.rect.center, self.angle, self.b_size, "vehicle"))
self.bullet_s.play()
if self.timer < 3:
self.timer += 1
if self.angle > 360:
self.angle = self.angle-360
if self.angle <0:
self.angle = self.angle+360
self.rect.center = self.x, self.y
x = self.rect.centerx
y = self.rect.centery
_x = self._rect.centerx
_y = self._rect.centery
for b in bricks:
if self.rect.colliderect(b.rect):
if _x+21 <= b.rect.left and x+21 > b.rect.left:
if b.left == True:
self.x = b.rect.left-21
if _x-21 >= b.rect.right and x-21 < b.rect.right:
if b.right == True:
self.x = b.rect.right+21
if _y+21 <= b.rect.top and y+21 > b.rect.top:
if b.top == True:
self.y = b.rect.top-21
if _y-21 >= b.rect.bottom and y-21 < b.rect.bottom:
if b.bottom == True:
self.y = b.rect.bottom+21
for b in bullets:
if self.rect.colliderect(b.rect):
b_size = b.get_size()
pygame.sprite.Sprite.kill(b)
if b_size == "small":
booms.add(Boom(b.rect.center, "small"))
self.health -= 1
if b_size == "big":
booms.add(Boom(b.rect.center, "big"))
self.health -=5
for b in bombs:
if self.rect.colliderect(b.rect) and b.timer == 20:
self.health -=5
if self.health <= 0:
booms.add(Boom(self.rect.center, "huge"))
self.alive = False
self.health = 0
'''if self.image_type == self.tank_pic:
self.image = self.tank1_pic
self.image_type = self.tank1_pic
print "switch 1"
if self.image_type == self.tank1_pic:
self.image = self.tank_pic
self.image_type = self.tank_pic
print "switch 2"'''
class Turret(pygame.sprite.Sprite):
def __init__(self, pos, angle, follow):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("turret.png").convert_alpha()
self.timer = 40
self.timer_start = self.timer
self.size = "big"
self.bang_s = pygame.mixer.Sound("bang.wav")
self.speed = 3
self.bang_s.set_volume(1.0)
self._image = self.image
self.rect = self.image.get_rect()
self.rect = self.rect.move(pos)
self.angle = angle
self.timer = 40
self.wait_timer = 5
self.timer_restart = 0
self.x, self.y = self.rect.center
self.follow = follow
def rotate(self):
center = self._rect.center
self.image = pygame.transform.rotozoom(self._image, self.angle, 1.0)
self.rect = self.image.get_rect(center = center)
def update(self, pos, mx, my, keys, booms, tank_angle, bricks, background, city):
self.background = background
self.city_size = city
self.t = True
self.bricks = bricks
self.end = None
self._rect = Rect(self.rect)
self._rect.center = pos
self.tank_angle = tank_angle
t_x, t_y = self.rect.center
if keys[K_m]:
if self.wait_timer >= 5:
self.follow = not self.follow
self.wait_timer = self.timer_restart
if self.follow:
self.mouse_angle = math.atan2(xd, yd)*(180/math.pi)+180 #used atan2(x,y) instead of atan2(y,x). Sprite was origanly drawn along the y axis, gave better results
if self.angle < self.mouse_angle:
if math.fabs(self.angle - self.mouse_angle) < 180:
self.angle +=self.speed
else:
self.angle -=self.speed
else:
if math.fabs(self.angle - self.mouse_angle) < 180:
self.angle -=self.speed
else:
self.angle +=self.speed
if math.fabs(self.angle - self.mouse_angle) < self.speed+.5:
self.angle = self.mouse_angle
if not self.follow:
if self.angle != self.tank_angle:
if self.angle < self.tank_angle:
if math.fabs(self.angle - self.tank_angle) < 180:
self.angle +=self.speed
else:
self.angle -=self.speed
else:
if math.fabs(self.angle - self.tank_angle) < 180:
self.angle -=self.speed
else:
self.angle +=self.speed
if math.fabs(self.angle - self.tank_angle) < self.speed+.5:
self.angle = self.tank_angle
else:
self.angle = self.tank_angle
self.rotate()
if self.angle > 360:
self.angle = self.angle-360
if self.angle <0:
self.angle = self.angle+360
if self.wait_timer < 5:
self.wait_timer += 1
# ---------- END OF CLASSES ---------- #
def main():
pygame.init()
version = "Tank Wars 2.0"
screen = pygame.display.set_mode((1170, 510),0,32)
n = 1
size = screen.get_size()
pygame.mouse.set_visible(False)
map_ = pygame.image.load("c2.png")
health = 40
health_full = health
bricks= pygame.sprite.Group()
bricks_des = pygame.sprite.Group()
bricks_non = pygame.sprite.Group()
bullets = pygame.sprite.Group()
booms = pygame.sprite.Group()
bombers = pygame.sprite.Group()
bombs = pygame.sprite.Group()
tanks = pygame.sprite.Group()
allgroup = pygame.sprite.LayeredUpdates()
#assign default groups to each sprite class
#Tank.groups = tanks, allgroup
#Turret.groups = allgroup
#Bullet.groups = bullets, allgroup
city = City(bricks, map_)
city_size = city.get_size()
clock = pygame.time.Clock()
timer = 0
chance = None
score = 0
player1 = Tank((150, 250), 360, 40)
tanks.add(player1)
player2 = Tank((1100, 250), 360, 40)
tanks.add(player2)
player1_turret = Turret((150, 250), 360, False)
player2_turret = Turret((1100, 250), 360, False)
background = pygame.Surface((city_size), 0, 32)
city.bricks.draw(screen)
font4 = pygame.font.Font("7theb.ttf", 13)
font5 = pygame.font.SysFont("Courier New", 16, bold=True)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
return
clock.tick(24)
time_passed = clock.tick()
keys = pygame.key.get_pressed()
m_x, m_y = pygame.mouse.get_pos()
background.fill((87, 87, 87))
if player1.alive == True:
player1.update(keys, bricks, bullets, booms, bombs)
player1_turret.update(player1.rect.center, m_x, m_y, keys, booms, 360, bricks, background, city_size)
screen.blit(player1.image, player1.rect)
screen.blit(player1_turret.image, player1_turret.rect)
if player2.alive == True:
player2.update(keys, bricks, bullets, booms, bombs)
player2_turret.update(player2.rect.center, m_x, m_y, keys, booms, 360, bricks, background, city_size)
screen.blit(player2.image, player2.rect)
screen.blit(player2_turret.image, player2_turret.rect)
bullets.update(bricks, booms)
bombs.update(booms, player1)
bombs.update(booms, player2)
booms.update(background)
bombs.draw(screen)
bullets.draw(screen)
bombers.draw(screen)
healthshow = font4.render('Health ', False, (255,255,255))
#---Player 1 Healthbar---#
pygame.draw.ellipse(screen, (255, ((player1.health*255)/health_full),0), (90, 20, 10, 13))
pygame.draw.ellipse(screen, (255, ((player1.health*255)/health_full),0), (92+(100*(float(player1.health)/float(health_full))), 20, 10, 13))
screen.fill((255,((player1.health*255)/health_full),0),(96,20,(100*(float(player1.health)/float(health_full))), 13))
screen.blit(healthshow, (5, 20))
#---Player 2 Healthbar---#
pygame.draw.ellipse(screen, (255, ((player2.health*255)/health_full),0), (90, 20, 10, 13))
pygame.draw.ellipse(screen, (255, ((player2.health*255)/health_full),0), (92+(100*(float(player2.health)/float(health_full))), 20, 10, 13))
screen.fill((255,((player2.health*255)/health_full),0),(96,20,(100*(float(player2.health)/float(health_full))), 13))
screen.blit(healthshow, (500, 20))
allgroup.clear(screen, background)
pygame.display.flip()
if __name__ == "__main__":
main()
My suggestion is to make a global screen variable (instead of being wrapped inside a function, and generate a single city image, with the bricks added to it. Since those are all the components that do not change, this can be blitted to the screen every time the frame is refreshed.
After that, on each frame, the tank (with health bar and other stuff) and bullets can be blitted directly to the screen variable.
It is probably simplest to just do pygame.display.flip() after all of the blitting is done, but keeping an array of pygame.Rects representing blit positions and then keeping the old blit Rects and then doing something like:
old_rects = current_rects
current_rects = []
#make blits, collect Rects in current_rects
pygame.display.update(current_rects + old_rects)
This way, pygame.display.update() can be used (since it is more efficient), and there won't be problems with no refreshing.
I hope this works and that you can implement something like this.

Categories

Resources