Use collidelist in class - python

I have created a class to create rectangles and put them in a list . I don't want them to collide so I use collidelist but it isn't working.rectangles are still colliding .
I also want rectangles to move down and change x position when hit a specific point ,
I can do that but I am not sure if it is preventing collidelist from working
Look the code below for more clarification.
import pygame
import random
from pygame.locals import *
import time
pygame.init()
a = 255,255,255
b = 0,0,0
c = 80,255,0
d = 0,125,125
r1 = (b,c,d)
r = random.choice(r1)
p_x = 500
p_y = 1399
width = 500
height = 1890
display = pygame.display.set_mode((width,height))
title = pygame.display.set_caption("Game")
clock = pygame.time.Clock()
run = False
exit_game = False
x = random.randrange(10,900)
y = random.randrange(10,900)
sy = 10
w = random.randrange(40,90)
h = random.randrange(40,90)
rectangles =[]
class Rectangle:
def __init__(self ,color ,x,y,w,h):
self.c = color
self.x = x
self.y = y
self.w = w
self.h = h
self.rect =pygame.Rect(self.x ,self.y ,self.w ,self.h)
def draw(self):
pygame.draw.rect(display , self.c ,(self.x ,self.y ,self.w,self.h))
self.y += sy
if self.y > height:
self.y = -25
self.x = random.randint(10,900)
return self.rect
return self.rect
for count in range(5):
r_c = random.randint(0,255) , random.randint(0,255) , random.randint(0,255)
r_x = random.randint(10,900)
r_y = random.randint(10,79)
r_w = random.randint(60,100)
r_h = random.randint(40,80)
rectangle = Rectangle(r_c ,r_x,r_y,r_w,r_h)
rectangles.append(rectangle)
while not run:
display.fill(a)
p =pygame.draw.rect(display,c,(p_x ,p_y ,56,56))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
p_x -= 60
if event.key == pygame.K_p:
p_x += 60
for rectangle in rectangles:
if rectangle.rect.collidelist(rectangles) > -1:
rectangle.draw()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()

collidelist() evaluates the collisions between a singe pygame.Rect object and a list of pygame.Rect objects.
Remove the attributes .x, .y, .w and .h from the class, but add an new method update:
class Rectangle:
def __init__(self, color, x, y, w, h):
self.c = color
self.rect = pygame.Rect(x, y, w, h)
def update(self):
self.rect.y += sy
if self.rect.y > height:
self.rect.y = -25
self.rect.x = random.randint(10,900)
def draw(self):
pygame.draw.rect(display, self.c, self.rect)
Before the collision test you have to generate a list of pygame.Rect objects. Since each rectangle is in the list, the collision test will always find at leas one rectangle (itself). Use collidelistall() and test if the number of colliding rectangles is less than 2:
while not run:
# [...]
for rectangle in rectangles:
rectangle.update()
rectlist = [r.rect for r in rectangles]
if len(rectangle.rect.collidelistall(rectlist)) < 2:
rectangle.draw()
Anyway, I recommend to create rectangles, which are not intersecting. At initialization:
rectangles = []
rectlist = []
for count in range(5):
r_c = random.randint(0,255) , random.randint(0,255) , random.randint(0,255)
create_new = True
while create_new:
r_x = random.randint(10,900)
r_y = random.randint(10,79)
r_w = random.randint(60,100)
r_h = random.randint(40,80)
rectangle = Rectangle(r_c, r_x,r_y,r_w,r_h)
create_new = rectangle.rect.collidelist(rectlist) > -1
rectangles.append(rectangle)
rectlist.append(rectangle.rect)
And in the method update of the class Rectangle:
class Rectangle:
# [...]
def update(self, rectangles):
self.rect.y += sy
if self.rect.y > height:
rectlist = [r.rect for r in rectangles if r != self]
self.rect.y = -25
self.rect.x = random.randint(10,900)
while self.rect.collidelist(rectlist) > -1:
self.rect.x = random.randint(10,900)
Complete Example:
import pygame
import random
from pygame.locals import *
import time
pygame.init()
a = 255,255,255
b = 0,0,0
c = 80,255,0
d = 0,125,125
r1 = (b,c,d)
r = random.choice(r1)
p_x = 500
p_y = 1399
width = 500
height = 1890
display = pygame.display.set_mode((width,height))
title = pygame.display.set_caption("Game")
clock = pygame.time.Clock()
run = False
exit_game = False
sy = 10
class Rectangle:
def __init__(self, color, x, y, w, h):
self.c = color
self.rect = pygame.Rect(x, y, w, h)
def update(self, rectangles):
self.rect.y += sy
if self.rect.y > height:
rectlist = [r.rect for r in rectangles if r != self]
self.rect.y = -25
self.rect.x = random.randint(10,900)
while self.rect.collidelist(rectlist) > -1:
self.rect.x = random.randint(10,900)
def draw(self):
pygame.draw.rect(display, self.c, self.rect)
rectangles = []
rectlist = []
for count in range(5):
r_c = random.randint(0,255) , random.randint(0,255) , random.randint(0,255)
create_new = True
while create_new:
r_x = random.randint(10,900)
r_y = random.randint(10,79)
r_w = random.randint(60,100)
r_h = random.randint(40,80)
rectangle = Rectangle(r_c, r_x,r_y,r_w,r_h)
create_new = rectangle.rect.collidelist(rectlist) > -1
rectangles.append(rectangle)
rectlist.append(rectangle.rect)
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
p_x -= 60
if event.key == pygame.K_p:
p_x += 60
for rectangle in rectangles[:]:
rectangle.update(rectangles)
display.fill(a)
p = pygame.draw.rect(display,c,(p_x ,p_y ,56,56))
for rectangle in rectangles:
rectangle.draw()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()

