Why my object doesn't rotate continuously? - python

Error : So, I'm having a logic error.
I want my object called 'Claw' rotate (range : 10-170 degrees) back and forth.
But, somehow when Claw reached the angle of 170, the value of angle didn't decrease. It sticks at the angle of 170.
Here is my Claw class
class Claw(pygame.sprite.Sprite):
def __init__(self,image,position):
super().__init__()
self.original_image = image #saved original image(unchangeable)
self.image = image
self.rect = image.get_rect(center = position)
self.offset = pygame.math.Vector2(default_offset_claw_x, 0)
self.position = position
self.direction = LEFT
self.angle = 10 #the first angle
self.angle_speed = 2.5
def update(self):
# rect_center = self.position + self.offset #mid point
# self.rect = self.image.get_rect(center = rect_center)
if self.direction == LEFT: #to the left
self.angle += self.angle_speed
elif self.direction == RIGHT:
self.angle -= self.angle_speed
#when it reaches an edge
if self.angle > 170:
self.angle = 170
self.directon = RIGHT
elif self.angle < 10:
self.angle = 10
self.direction = LEFT
print(self.angle, self.direction)
**Here is my game main variables**
default_offset_claw_x = 40
LEFT = -1
RIGHT = 1
Here is my main
#claw initialize
claw_image = pygame.image.load(os.path.join(current_path, "img\\claw.png"))
claw = Claw(claw_image, (screen_width//2, 110))
#game loop
running = True
while running:
clock.tick(30) # FPS = 30
# Look at every event in the queue
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.blit(background,(0,0))
gemstone_group.draw(screen) #draw all sprites in gemstone_group
claw.update()
claw.draw(screen)
pygame.display.update() #display update
pygame.quit()

There is missing an i in directon. It has to be self.direction = RIGHT. Anyway you can simplify the code:
class Claw(pygame.sprite.Sprite):
# [...]
def update(self):
self.angle += self.angle_speed * self.direction
if self.angle > 170 or self.angle < 10:
self.anlge = max(10, min(170, self.angle))
self.direction *= -1

Related

Starting point for bullet including player rotation in Pygame

I'm making a simple topdown shooter game. The problem is with bullet starting point. I have player image where rifle barrel is between topmid and topright of rectangle. How to make bullet start always from the rifle barrel regardless image rotation?
# key input
def get_input(self, dt):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.direction.rotate_ip(dt * -360)
if keys[pygame.K_RIGHT]:
self.direction.rotate_ip(dt * 360)
self.angle = self.direction.angle_to((0, -1))
self.image = pygame.transform.rotate(self.image, self.angle)
self.rect = self.image.get_rect(center=self.rect.center)
if keys[pygame.K_UP]:
self.movement = 1
self.status = 'move'
else:
self.movement = 0
self.status = 'idle'
if keys[pygame.K_DOWN]:
self.movement = -1
self.status = 'move'
if keys[pygame.K_SPACE] and not self.bullet.sprites():
self.create_bullet(dt)
shoot = pygame.mixer.Sound('audio/rumble.flac')
shoot.play()
# movement
def move(self, speed, dt):
movement_v = self.direction * self.movement
if movement_v.length() > 0:
movement_v.normalize_ip()
self.pos += movement_v * dt * speed
self.rect = self.image.get_rect(center=self.pos)
# boundary
if self.rect.x <= 0:
self.rect.x = 0
if self.rect.x >= 1216:
self.rect.x = 1216
if self.rect.y <= 0:
self.rect.y = 0
if self.rect.y >= 704:
self.rect.y = 704
# create bullet instance
def create_bullet(self, dt):
self.bullet.add(Bullet(self.rect.center, self.direction.normalize(), self.bullet_speed, self.angle))
You have rotate the offset of the bullet starting point with pygame.math.Vector2.rotate. e.g.:
offset = pygame.math.Vecotr2(15, 10) # (15, 10) is just an example
rotated_offset = offset.rotate(-self.angle)
pos = pygame.math.Vector2(self.rect.center) + rotated_offset
bullet = Bullet((pos.x, pos.y), ...)

Problem with rounding in pygame, using rotate_ip function

i have made a simple car game in pygame, w for acceleration, a and d for steering. However, Ive noticed that after some rotational motion, my sprite is going sideways while the image displays as if its going straight.
By printing velocity vector I have noticed that after some rotation and translation, value is no longer 0, but e-13/14. Could this be the problem? And if yes, how do I round it up, so the image actually follows the vector?
My code so far:
import pygame
import os
import sys
pygame.init()
screen_height = 750
screen_width = 1500
screen = pygame.display.set_mode((screen_width,screen_height))
robot = pygame.Surface((100,81), pygame.SRCALPHA)
robot.fill((255, 0, 0))
class Robot(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.robot_image = robot
self.image = self.robot_image
self.rect = self.image.get_rect(center=(100,355))
self.velocity = pygame.math.Vector2(1,0)
self.angle = 0
self.rotation = 5
self.state = 0
self.direction = 0
def update(self):
self.drive()
self.rotate()
def drive(self):
if self.state == 1:
self.rect.center += self.velocity * 6
if self.state == -1:
self.rect.center -= self.velocity * 6
def rotate(self):
if self.direction == 1:
self.angle -= self.rotation
self.velocity.rotate_ip(self.rotation)
if self.direction == -1:
self.angle += self.rotation
self.velocity.rotate_ip(-self.rotation)
self.rect = self.image.get_rect(center=self.rect.center)
self.image = pygame.transform.rotozoom(self.robot_image,self.angle,1)
rob = pygame.sprite.GroupSingle(Robot())
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
input = pygame.key.get_pressed()
if sum(pygame.key.get_pressed()) <= 1:
rob.sprite.state = 0
rob.sprite.direction = 0
if input[pygame.K_w]:
rob.sprite.state = 1
if input[pygame.K_s]:
rob.sprite.state = -1
if input[pygame.K_d]:
rob.sprite.direction = 1
if input[pygame.K_a]:
rob.sprite.direction = -1
screen.fill(0)
rob.draw(screen)
rob.update()
pygame.display.update()
main()
As you accumulate the rotation of the vector, a floating point precision error accumulates over time. I suggest calculating the velocity vector from the angle.
self.velocity = pygame.math.Vector2(1,0)
self.velocity.rotate_ip(-self.angle)
Since pygame.Rect is supposed to represent an area on the screen, a pygame.Rect object can only store integral data. The fraction part of the coordinates gets lost when the velocity vector is added to the position of the rectangle. If you want to store object positions with floating point accuracy, you have to store the location of the object in separate attribute and to synchronize the pygame.Rect object.
if self.state == 1:
self.position += self.velocity * 6
if self.state == -1:
self.position -= self.velocity * 6
self.rect.center = self.position
Complete class Robot:
class Robot(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.robot_image = robot
self.image = self.robot_image
self.rect = self.image.get_rect(center=(100,355))
self.position = self.rect.center
self.velocity = pygame.math.Vector2(1,0)
self.angle = 0
self.rotation = 5
self.state = 0
self.direction = 0
def update(self):
self.drive()
self.rotate()
def drive(self):
if self.state == 1:
self.position += self.velocity * 6
if self.state == -1:
self.position -= self.velocity * 6
self.rect.center = self.position
def rotate(self):
if self.direction == 1:
self.angle -= self.rotation
if self.direction == -1:
self.angle += self.rotation
self.velocity = pygame.math.Vector2(1,0)
self.velocity.rotate_ip(-self.angle)
self.rect = self.image.get_rect(center=self.rect.center)
self.image = pygame.transform.rotozoom(self.robot_image,self.angle,1)

Enemy collision with pygame.sprite.spritecollide()

In my game, there is a sprite player which I can control. It can move right or left, it can jump, shoot fireballs(bullets) and a breathe fire. I have added an enemy which can move on itself from right to left on a limited distance that I set. What I would like to do now is make my player loose health if it collides with the enemy sprite using pygame.sprite.spritecollide(). However it isn't working out well I don't know how to fix my issue which is the following: if I run my code below it says NameError: name 'enemy_list' is not defined. The errored line is in Sprite1.py in the Player class under the update function. How do I fix my code? I created my Enemy class and Level class with the following website: https://opensource.com/article/18/5/pygame-enemy. I'm open to all suggestions. Thanks beforehand! I separated my code into three files: main.py, settings.py and Sprite1.py. Here's main.py:
import pygame
import os
import sys
import time
from pygame import mixer
from Sprite1 import *
from settings import *
'''
Setup
'''
pygame.init()
clock = pygame.time.Clock()
pygame.mixer.music.load('.\\sounds\\Fairy.mp3')
pygame.mixer.music.play(-1, 0.0)
all_sprites = pygame.sprite.Group()
player = Player(all_sprites)
player.rect.x = 500
player.rect.y = 500
eloc = []
eloc = [400,500]
enemy_list = Level.bad( 1, eloc )
showStartScreen(surface)
x = 0
'''
Main loop
'''
main = True
while main == True:
background = pygame.image.load(os.path.join('images', 'Bg.png')).convert()
surface.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
main = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.control(-steps,0)
if event.key == pygame.K_RIGHT:
player.control(steps,0)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.control(steps,0)
if event.key == pygame.K_RIGHT:
player.control(-steps,0)
keys = pygame.key.get_pressed()
if not(isJump):
if keys[pygame.K_UP]:
isJump = True
else:
if jumpCount >= -10:
player.rect.y -= (jumpCount * abs(jumpCount)) * 1
jumpCount -= 2
else:
jumpCount = 10
isJump = False
# dt = time since last tick in milliseconds.
dt = clock.tick(60) / 1000
all_sprites.update(dt)
player.update(dt)
all_sprites.draw(surface) #refresh player position
enemy_list.draw(surface)
for e in enemy_list:
e.move()
pygame.display.flip()
Here's my settings.py:
import pygame
isJump = False
jumpCount = 10
width = 960
height = 720
fps = 40 # frame rate
pygame.display.set_caption('B.S.G.')
surface = pygame.display.set_mode((width, height))
PLAYER_ACC = 0.5
PLAYER_FRICTION = -0.12
PLAYER_GRAV = 0.8
PLAYER_JUMP = 20
PLAYER_LAYER = 2
PLATFORM_LAYER = 1
RED = (255, 0, 0)
steps = 10 # how fast to move
And here's my Sprite1.py:
import pygame
import sys
import os
import time
from pygame import mixer
from pygame.locals import *
from settings import *
vec = pygame.math.Vector2
def showStartScreen(surface):
show = True
while (show == True):
background = pygame.image.load(os.path.join('images', 'Starting_scr.png'))
# rect = surface.get_rect()
surface.blit(background, (0,0))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
show = False
class Player(pygame.sprite.Sprite):
def __init__(self, all_sprites):
pygame.sprite.Sprite.__init__(self)
self.movex = 0
self.movey = 0
self.frame = 0
self.health = 10
self.jumping = False
self.images = []
self.imagesleft = []
self.imagesright = []
self.direction = "right"
self.alpha = (0,0,0)
self.ani = 4 # animation cycles
self.all_sprites = all_sprites
self.add(self.all_sprites)
self.fire_timer = .1
self.bullet_timer = .1
self.pos = vec(40, height - 100)
self.vel = vec(0, 0)
for i in range(1,5):
img = pygame.image.load(os.path.join('images','hero' + str(i) + '.png')).convert()
img.convert_alpha()
img.set_colorkey(self.alpha)
self.imagesright.append(img)
self.image = self.imagesright[0]
self.rect = self.image.get_rect()
for i in range(1,5):
img = pygame.image.load(os.path.join('images','hero' + str(i) + '.png')).convert()
img = pygame.transform.flip(img, True, False)
img.convert_alpha()
img.set_colorkey(self.alpha)
self.imagesleft.append(img)
self.image = self.imagesleft[0]
self.rect = self.image.get_rect()
def control(self,x,y):
'''
control player movement
'''
self.movex += x
self.movey -= y
def update(self, dt):
'''
Update sprite position
'''
self.rect.x = self.rect.x + self.movex
self.rect.y = self.rect.y + self.movey
enemy_hit_list = pygame.sprite.spritecollide(self, enemy_list, False)
for enemy in ennemy_hit_list:
self.health -= 1
print(self.health)
# moving left
if self.movex < 0:
self.frame += 1
if self.frame > 3*self.ani:
self.frame = 0
self.image = self.imagesleft[self.frame//self.ani]
self.direction = "left"
# moving right
if self.movex > 0:
self.frame += 1
if self.frame > 3*self.ani:
self.frame = 0
self.image = self.imagesright[self.frame//self.ani]
self.direction = "right"
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
self.bullet_timer -= dt # Subtract the time since the last tick.
if keys[pygame.K_x]:
self.fire_timer -= dt
if self.bullet_timer <= 0:
self.bullet_timer = 100 # Bullet ready.
if keys: # Left mouse button.
# Create a new bullet instance and add it to the groups.
if self.direction == "right":
Bullet([self.rect.x + self.image.get_width(), self.rect.y + self.image.get_height()/2], self.direction, self.all_sprites)
else:
Bullet([self.rect.x, self.rect.y + self.image.get_height()/2], self.direction, self.all_sprites)
self.bullet_timer = .5 # Reset the timer.
if self.fire_timer <= 0:
self.fire_timer = 100
if keys:
if self.direction == "right":
Fire([self.rect.x + 170, self.rect.y + self.image.get_height()/2], self.direction, self.all_sprites)
else:
Fire([self.rect.x - 90, self.rect.y + self.image.get_height()/2], self.direction, self.all_sprites)
self.fire_timer = .1
if self.health == 0:
self.kill()
class Enemy(pygame.sprite.Sprite):
'''
Spawn an enemy
'''
def __init__(self,x,y,img):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join('images',img))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.counter = 0 # counter variable
def move(self):
'''
enemy movement
'''
distance = 20
speed = 15
if self.counter >= 0 and self.counter <= distance:
self.rect.x += speed
elif self.counter >= distance and self.counter <= distance*2:
self.rect.x -= speed
else:
self.counter = 0
self.counter += 1
class Bullet(pygame.sprite.Sprite):
IMAGE = None
FLIPPED_IMAGE = None
def __init__(self, pos, direction, *sprite_groups):
super().__init__(*sprite_groups)
# cache images
if not Bullet.IMAGE:
Bullet.IMAGE = pygame.image.load(os.path.join('images','fireball.png'))
Bullet.FLIPPED_IMAGE = pygame.transform.flip(Bullet.IMAGE, True, False)
if direction == "right":
self.vel = pygame.math.Vector2(750, 0)
self.image = Bullet.IMAGE
else:
self.vel = pygame.math.Vector2(-750, 0)
self.image = Bullet.FLIPPED_IMAGE
self.pos = pygame.math.Vector2(pos)
self.rect = self.image.get_rect(center=pos)
def update(self, dt):
# Add the velocity to the position vector to move the sprite
self.pos += self.vel * dt
self.rect.center = self.pos # Update the rect pos.
if not pygame.display.get_surface().get_rect().colliderect(self.rect):
self.kill()
class Fire(pygame.sprite.Sprite):
IMAGE = None
FLIPPED_IMAGE = None
def __init__(self, pos, direction, *sprite_groups):
super().__init__(*sprite_groups)
# cache images
if not Fire.IMAGE:
Fire.IMAGE = pygame.image.load(os.path.join('images','fire_drag.png'))
Fire.FLIPPED_IMAGE = pygame.transform.flip(Fire.IMAGE, True, False)
if direction == "right":
self.image = Fire.IMAGE
self.vel = pygame.math.Vector2(0, 0)
else:
self.image = Fire.FLIPPED_IMAGE
self.vel = pygame.math.Vector2(0, 0)
self.pos = pygame.math.Vector2(pos)
self.rect = self.image.get_rect(center=pos)
def update(self, dt):
self.too = True
self.pos += self.vel * dt
self.rect.center = self.pos # Update the rect pos.
if self.too == True:
self.kill()
class Level():
def bad(lvl,eloc):
if lvl == 1:
enemy = Enemy(eloc[0],eloc[1],'cookie1.png') # spawn enemy
enemy_list = pygame.sprite.Group() # create enemy group
enemy_list.add(enemy) # add enemy to group
if lvl == 2:
print("Level " + str(lvl) )
return enemy_list
def loot(lvl,lloc):
print(lvl)
enemy_list is defined in global namespace, in main.py, thus it is not accessible in the module Sprite.py.
Add an additional argument to the update method of the class Player:
class Player(pygame.sprite.Sprite):
# [...]
def update(self, dt, enemy_list):
# [...]
enemy_hit_list = pygame.sprite.spritecollide(self, enemy_list, False)
# [...]
Since player is a member of all_sprites, you have to add the argument to the update methods of the other sprites (Enemy, Bullet), too.
Pass enemy_list to the update method all_sprites in the main application loop. Note the update method of Player is invoked by all_sprites.update, thus player.update(dt, enemy_list) is superflous:
while main == True:
# [...]
all_sprites.update(dt, enemy_list)
# [...]

All the sprites have to move in pygame

I am using Vector2 for my game to make the camera. But when I collided with the wall all the sprites started moving so I fixed it with my walls and floor, but I can't figure out how to fix my enemy. Any ideas?
this is my code:
import sys
import pygame as pg
from pygame.math import Vector2
width = 1280
height = 720
x1 = 200
y1 = 100
x2 = 500
y2 = 400
x3 = 100
y3 = 300
x = 0
y = 0
class Player(pg.sprite.Sprite):
def __init__(self, pos, *groups):
super().__init__(*groups)
pg.sprite.Sprite.__init__(self)
self.image = pg.image.load("character.png")
self.rect = self.image.get_rect(center=pos)
self.vel = Vector2(0, 0)
self.pos = Vector2(pos)
def update(self):
self.pos += self.vel
self.rect.center = self.pos
#enemy class
class Enemy(pg.sprite.Sprite):
def __init__(self, pos, waypoints, *groups):
super().__init__(*groups)
self.image = pg.image.load("enemy.png")
self.image = pg.transform.scale(self.image, (int(50), int(50)))
self.rect = self.image.get_rect(center=pos)
self.vel = Vector2(0,0)
self.max_speed = 5
self.pos = Vector2(pos)
self.waypoints = waypoints
self.waypoint_index = 0
self.target = self.waypoints[self.waypoint_index]
self.target_radius = 50
self.rect.x = width / 2
self.rect.y = height / 2
def update(self):
# A vector pointing from self to the target.
heading = self.target - self.pos
distance = heading.length() # Distance to the target.
heading.normalize_ip()
if distance <= 2: # We're closer than 2 pixels.
# Increment the waypoint index to swtich the target.
# The modulo sets the index back to 0 if it's equal to the length.
self.waypoint_index = (self.waypoint_index + 1) % len(self.waypoints)
self.target = self.waypoints[self.waypoint_index]
if distance <= self.target_radius:
# If we're approaching the target, we slow down.
self.vel = heading
else: # Otherwise move with max_speed.
self.vel = heading * self.max_speed
self.pos += self.vel
self.rect.center = self.pos
#Enemy waypoints
waypoints = [[x1, y1], [x2, y2], [x3, y3]]
class Floor(pg.sprite.Sprite):
def __init__(self, x, y, *groups):
super().__init__(*groups)
self.image = pg.image.load("floor.png")
self.rect = self.image.get_rect(topleft=(x, y))
self.rect.x = x
self.rect.y = y
class SideWall(pg.sprite.Sprite):
def __init__(self, x, y, *groups):
super().__init__(*groups)
self.image = pg.image.load("sidewall.png")
self.rect = self.image.get_rect(topleft=(x, y))
self.rect.x = x
self.rect.y = y
class TopAndBottomWall(pg.sprite.Sprite):
def __init__(self, x, y, *groups):
super().__init__(*groups)
self.image = pg.image.load("topandbottomwall.png")
self.rect = self.image.get_rect(topleft=(x, y))
self.rect.x = x
self.rect.y = y
def main():
screen = pg.display.set_mode((1280, 720))
clock = pg.time.Clock()
#all the sprites group
all_sprites = pg.sprite.Group()
#the floor
floor = Floor(540, -620, all_sprites)
#player
player = Player(((width / 2), (height / 2)), all_sprites)
#walls group
walls = pg.sprite.Group()
#all walls
walltop = TopAndBottomWall(540, -620, all_sprites, walls)
wallbottom = TopAndBottomWall(540, 410, all_sprites, walls)
wallleft = SideWall((width / 2) - 100, (height / 2) - 930, all_sprites, walls)
wallright = SideWall((wallleft.rect.x + (1920 - 50)), (height / 2) - 930, all_sprites, walls)
#all enemy's
enemy = Enemy((100, 300), waypoints, all_sprites)
camera = Vector2(0, 0)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
#player movement
elif event.type == pg.KEYDOWN:
if event.key == pg.K_d:
player.vel.x = 5
elif event.key == pg.K_a:
player.vel.x = -5
elif event.key == pg.K_w:
player.vel.y = -5
elif event.key == pg.K_s:
player.vel.y = 5
elif event.type == pg.KEYUP:
if event.key == pg.K_d and player.vel.x > 0:
player.vel.x = 0
elif event.key == pg.K_a and player.vel.x < 0:
player.vel.x = 0
elif event.key == pg.K_w:
player.vel.y = 0
elif event.key == pg.K_s:
player.vel.y = 0
camera -= player.vel
all_sprites.update()
if pg.sprite.spritecollide(player, walls, False):
#stop the left wall from moving
wallleft.rect.x = wallleft.rect.x + player.vel.x
wallleft.rect.y = wallleft.rect.y + player.vel.y
#stop the top wall from moving
walltop.rect.y = walltop.rect.y + player.vel.y
walltop.rect.x = walltop.rect.x + player.vel.x
#stop the right wall from moving
wallright.rect.x = wallright.rect.x + player.vel.x
wallright.rect.y = wallright.rect.y + player.vel.y
#stop the bottom wall from moving
wallbottom.rect.x = wallbottom.rect.x + player.vel.x
wallbottom.rect.y = wallbottom.rect.y + player.vel.y
#stop the floor from moving
floor.rect.x = floor.rect.x + player.vel.x
floor.rect.y = floor.rect.y + player.vel.y
screen.fill((0, 0, 0))
for sprite in all_sprites:
screen.blit(sprite.image, sprite.rect.topleft+camera)
pg.display.flip()
clock.tick(30)
main()
pg.quit()
This is a link with the files if you want to run it.
https://geordyd.stackstorage.com/s/hZZ1RWcjal6ecZM
You are working with screen coordinates. You only move the walls while checking for keypresses; you should move the enemies at the same time.
But there is a far better way; more expandable if you add more classes, and less confusing.
Remove the keypresses check out of Wall and put them into Player. This will change the player's position in world coordinates, the same coordinates as the walls are (static) and the enemies move in (dynamic).
Then draw both walls and enemies at world_position - players_position, adjusted for the player's relative position in the center. Technically even the player itself is drawn at that position – the calculation for him amounts to zero movement relative to the screen.
For even more flexibility you could consider a separate Camera class, which is set up to follow the player by default. That allows you to let the camera move 'elsewhere', or if the player is near the edge of your world, let him walk off the screen center.

Artificial inteligence in python and pygame for Pong clone

I'm making a program that clones pong based off a tutorial and I already have the program to where it is multiplayer with two separate people. I want to add an AI in the program instead of a player 2. I've been stuck on this for quite some time and would appreciate any help! Here is the code currently:
import sys, os, math, random, pygame
from pygame.locals import *
class paddle(pygame.sprite.Sprite):
def __init__(self, xy):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join('assets', 'pong_paddle.gif'))
self.rect = self.image.get_rect()
self.rect.centerx, self.rect.centery = xy
self.movementspeed = 5
self.velocity = 0
def up(self):
# increases vertical velocity
self.velocity -= self.movementspeed
def down(self):
# decreases vertical velocity
self.velocity += self.movementspeed
def move(self, dy):
# moves the paddle y, doesn't go out of top or bottom
if self.rect.bottom + dy > 400:
self.rect.bottom = 400
elif self.rect.top + dy < 0:
self.rect.top = 0
else:
self.rect.y += dy
def update(self):
# makes the paddle move every frame
self.move(self.velocity)
class aiplayer(object):
def __init__(self):
self.bias = random.random() - 0.5
self.hit_count = 0
def update(self, paddle, game,):
if (paddle.rect.centerx < game.bounds.centerx and game.ball.rect.centerx < game.bounds.centerx) or (paddle.rect.centerx > game.bounds.centerx and game.ball.rect.centerx > game.bounds.centerx):
delta = (paddle.rect.centery + self.bias * paddle.rect.height) - game.ball.rect.centery
if abs(delta) > paddle.velocity:
if delta > 0:
paddle.direction = -1
else:
paddle.direction = 1
else:
paddle.direction = 0
else:
paddle.direction = 0
def hit(self):
self.hit_count += 1
if self.hit_count > 6:
self.bias = random.random() - 0.5
self.hit_count = 0
def lost(self):
self.bias = random.random() - 0.5
def won(self):
pass
def render(self, surface):
x, y = self.location
w, h = self.image.get_size()
surface.blitz(self.image, (x-w/2, y-h/2))
class Ball(pygame.sprite.Sprite):
def __init__(self, xy):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join('assets', 'pong_ball.gif'))
self.rect = self.image.get_rect()
self.rect.centerx, self.rect.centery = xy
self.maxspeed = 10
self.servespeed = 5
self.velx = 0
self.vely = 0
def reset(self):
self.rect.centerx, self.rect.centery = 400, 200
self.velx = 0
self.vely = 0
def serve(self):
angle = random.randint(-45, 45)
if abs(angle) < 5 or abs(angle-180) < 5:
angle = random.randint(10, 20)
if random.random() > .5:
angle += 180
# this gets the velocity for the x and y coords
x = math.cos(math.radians(angle))
y = math.sin(math.radians(angle))
self.velx = self.servespeed * x
self.vely = self.servespeed * y
class Game(object):
def __init__(self):
pygame.init()
# creates the window
self.window = pygame.display.set_mode((800, 400))
# makes a clock
self.clock = pygame.time.Clock()
# window title
pygame.display.set_caption("Pong")
# tells pygame to watch for these certain events so we can close window
pygame.event.set_allowed([QUIT, KEYDOWN, KEYUP])
self.background = pygame.Surface((800, 400))
self.background.fill((55, 255, 85))
pygame.draw.line(self.background, (0,0,0), (400, 0), (400, 400), 2)
self.window.blit(self.background, (0,0))
#lets the background show up
pygame.display.flip()
#renders the sprites so that they actually show up
self.sprites = pygame.sprite.RenderUpdates()
# makes the paddles, adds to sprite group
self.leftpaddle = paddle((50, 200))
self.sprites.add(self.leftpaddle)
self.rightpaddle = paddle((750, 200))
self.sprites.add(self.rightpaddle)
# makes the ball
self.ball = Ball((400, 200))
self.sprites.add(self.ball)
def run(self):
# this lets the game run using a loop so its always active and never closes
running = True
while running:
self.clock.tick(60)
# pygame event, if user closes the game, then stop running
running = self.handleEvents()
pygame.display.set_caption("Pong %d fps" % self.clock.get_fps())
self.manageBall()
# updates the sprites(paddles, ball)
for sprite in self.sprites:
sprite.update()
# renders the sprites
self.sprites.clear(self.window, self.background)
dirty = self.sprites.draw(self.window)
pygame.display.update(dirty)
def handleEvents(self):
for event in pygame.event.get():
if event.type == QUIT:
return False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
return False
# controls the right paddle
if event.key == K_w:
self.leftpaddle.up()
if event.key == K_s:
self.leftpaddle.down()
if event.key == K_UP:
self.rightpaddle.up()
if event.key == K_DOWN:
self.rightpaddle.down()
# serves the ball
if event.key == K_SPACE:
if self.ball.velx == 0 and self.ball.vely == 0:
self.ball.serve()
elif event.type == KEYUP:
if event.key == K_w:
self.leftpaddle.down()
if event.key == K_s:
self.leftpaddle.up()
if event.key == K_UP:
self.rightpaddle.down()
if event.key == K_DOWN:
self.rightpaddle.up()
elif event.type ==
return True
def manageBall(self):
# this moves the ball
self.ball.rect.x += self.ball.velx
self.ball.rect.y += self.ball.vely
if self.ball.rect.top < 0:
self.ball.rect.top = 1
# makes the ball bounce
self.ball.vely *= -1
elif self.ball.rect.bottom > 400:
self.ball.rect.bottom = 399
# makes ball bounce off bottom
self.ball.vely *= -1
# resets the ball if it hits the left or right screen
if self.ball.rect.left < 0:
self.ball.reset()
return
elif self.ball.rect.right > 800:
self.ball.reset()
return
collision = pygame.sprite.spritecollide(self.ball, [self.leftpaddle, self.rightpaddle], dokill = False)
if len(collision) > 0:
hitpaddle = collision[0]
# sends the ball back
self.ball.velx *= -1
# makes sure the ball doesn't get stuck in the paddle
self.ball.rect.x += self.ball.velx
# makes the game and runs it
if __name__ == '__main__':
game = Game()
game.run()
Make a function AI in the aiplayer and have it return up or down
int AI(self.position,ball.position):
if self.y>ball.y:
return -1
elif self.y<ball.y:
return 1
then, in the update() code for aiplayer, do something similar to this
self.y += movespeed*AI(self.position,ball.position)
Then, it either moves the paddle up or down depending on where the ball is ( you might need to switch if you add or subtract the movespeed to get the paddle to go the right direction). Also, it might be more effective to use the center of the paddle so that it won't put the top or bottom edge of the paddle at the ball.

Categories