Related

Rotate a rectangle over waves

I am trying to move a rectangle over the waves,trying to simulate a boat sailing.
For that, I rotate the rectangle using the height of the drawn lines and calculating the angle they form with the rectangle.
However, for some reason, in some points of the waves the rectangle is flickering.
The code shows the problem.
import sys
import math
import pygame
from pygame.locals import *
pygame.init()
SCREEN_WIDTH = 900
SCREEN_HEIGHT = 900
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
BLUE = (0, 103, 247)
RED = (255,0,0)
watter_levels = [0 for _ in range(SCREEN_WIDTH)]
def draw_water(surface, dy):
amplitude = 35
global watter_levels
for x in range(SCREEN_WIDTH):
y = int(math.sin((x)*(0.01)) * amplitude + dy)
watter_levels[x] = y
pygame.draw.aaline(surface,BLUE,(x,y),(x,y))
def get_water_level(index):
if index <= 0:
return watter_levels[0]
if index >= SCREEN_WIDTH:
return watter_levels[-1]
return watter_levels[index]
font = pygame.font.Font(None,30)
def debug(info,x=10,y=10):
display_surf = pygame.display.get_surface()
debug_surface = font.render(str(info),True,'White')
debug_rect = debug_surface.get_rect(topleft=(x,y))
pygame.draw.rect(display_surf,'Black',debug_rect)
display_surf.blit(debug_surface,debug_rect)
class Ship(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((80,80),pygame.SRCALPHA)
self.image.fill('red')
self.rect = self.image.get_rect(topleft=(0,0))
self.copy_img = self.image.copy()
self.move = pygame.math.Vector2(0,0)
self.copy_img = self.image.copy()
self.velocity = 8
def rotate(self, angle):
self.image = pygame.transform.rotate(self.copy_img, int(angle))
self.rect = self.image.get_rect(center=(self.rect.center))
def update(self):
self.get_input()
self.rect.bottom = get_water_level(self.rect.centerx)
left_y = get_water_level(self.rect.left)
right_y = get_water_level(self.rect.right)
angle = 180*math.atan2(left_y-right_y,self.image.get_width())/math.pi
debug("angle: "+str(int(angle)))
print(self.rect.left,self.rect.right)
self.rotate(angle)
def replicate(self):
if self.rect.left == 363:
return
self.rect.x += 1
def get_input(self):
self.replicate()
# keys = pygame.key.get_pressed()
# if keys[K_LEFT]:
# self.move.x = -self.velocity
# elif keys[K_RIGHT]:
# self.move.x = self.velocity
# else:
# self.move.x = 0
# if keys[K_UP]:
# self.move.y = -self.velocity
# elif keys[K_DOWN]:
# self.move.y = self.velocity
# else:
# self.move.y = 0
# self.rect.x += self.move.x
# self.rect.y += self.move.y
# if self.rect.left <= 0:
# self.rect.left = 0
# if self.rect.right >= SCREEN_WIDTH:
# self.rect.right = SCREEN_WIDTH
ship_sprite = pygame.sprite.GroupSingle()
ship_sprite.add(Ship())
while True:
screen.fill((200,210,255,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
ship_sprite.update()
ship_sprite.draw(screen)
draw_water(screen,SCREEN_HEIGHT-300)
clock.tick(60)
pygame.display.update()
It's about accuracy. You can improve the result by storing the water level with floating point precision:
def draw_water(surface, dy):
amplitude = 35
global watter_levels
for x in range(SCREEN_WIDTH):
y = math.sin(x * 0.01) * amplitude + dy
watter_levels[x] = y
pygame.draw.aaline(surface, BLUE, (x, round(y)), (x, round(y)))
Furthermore you have to get the left and right from the "unrotated" rectangle:
class Ship(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((80,80),pygame.SRCALPHA)
self.image.fill('red')
self.rect = self.image.get_rect(topleft = (0, 0))
self.copy_img = self.image.copy()
self.copy_rect = pygame.Rect(self.rect)
self.move = pygame.math.Vector2(0, 0)
self.velocity = 8
def rotate(self, angle):
self.image = pygame.transform.rotate(self.copy_img, round(angle))
self.rect = self.image.get_rect(center = (self.copy_rect.center))
def update(self):
self.get_input()
self.copy_rect.bottom = round(get_water_level(self.copy_rect.centerx))
left_y = get_water_level(self.copy_rect.left)
right_y = get_water_level(self.copy_rect.right)
angle = math.degrees(math.atan2(left_y-right_y, self.copy_img.get_width()))
debug("angle: " + str(round(angle)))
print(self.copy_rect.left, self.copy_rect.right)
self.rotate(angle)
def replicate(self):
if self.copy_rect.left == 363:
return
self.copy_rect.x += 1

Pygame Curve Movement Problem How To Fix?

VIDEO < I'm trying to make the white rectangle curve slowly and smoothly towards the red rectangle and then stop but for some reason the white rectangle isn't curving right and its moving way to fast I want it to move smoothly and curve smoothly that the eyes can see and also alawys curve to the red rectangle is there a way i could change up my code and get it to work like that?
in my curvemove rect class I have a
self.yspeed = 0.05 self.xspeed = -0.5 self.gravity = -0.01 the x and y speed the rect will move in and the gravity effecting it
then on my redraw on the curvemove class
this is how my rect is moving if the key V is pressed then it should mave the self.move true after that it should run the code which curves the object
if keys[pygame.K_v]:
curve_move1.move = True
if self.move:
curve_move1.x += curve_move1.xspeed
curve_move1.y += curve_move1.yspeed
curve_move1.yspeed += curve_move1.gravity
# curve it towards the red rectangle
else:
curve_move1.move2 = True
class curvemove:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
self.yspeed = 0.05
self.xspeed = -0.5
self.gravity = -0.01
self.move = False
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
# -------------- curve_move1 is our curving rectangle
if keys[pygame.K_v]:
curve_move1.move = True
if self.move:
curve_move1.x += curve_move1.xspeed
curve_move1.y += curve_move1.yspeed
curve_move1.yspeed += curve_move1.gravity
# curve it towards the red rectangle
else:
curve_move1.move2 = True
full code its all rectangles so you can test it out
import pygame
pygame.init()
# our window
window = pygame.display.set_mode((500,500))
pygame.display.set_caption("test map")
# our class
class curvemove:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
self.yspeed = 0.05
self.xspeed = -0.5
self.gravity = -0.01
self.move = False
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
# -------------- curve_move1 is our curving rectangle
if keys[pygame.K_v]:
curve_move1.move = True
if self.move:
curve_move1.x += curve_move1.xspeed
curve_move1.y += curve_move1.yspeed
curve_move1.yspeed += curve_move1.gravity
# curve it towards the red rectangle
else:
curve_move1.move2 = True
white = 255,255,255
red = 205,0,10
curve_move1 = curvemove(250,400,50,50,white)
touched = curvemove(250,200,50,50,red)
# our game fps
fps = 60
clock = pygame.time.Clock()
# d redraw()
def redraw():
window.fill((0,0,0))
curve_move1.draw()
touched.draw()
# our main loop
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if curve_move1.rect.colliderect(touched.rect):
curve_move1.move = False
redraw()
pygame.display.update()
pygame.quit()
I want a way to make this rect smooth curve towards the red rectangle not fast but normal this is my best try
The gravitational force must act in the direction of the target.
I recommend to use pygame.math.Vector2 for the calculations. Calculate the direction vector from the object to the target and scale it to the size of the gravitational force. Change the motion vector depending on gravity in each frame:
dir_vec = pygame.math.Vector2(target_pos) - self.rect.center
v_len_sq = dir_vec.length_squared()
if v_len_sq > 0:
dir_vec.scale_to_length(self.gravity)
self.speed = (self.speed + dir_vec) * self.friction
self.pos += self.speed
Minimal example:
import pygame
pygame.init()
class curvemove:
def __init__(self, x, y, height, width, color):
self.pos = pygame.math.Vector2(x, y)
self.color = color
self.rect = pygame.Rect(x, y, height, width)
self.speed = pygame.math.Vector2(-5.0, 0)
self.gravity = 0.5
self.friction = 0.99
def draw(self):
self.rect.center = (self.pos.x, self.pos.y)
pygame.draw.circle(window, self.color, (self.pos.x, self.pos.y), self.rect.width//2)
def update(self, target_pos):
dir_vec = pygame.math.Vector2(target_pos) - self.rect.center
v_len_sq = dir_vec.length_squared()
if v_len_sq > 0:
dir_vec.scale_to_length(self.gravity)
self.speed = (self.speed + dir_vec) * self.friction
self.pos += self.speed
window = pygame.display.set_mode((500,500))
pygame.display.set_caption("test map")
clock = pygame.time.Clock()
white = 255, 255, 255
red = 205, 0, 10
curve_move1 = curvemove(250, 400, 20, 20, white)
touched = curvemove(250, 200, 20, 20, red)
fps = 60
move = False
def redraw():
window.fill((0,0,0))
curve_move1.draw()
touched.draw()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_v:
move = True
if event.key == pygame.K_r:
move = False
curve_move1 = curvemove(250, 400, 20, 20, white)
if (curve_move1.pos - touched.pos).length() < 10:
move = False
if move:
curve_move1.update(touched.rect.center)
redraw()
pygame.display.update()
clock.tick(fps)
pygame.quit()
exit()
Play around with the values of speed, gravity and friction to get different effects.
e.g.:
self.speed = pygame.math.Vector2(-10.0, 0)
self.gravity = 1
self.friction = 0.95
e.g.:
self.speed = pygame.math.Vector2(-10.0, 0)
self.gravity = 1
self.friction = 0.91

How do I get multiple balls to move randomly in pygame using self and other?

Ok so I am new to both Stack Overflow and Python and I am using Pygame to make a game where a bunch of randomly colored balls are spawned, and they will hopefully move randomly and when they collide will become bigger and take the "average" of the color by using the integers of the r g and b values. My current problem is that I have the balls spawned but I can not get the balls to move. I am trying to get them to move in an update function in my ball class. I want them to move randomly as they move without user input.
This is my main
import pygame
import random
import ballClass
WORLD_WIDTH = 800
WORLD_HEIGHT = 600
WIN = pygame.display.set_mode((WORLD_WIDTH, WORLD_HEIGHT))
paused = False
def main():
global numberOfBalls, WIN, paused
pygame.init()
clock = pygame.time.Clock()
running = True
WHITE = (255, 255, 255)
#ballClass.Ball().initialize(10)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
paused = not paused
if not paused:
WIN.fill(WHITE)
ballClass.Ball().draw(WIN)
ballClass.Ball().update(WIN)
#drawWorld(WIN)
clock.tick(1)
pygame.display.update()
def setInitialBallAttributes():
pass
#def checkForCollision(self): #iterate through list of objects and check each one against , tell it to do averaging
# ball1 = self.ball
# ball2 = self.ball
# ball3 = self.ball
# ball4 = self.ball
# ball5 = self.ball
# balls = [ball1, ball2, ball3, ball4, ball5]
# #from Mr. Sharick
# for i in range(len(balls)):
# for j in range(i, len(balls)): # start this loop at i+1 so you don't check collisions twice
# balls[i].collision(balls[j])
#def drawWorld(WIN, self):
# ball1 = self.ball
# ball2 = self.ball
# ball3 = self.ball
# ball4 = self.ball
# ball5 = self.ball
#pygame.draw.rect(WIN, 'black', width=0)
#pass
if __name__ == "__main__":
main()
and this is my ballClass
import pygame
import random
import math
balls = []
center1 = random.randint(0,600)
center2 = random.randint(0, 600)
balls.append((center1, center2))
class Ball():
WORLD_WIDTH = 600
WORLD_HEIGHT = 600
def __init__(self, centerX=None, centerY=None, direction=None, r=None, g=None, b=None, radius= None, speed= None):
self.centerX = 10
self.centerY = 20
self.r = r
self.g = g
self.b = b
self.direction = direction
self.radius = radius
self.speed = 3 #Set as default 3; change later
def initialize(self, num):
global balls
for i in range(num):
self.centerX = random.randint(0, 600)
self.centerY = random.randint(0,600)
balls.append((self.centerX, self.centerY))
def draw(self, WIN):
global ball, radius, centerX, centerY, r, g, b, balls
self.radius = 10
self.direction = random.randint(0, 600), random.randint(0, 600)
self.speed = 3
#self.centerX = random.randint(self.radius, 600 - self.radius)
#self.centerY = random.randint(self.radius, 600 - self.radius)
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
#self.ball = Ball(centerX, centerY, radius, WIN)
# needs all 8
for ball in balls:
pygame.draw.circle(WIN, ((r,g,b)), (ball[0],ball[1]),self.radius)
print(ball)
#balls.append((self.centerX, self.centerY))
# def collision(self, other): #pass in another ball object as another parameter
# distance = math.sqrt((self.centerX - self.centerY)^2 + (other.centerX - other.centerY)^2)
# if distance < self.radius + other.radius:
# (self.r + other.r) / 2 == self.r
# (self.g + other.g) / 2 == self.g
# (self.b + other.b) / 2 == self.b
# if self.radius > other.radius:
# self.radius = other.radius + self.radius
# other.radius = 0
# if other.radius > self.radius:
# other.radius = other.radius + self.radius
# self.radius = 0
# else:
# #self.radius = other.radius + self.radius
# self.centerX = (other.centerX + self.centerX) / 2
# self.centerY = (other.centerY + self.centerY) / 2
# other.radius = 0
def update(self, win):
WORLD_WIDTH = 600
WORLD_HEIGHT = 600
# if len(balls):
# for i in range(len(balls)):
# balls[i] = (center1+10, center2 +10)
newBallCoords
# if self.right >= WORLD_WIDTH or ball.left <= 0:
# self.direction = -self.centerX, self.centerX
# if self.bottom >= WORLD_HEIGHT or ball.top <= 0:
#self.direction = self.centerX, -self.centerX
I commented out stuff that was for the collision part... I am not there yet ad hoping to get the balls to move. I apologize in advance If i am using this page wrong as I am new but would greatly appreciate any help.
If you want the balls to move with their own independent directions, each ball should have it's own X and Y velocities which should be generated randomly upon initialization.
Looking at the program you have included I would recommend you consider separating the Ball class from the logic of controlling all the balls.
I have included a modified version of your program that includes this idea.
Main:
import pygame
import random
import math
import ball as ballClass
WORLD_WIDTH = 800
WORLD_HEIGHT = 600
def main():
paused = False
WIN = pygame.display.set_mode((WORLD_WIDTH, WORLD_HEIGHT))
pygame.init()
clock = pygame.time.Clock()
running = True
WHITE = (255, 255, 255)
balls = [ballClass.Ball() for _ in range(8)]
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
paused = not paused
if not paused:
WIN.fill(WHITE)
for ball in balls:
ball.draw(WIN)
ball.update(WIN)
clock.tick(1)
pygame.display.update()
if __name__ == "__main__":
main()
Ball
import random, pygame
class Ball():
WORLD_WIDTH = 600
WORLD_HEIGHT = 600
def __init__(self, centerX=None, centerY=None, direction=None, r=None, g=None, b=None, radius= None, speed= None):
self.centerX = random.randint(0, 600)
self.centerY = random.randint(0,600)
self.r = random.randint(0, 255)
self.g = random.randint(0, 255)
self.b = random.randint(0, 255)
self.velocityX = random.randint(-10, 10)
self.velocityY = random.randint(-10, 10)
self.radius = 10
def draw(self, WIN):
pygame.draw.circle(WIN, ((self.r,self.g,self.b)), (self.centerX, self.centerY), self.radius)
def update(self, win):
self.centerX += self.velocityX
self.centerY += self.velocityY
In the code I provided the Ball's update method only updates a single ball, with it's respective velocities, and the main program tells all the balls that they need to be updated.

How do you make the game end when the main player collides with an image (obstacle)?

import math
import random
import pygame
from pygame.locals import *
import sys
def events():
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
pygame.init()
W = 700
H = 400
updater = pygame.time.Clock()
display = pygame.display.set_mode((700, 400))
pygame.display.set_caption("Skating_Game")
x = y = 0
surface = pygame.image.load("man2.png")
pygame.display.set_icon(surface)
class player:
def __init__(self, velocity, maxJumpRange):
self.velocity = velocity
self.maxJumpRange = maxJumpRange
def setLocation(self, x, y):
self.x = x
self.y = y
self.xVelocity = 0
self.jumping = False
self.jumpCounter = 0
self.falling = True
def keys(self):
k = pygame.key.get_pressed()
if k[K_LEFT]:
self.xVelocity = -self.velocity
elif k[K_RIGHT]:
self.xVelocity = self.velocity
else:
self.xVelocity = 0
if k[K_SPACE] and not self.jumping and not self.falling:
self.jumping = True
self.jumpCounter = 0
def move(self):
self.x += self.xVelocity
if self.jumping:
self.y -= self.velocity
self.jumpCounter += 1
if self.jumpCounter == self.maxJumpRange:
self.jumping = False
self.falling = True
elif self.falling:
if self.y <= H - 60 and self.y + self.velocity >= H - 60:
self.y = H - 60
self.falling = False
else:
self.y += self.velocity
def draw(self):
display = pygame.display.get_surface()
character = pygame.image.load("man3.png").convert_alpha()
display.blit(character, (self.x, self.y - 100))
#pygame.draw.circle(display, (255, 255, 255), (self.x, self.y - 25), 25, 0)
def do(self):
self.keys()
self.move()
self.draw()
P = player(3, 50)
P.setLocation(350, 0)
BLACK = ( 0, 0, 0)
g=0
font = pygame.font.SysFont("Plump", 30)
obstacle = pygame.image.load("obstacle.png").convert_alpha()
background = pygame.image.load("Road.png").convert()
x = 0
while True:
events()
rel_x = x % background.get_rect().width
display.blit(background, (rel_x - background.get_rect().width,0))
if rel_x < 700:
display.blit(background, (rel_x, 0))
x -= 1
g += 0.01
pygame.draw.rect(display, (255,255,255,128), [rel_x, 275, 150, 50])
display.blit(obstacle, (rel_x, 250))
text = font.render("Score: "+str(int(g)), True, (255, 255, 255))
display.blit(text, (0,0))
P.do()
if P.rect.collidepoint(self.x,self.y):
pygame.quit()
pygame.display.update()
updater.tick(200)
So if the player collides with the obstacle image the game should stop. How do i do this? I have made a class for the player and the obstacle is just an image which is constantly moving.
I was thinking maybe I could track the x and y coordinate of the player and obstacle and when their radius overlaps the game could stop.
Here's a working (simplified) version of your program with some comments. You have to create rects for the obstacle and the player and then check if the rects collide with the help of the colliderect method.
import sys
import pygame
from pygame.locals import *
pygame.init()
W = 700
H = 400
updater = pygame.time.Clock()
display = pygame.display.set_mode((700, 400))
PLAYER_IMAGE = pygame.Surface((30, 50))
PLAYER_IMAGE.fill(pygame.Color('dodgerblue1'))
class Player:
def __init__(self, x, y, velocity, maxJumpRange):
self.velocity = velocity
self.maxJumpRange = maxJumpRange
self.image = PLAYER_IMAGE # Give the player an image.
# Create a rect with the size of the PLAYER_IMAGE and
# pass the x, y coords as the topleft argument.
self.rect = self.image.get_rect(topleft=(x, y))
self.x = x
self.y = y
self.xVelocity = 0
self.jumping = False
self.jumpCounter = 0
self.falling = True
def keys(self):
k = pygame.key.get_pressed()
if k[K_LEFT]:
self.xVelocity = -self.velocity
elif k[K_RIGHT]:
self.xVelocity = self.velocity
else:
self.xVelocity = 0
if k[K_SPACE] and not self.jumping and not self.falling:
self.jumping = True
self.jumpCounter = 0
def move(self):
self.x += self.xVelocity
if self.jumping:
self.y -= self.velocity
self.jumpCounter += 1
if self.jumpCounter == self.maxJumpRange:
self.jumping = False
self.falling = True
elif self.falling:
if self.y >= H - 160: # Simplified a little.
self.y = H - 160
self.falling = False
else:
self.y += self.velocity
# Update the position of the rect, because it's
# used for the collision detection.
self.rect.topleft = self.x, self.y
def draw(self, display):
# Just draw the image here.
display.blit(self.image, (self.x, self.y))
def do(self):
self.keys()
self.move()
player = Player(350, 0, 3, 50)
obstacle = pygame.Surface((150, 50))
obstacle.fill(pygame.Color('sienna1'))
# Create a rect with the size of the obstacle image.
obstacle_rect = obstacle.get_rect()
g = 0
x = 0
FPS = 60 # Cap the frame rate at 60 or 30 fps. 300 is crazy.
while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
# --- Update the game ---
player.do()
rel_x = x % display.get_width()
x -= 7
g += 0.01
obstacle_rect.topleft = rel_x, 250 # Update the position of the rect.
# --- Draw everything ---
display.fill((30, 30, 30))
display.blit(obstacle, (rel_x, 250))
if g > 30:
display.blit(obstacle, (rel_x+350, 250))
# Check if the obstacle rect and the player's rect collide.
if obstacle_rect.colliderect(player.rect):
print("Game over!") # And call pygame.quit and sys.exit if you want.
# Draw the image/surface of the player onto the screen.
player.draw(display)
# Draw the actual rects of the objects (for debugging).
pygame.draw.rect(display, (200, 200, 0), player.rect, 2)
pygame.draw.rect(display, (200, 200, 0), obstacle_rect, 2)
pygame.display.update()
updater.tick(FPS)
Pygame rectangles include a collidepoint and colliderect method that allows you to check to see if something intersects with a rectangle. So you could have rectangles drawn beneath the obstacle and check to see if the player's coordinates intersect with the rectangle. Like this:
if self.rect.collidepoint(self.x,self.y):
pygame.quit()

Pygame Inquiry - How to have different sprites from different classes collide

So I have been searching for a long time online to try and find out how to get my two sprite classes in pygame to collide. I am trying to make a basic game where the player has to dodge the squares. I would like some code for when the player hits one of the squares gameOver is true. Here's the code the player.
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, image):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('Tri.png')
self.image = pygame.transform.scale (self.image, (int(width/16), int(width/15)))
self.rect = self.image.get_rect()
self.width, self.height = self.image.get_size()
self.rect.x = x
self.rect.y = y
def update(self):
mx, my = pygame.mouse.get_pos()
self.rect.x = mx - self.width/2
self.rect.y = (height * 0.8)
if self.rect.x <= 0 - self.width/2 + 10:
self.rect.x += 10
if self.rect.x + self.width >= width:
self.rect.x = width - self.width
def draw(self, screen):
if bgColour == black:
self.image = pygame.image.load('Tri2.png')
self.image = pygame.transform.scale (self.image, (int(width/16), int(width/15)))
else:
self.image = pygame.image.load('Tri.png')
self.image = pygame.transform.scale (self.image, (int(width/16), int(width/15)))
self.width, self.height = self.image.get_size()
gameDisplay.blit(self.image, self.rect)
Here's the code for the squares
class Square(pygame.sprite.Sprite):
def __init__(self, box_x, box_y, box_width, box_height,colour, box_speed, box_border, BC):
self.box_x = box_x
self.box_y = box_y
self.box_width = box_width
self.box_height = box_height
self.colour = colour
self.box_speed = box_speed
self.box_border = box_border
self.BC = BC
border = pygame.draw.rect(gameDisplay, self.BC, [self.box_x - self.box_border/2, self.box_y - self.box_border/2, self.box_width + self.box_border, self.box_height + self.box_border])
box = pygame.draw.rect(gameDisplay, self.colour, [self.box_x, self.box_y, self.box_width, self.box_height])
def Fall(self):
if self.box_y < height:
self.box_y += box_speed
elif self.box_y > height + 100:
del square[0]
border = pygame.draw.rect(gameDisplay, self.BC, [self.box_x - self.box_border/2, self.box_y - self.box_border/2, self.box_width + self.box_border, self.box_height + self.box_border])
box = pygame.draw.rect(gameDisplay, self.colour, [self.box_x, self.box_y, self.box_width, self.box_height])
And the main game loop. Sorry for the messy code and probably redundant variables but I'm still learning :)
def game_loop():
mx, my = pygame.mouse.get_pos()
x = mx
y = (height * 0.8)
player = Player(x, y, 'Tri.png')
box_width = int(width/15)
if round(box_width/5) % 10 == 0:
box_border = round(box_width/5)
else:
box_border = round(box_width/5 + 1)
box_x = random.randrange(0, width)
box_y = 0 - box_width
min_gap = box_width/4
global box_speed
box_col = False
box_start = random.randrange(0, width)
delay = 0
global square
square = []
move_speed = 10
#level variables
box_speed = 6
max_gap = box_width/2
score = 0
bgColourList = [white, black, white, white]
global bgColour
bgColour = bgColourList[0]
Blist = [red, green, black, pink, white]
BC = Blist[0]
Clist = [red, black, black, pink, white]
box_colour = red
text_colour = black
z = 60
level = 0
delayBG = 0
levelChange = 400
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameDisplay.fill(bgColour)
#blitting the player
player.update()
player.draw(gameDisplay)
#sets delay for level change
if score % levelChange == 0:
delayBG = 120
z = 120
if delayBG == 0:
bgColour = bgColourList[level]
BC = Blist[level]
box_colour = Clist[level]
if delay == 0:
score += 1
delay += 3
if delayBG == 0:
level += 1
box_speed += 1
max_gap -= 1
#creating a new square
if z == 0:
new = random.randint(0, width)
square.append(Square(new, box_y, box_width, box_width , box_colour, box_speed, box_border, BC))
z = random.randint(int(min_gap), int(max_gap))
last = new
lasty = box_y
#calling the Square.fall() function
for i in square:
i.Fall()
"""tris.remove(i)
i.checkCollision(tris)
tris.add(i)"""
pygame.draw.rect(gameDisplay, bgColour, [0,0, width, int(height/23)])
message_to_screen(str(score), text_colour, -height/2 + 15, 0)
delayBG -= 1
z -= 1
delay -= 1
pygame.display.update()
clock.tick(FPS)
game_loop()
pygame.quit()
quit()
Thank you in advance!
Create a group to hold all your Square objects:
square_group = pygame.sprite.Group()
Every time you create a Square object, add it to the group:
steven = Square(new, box_y, box_width, box_width , box_colour, box_speed, box_border, BC)
square_group.add(steven)
Then you can use spritecollide to check for collisions and act accordingly.
collisions = pygame.sprite.spritecollide(player, square_group, False)
if collisions:
gameExit = True

Categories

Resources