I am trying to create my own game using pygame with classes and my problem is that I cannot understand why my program is not working properly
import pygame
import time
import random
import sys
pygame.init()
screen = pygame.display.set_mode((1280, 720))
pygame.display.set_caption("this game")
class Background:
def __init__(self, x, y):
self.xpos = x
self.ypos = y
picture = pygame.image.load("C:/images/dunes.jpg")
picture = pygame.transform.scale(picture, (1280, 720))
def draw(self):
pygame.surface.Surface.blit(picture, (self.xpos, self.ypos))
class Monster:
def __init__(self, x, y):
self.xpos = x
self.ypos = y
def move_left(self):
self.xpos =- 5
def move_right(self):
self.xpos =+ 5
def jump(self):
for x in range(1, 10):
self.ypos =-1
pygame.display.show()
for x in range(1, 10):
self.ypos =+1
pygame.display.show()
picture = pygame.image.load("C:/pics/hammerhood.png")
picture = pygame.transform.scale(picture, (200, 200))
def draw(self):
pygame.surface.Surface.blit(picture, (self.xpos, self.ypos))
class enemy:
def __init__(self, x, y):
self.xpos = x
self.ypos = y
picture = pygame.image.load("C:/pics/dangler_fish.png")
picture = pygame.transform.scale(picture, (200, 200))
def teleport(self):
self.xpos = random.randint(1, 1280)
self.pos= random.randint(1, 720)
def draw(self):
pygame.surface.Surface.blit(picture, (self.xpos, self.ypos))
while True:
ice = Background(720, 360)
hammerhood = Monster(200, 500)
fish = enemy(0, 0)
fish.teleport()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if pygame.event == pygame.K_d:
hammerhood.move_right()
if pygame.event == pygame.K_a:
hammerhood.move_left()
if pygame.event == pygame.K_w:
hammerhood.jump()
hammerhood.draw()
ice.draw()
fish.draw()
what it is saying to me is, line 49, in the draw
pygame.surface.Surface.blit(picture, (self.xpos, self.ypos))
NameError: name 'picture' is not defined
I have tried everything and there is another project I copied from the internet and this has the exact same way of blitting the picture in the class but in this one, it is not working
you define picture at the class top level which makes it a class attribute. To access it from a method you have to use self. picture
The picture attributes are class attributes (defined outside of the __init__ method) but they can be accessed like any other attributes by prepending them with self.. Also, you most likely want to blit the pictures onto the screen surface, so just call the blit method of this surface:
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
Note that class attributes are shared between all instances, so if you modify one surface/image, they'll be changed for all sprites.
There are a few more problems:
The event handling doesn't work.
The sprites won't move continuously. Define some speed attributes and add them to the positions each frame.
You should almost always call the convert or convert_alpha methods to improve the blit performance of the surfaces.
Here's a fixed version (except for the jumping) with some comments:
import pygame
import random
import sys
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock() # A clock to limit the frame rate.
pygame.display.set_caption("this game")
class Background:
picture = pygame.image.load("C:/images/dunes.jpg").convert()
picture = pygame.transform.scale(picture, (1280, 720))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
def draw(self):
# Blit the picture onto the screen surface.
# `self.picture` not just `picture`.
screen.blit(self.picture, (self.xpos, self.ypos))
class Monster:
picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
# If you want to move continuously you need to set these
# attributes to the desired speed and then add them to
# self.xpos and self.ypos in an update method that should
# be called once each frame.
self.speed_x = 0
self.speed_y = 0
def update(self):
# Call this method each frame to update the positions.
self.xpos += self.speed_x
self.ypos += self.speed_y
# Not necessary anymore.
# def move_left(self):
# self.xpos -= 5 # -= not = -5 (augmented assignment).
#
# def move_right(self):
# self.xpos += 5 # += not = +5 (augmented assignment).
def jump(self):
# What do you want to do here?
for x in range(1, 10):
self.ypos -= 1 # -= not =-
# pygame.display.show() # There's no show method.
for x in range(1, 10):
self.ypos += 1
# pygame.display.show()
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
class Enemy: # Use upper camelcase names for classes.
picture = pygame.image.load("C:/pics/dangler_fish.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
def teleport(self):
self.xpos = random.randint(1, 1280)
self.pos= random.randint(1, 720)
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
# Create the instances before the while loop.
ice = Background(0, 0) # I pass 0, 0 so that it fills the whole screen.
hammerhood = Monster(200, 500)
fish = Enemy(0, 0)
while True:
# Handle events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Check if the `event.type` is KEYDOWN first.
elif event.type == pygame.KEYDOWN:
# Then check which `event.key` was pressed.
if event.key == pygame.K_d:
hammerhood.speed_x = 5
elif event.key == pygame.K_a:
hammerhood.speed_x = -5
elif event.key == pygame.K_w:
hammerhood.jump()
elif event.type == pygame.KEYUP:
# Stop moving when the keys are released.
if event.key == pygame.K_d and hammerhood.speed_x > 0:
hammerhood.speed_x = 0
elif event.key == pygame.K_a and hammerhood.speed_x < 0:
hammerhood.speed_x = 0
# Update the game.
hammerhood.update()
fish.teleport()
# Draw everything.
ice.draw() # Blit the background to clear the screen.
hammerhood.draw()
fish.draw()
pygame.display.flip()
clock.tick(60) # Limit the frame rate to 60 FPS.
Related
I am really newer to study the python and doing the exercrise
I want to create an rectange or any other graph such as i usepygame.Rect(x,y,w,h)
and set the screen_center by
self.screen_rect=setting.screen.get_rect()
self.screent_center=self.screen_rect.center
but the rectange's center is not in the screen's center
also i want use self.b=self.bullet.get_rect()but it show error
how can i fix it?
here's the code:
#! /usr/bin/python
import pygame as p
import sys
class Setting():
def __init__(self,width,height):
self.w=width
self.h=height
self.flag=p.RESIZABLE
self.color=(255,255,255)
self.speed=1
self.screen=p.display.set_mode((self.w,self.h),self.flag)
p.display.set_caption("Bullet")
self.bullet_s=1
self.bullet_w=100
self.bullet_h=300
self.bullet_c=(0,0,0)
class Bullet(p.sprite.Sprite):
def __init__(self,setting):
super().__init__()
self.screen_rect=setting.screen.get_rect()
self.screent_center=self.screen_rect.center
self.bullet=p.Rect((self.screen_center),(setting.bullet_w,setting.bullet_h)) **<-- not in the center**
self.b=self.bullet.get_rect() **<-- AttributeError: 'pygame.Rect' object has no attribute 'get_rect'**
self.color=setting.bullet_c
self.speed=setting.bullet_s
# self.centery=float(self.bullet.centery)
def bullet_move(self):
self.y -= self.speed
self.bullet.y=self.y
def draw_bullet(self,setting):
self.rect=p.draw.rect(setting.screen,self.color,self.bullet)
def game():
p.init()
setting=Setting(1200,800)
bullet=Bullet(setting)
while True:
for event in p.event.get():
if event.type == p.QUIT:
sys.exit()
setting.screen.fill((255,0,0))
bullet.draw_bullet(setting)
p.display.flip()
game()
This code center rectangle on screen using
self.screen_rect = setting.screen.get_rect()
self.rect.center = self.screen_rect.center
It also moves rectangle when you press UP or DOWN.
It uses KEYDOWN, KEYUP to change speed and it runs move() in every loop and this function uses speed to change position (without checking keys).
It also compare rect.top with screen.top and rect.bottom with screen.bottom to stop rectangle when it touchs border of the screen.
BTW: I also add spaces and empty lines in code to make it more readable.
See: PEP 8 -- Style Guide for Python Code
import pygame as p
class Setting():
def __init__(self, width, height):
self.w = width
self.h = height
self.flag = p.RESIZABLE
self.color = (255, 255, 255)
self.speed = 1
self.screen = p.display.set_mode((self.w, self.h), self.flag)
p.display.set_caption("Bullet")
self.bullet_s = 1
self.bullet_w = 100
self.bullet_h = 300
self.bullet_c = (0, 0, 0)
class Bullet(p.sprite.Sprite):
def __init__(self, setting):
super().__init__()
self.setting = setting
self.screen_rect = setting.screen.get_rect()
self.rect = p.Rect(0, 0, setting.bullet_w, setting.bullet_h)
self.rect.center = self.screen_rect.center
self.color = setting.bullet_c
self.speed = 0 #setting.bullet_s
def move(self):
self.rect.y -= self.speed
if self.rect.top < 0:
self.rect.top = 0
elif self.rect.bottom > self.screen_rect.bottom:
self.rect.bottom = self.screen_rect.bottom
def draw(self):
p.draw.rect(self.setting.screen, self.color, self.rect)
def handle_event(self, event):
if event.type == p.KEYDOWN:
if event.key == p.K_UP:
self.speed = self.setting.bullet_s
elif event.key == p.K_DOWN:
self.speed = -self.setting.bullet_s
elif event.type == p.KEYUP:
if event.key == p.K_UP:
self.speed = 0
elif event.key == p.K_DOWN:
self.speed = 0
def game():
p.init()
setting = Setting(1200,800)
bullet = Bullet(setting)
running = True
while running:
for event in p.event.get():
if event.type == p.QUIT:
running = False
bullet.handle_event(event)
bullet.move()
setting.screen.fill((255, 0, 0))
bullet.draw()
p.display.flip()
p.quit()
game()
This will calculate the position from the top-left corner of the sprite
screen = pygame.display.set_mode((1080, 720))
def getcenter(sprite):
screen_size = screen.get_size()
sprite_size = sprite.image.get_size()
center_x = screen_size[0]/2 + sprite_size[0]/2
center_y = screen_size[1]/2 + sprite_size[1]/2
return (center_x, center_y)
getcenter(spritehere)
I have made a putt-putt game and now I want to add a slanted wall type. Because of this, I need to use masks for the collision (until now I have just used rects). I have spent hours learning about masks and trying to figure out why my code won't work. There are no errors, the collision just isn't detected.
I have simplified my code down to something much smaller just as a way for me to test it efficiently. From everything I've seen this seems like it should work, but it doesnt. Here it is:
import pygame
# Pygame init stuff
pygame.init()
wind_width = 1200
wind_height = 700
gameDisplay = pygame.display.set_mode((wind_width, wind_height))
pygame.display.set_caption("Mini Golf!")
pygame.display.update()
gameExit = False
clock = pygame.time.Clock()
# Class setups
class Ball:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("sball.png")
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
def render(self):
self.rect.topleft = (self.x, self.y)
gameDisplay.blit(self.image, self.rect)
class Slant:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("posslant.png")
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
def render(self):
self.rect.topleft = (self.x, self.y)
gameDisplay.blit(self.image, self.rect)
# Creating objects
ball = Ball(250, 250)
slant = Slant(270, 250)
# Game loop
gameExit = False
while not(gameExit):
# Moves ball
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
ball.y -= 1
elif event.key == pygame.K_DOWN:
ball.y += 1
elif event.key == pygame.K_LEFT:
ball.x -= 1
elif event.key == pygame.K_RIGHT:
ball.x += 1
# Collision detection
offset_x, offset_y = (slant.rect.x - ball.rect.x), (slant.rect.y - ball.rect.y)
if slant.mask.overlap(ball.mask, (offset_x, offset_y)):
print("hit")
# Draws everything
gameDisplay.fill((0, 0, 0))
ball.render()
slant.render()
pygame.display.update()
clock.tick(100)
The offset parameter of the method overlap() is the relative position of the othermask in relation to the pygame.mask.Mask object.
So the offset is calculated by subtracting the coordinates of slant from the coordinates of ball:
offset_x, offset_y = (slant.rect.x - ball.rect.x), (slant.rect.y - ball.rect.y)
offset = (ball.rect.x - slant.rect.x), (ball.rect.y - slant.rect.y)
if slant.mask.overlap(ball.mask, offset):
print("hit")
When you create the mask images, then I recommend to ensure that the image has per pixel alpha format by calling .convert_alpha():
class Ball:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("sball.png")
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image.convert_alpha()) # <---
class Slant:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("posslant.png")
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image.image.convert_alpha()) # <---
Minimal example: repl.it/#Rabbid76/PyGame-SurfaceMaskIntersect
See also: Mask
I am trying to make the sprite move up and down by using the arrow keys but it schemes to only be moving slightly upwards and slightly downwards: there is a speed for the x and y axis and also a position. There are also two functions which are draw and update (which gets the new xpos and the new ypos). here is my code
import pygame
import random
import sys
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock() # A clock to limit the frame rate.
pygame.display.set_caption("this game")
class Background:
picture = pygame.image.load("C:/images/cliff.jpg").convert()
picture = pygame.transform.scale(picture, (1280, 720))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
class player_first:
picture = pygame.image.load("C:/aliens/ezgif.com-crop.gif")
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
self.speed_x = 0
self.speed_y = 0
self.rect = self.picture.get_rect()
def update(self):
self.xpos =+ self.speed_x
self.ypos =+ self.speed_y
def draw(self): #left right
#screen.blit(pygame.transform.flip(self.picture, True, False), self.rect)
screen.blit(self.picture, (self.xpos, self.ypos))
class Bullet_player_1(pygame.sprite.Sprite):
picture = pygame.image.load("C:/aliens/giphy.gif").convert_alpha()
picture = pygame.transform.scale(picture, (100, 100))
def __init__(self):
self.xpos = 360
self.ypos = 360
self.speed_x = 0
super().__init__()
self.rect = self.picture.get_rect()
def update(self):
self.xpos += self.speed_x
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
#self.screen.blit(pygame.transform.flip(self.picture, False, True), self.rect)
def is_collided_with(self, sprite):
return self.rect.colliderect(sprite.rect)
player_one_bullet_list = pygame.sprite.Group()
player_one = player_first(0, 0)
cliff = Background(0, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player_one.speed_y = -5
print("up")
elif event.key == pygame.K_s:
player_one.speed_y = 5
print("down")
elif event.key == pygame.K_SPACE:
bullet_player_one = Bullet_player_1
bullet_player_one.ypos = player_one.ypos
bullet_player_one.xpos = player_one.xpos
bullet_player_one.speed_x = 14
player_one_bullet_list.add(bullet_player_one)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d and player_one.speed_x > 0:
player_one.speed_x = 0
elif event.key == pygame.K_a and player_one.speed_x < 0:
player_one.speed_x = 0
player_one.update()
cliff.draw()
player_one.draw()
player_one_bullet_list.update()
for bullet_player_one in player_one_bullet_list:
bullet_player_one.draw()
pygame.display.flip()
clock.tick(60)
You've written self.xpos =+ self.speed_x (which is interpret as self.xpos = +self.speed_x) instead of self.xpos += self.speed_x. So you're not adding the speed to the position, you're overwriting it.
I am struggling with finding a way to make the sprite jump in pygame. when i basically run the program now the sprite goes straight to the top of the screen and i just want it to do a normal jump. the jump is in a class and is a function. I have used KEYDOWN to check when its pressed to move it downwards.
import pygame
import random
import sys
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock() # A clock to limit the frame rate.
pygame.display.set_caption("this game")
class Background:
picture = pygame.image.load("C:/images/dunes.jpg").convert()
picture = pygame.transform.scale(picture, (1280, 720))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
def draw(self):
# Blit the picture onto the screen surface.
# `self.picture` not just `picture`.
screen.blit(self.picture, (self.xpos, self.ypos))
class Monster:
picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
# If you want to move continuously you need to set these
# attributes to the desired speed and then add them to
# self.xpos and self.ypos in an update method that should
# be called once each frame.
self.speed_x = 0
self.speed_y = 0
def update(self):
# Call this method each frame to update the positions.
self.xpos += self.speed_x
self.ypos += self.speed_y
# Not necessary anymore.
def move_left(self):
self.xpos -= 5 # -= not = -5 (augmented assignment).
def move_right(self):
self.xpos += 5 # += not = +5 (augmented assignment).
def jump(self): #vvvvvvv this is the part i do not know how to fix
# What do you want to do here?
#for x in range(1, 10):
#self.ypos -= 1 # -= not =-
# pygame.display.show() # There's no show method.
#for x in range(1, 10):
#self.ypos += 1
# pygame.display.show()
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
class Enemy: # Use upper camelcase names for classes.
picture = pygame.image.load("C:/pics/dangler_fish.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
def teleport(self):
self.xpos = random.randint(1, 1280)
self.ypos= random.randint(1, 720)
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
# Create the instances before the while loop.
ice = Background(0, 0) # I pass 0, 0 so that it fills the whole screen.
hammerhood = Monster(200, 500)
fish = Enemy(0, 0)
while True:
# Handle events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Check if the `event.type` is KEYDOWN first.
elif event.type == pygame.KEYDOWN:
# Then check which `event.key` was pressed.
if event.key == pygame.K_d:
#hammerhood.move_right()
hammerhood.speed_x = 5
elif event.key == pygame.K_a:
#hammerhood.move_left()
hammerhood.speed_x = -5
elif event.key == pygame.K_w:
hammerhood.jump()
elif event.type == pygame.KEYUP:
# Stop moving when the keys are released.
if event.key == pygame.K_d and hammerhood.speed_x > 0:
hammerhood.speed_x = 0
elif event.key == pygame.K_a and hammerhood.speed_x < 0:
hammerhood.speed_x = 0
# Update the game.
hammerhood.update()
fish.teleport()
if hammerhood.xpos == 1280 or hammerhood.xpos == 0:
hammerhood.speed_x = 0
# Draw everything.
ice.draw() # Blit the background to clear the screen.
hammerhood.draw()
fish.draw()
pygame.display.flip()
clock.tick(60) # Limit the frame rate to 60 FPS.
Just add a GRAVITY constant and add it to the self.speed_y each frame to accelerate the player.
GRAVITY = .9 # Define this in the global scope.
# The Monster's update method.
def update(self):
self.speed_y += GRAVITY # Accelerate downwards.
self.xpos += self.speed_x
self.ypos += self.speed_y
# Stop falling at the bottom of the screen.
if self.ypos >= 520:
self.ypos = 520
self.speed_y = 0
self.on_ground = True
Set the self.speed_y to a negative value when you start to jump:
def jump(self):
if self.on_ground: # This prevents air jumps.
self.on_ground = False
self.speed_y = -25
I've tried everything and cannot get how I check if my two blocks have collided.
Here's my code:
import pygame
import random
pygame.init()
display_width = 600
display_height = 300
class player:#Just info for player
width = 30
height = 30
x = display_width // 2
y = display_height - height - 5
a = x + 30
pic = 'SnakePart.png'
thing = pygame.image.load(pic)
def place(x,y):#function for placing player object
gameDisplay.blit(player.thing, (player.x,player.y))
class enemy:#enemy info class
width = 30
height = 30
x = random.randint(0,display_width - width)
y = 1
a = x + 30
pic = 'Apple.png'
thing = pygame.image.load(pic)
speed = 10#sets speed
def change_x():
enemy.x = random.randint(0,display_width - enemy.width)
def change_y():
enemy.y += enemy.speed
def make(x,y):#set up funtion
gameDisplay.blit(enemy.thing, (x,y))
def respawn():#reseting enemy entity
enemy.y = 1
gameDisplay.blit(enemy.thing, (enemy.x,enemy.y))
player.thing#uses the variables in the classes to set up the images for use
enemy.thing
black = (0,0,0)
white = (255,255,255)
player_height = 30
player_width = 30
clock = pygame.time.Clock()
x_change = 0#This is to make movment
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Bullet Hell.')
dodged = 0#counter will be used in the more polished vesion.
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:#Checks for keypress, to make player entity move
if event.key == pygame.K_RIGHT:
x_change = 5
if event.key == pygame.K_LEFT:
x_change = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or pygame.K_LEFT:
x_change = 0
player.x += x_change
gameDisplay.fill(black)
enemy.make(enemy.x,enemy.y)
player.place(player.x,player.y)
enemy.change_y()
if enemy.y > display_height:#Brings enemy back to top once it has gotten to th bottom
enemy.change_x()
dodged += 1
enemy.respawn()
pygame.display.update()
clock.tick(60)
It's all really the collision and I think I'll be good. Oh yeah, if you guys could also tell me how to display text that would be great!
Take a look at the pygame.Rect class and its collision detection methods. Give your objects rects as attributes which you get by calling the .get_rect() method of the images/surfaces. Update the coordinates of these rects each frame and in your main loop call player.rect.colliderect(enemy.rect) to check if the two rects collide.
import pygame
import random
pygame.init()
BLACK = pygame.Color('black')
display = pygame.display.set_mode((600, 300))
# Create a rect with the dimensions of the screen at coords (0, 0).
display_rect = display.get_rect()
clock = pygame.time.Clock()
# SNAKE_IMAGE = pygame.image.load('SnakePart.png').convert_alpha()
# APPLE_IMAGE = pygame.image.load('Apple.png').convert_alpha()
# Replacement images.
SNAKE_IMAGE = pygame.Surface((30, 30))
SNAKE_IMAGE.fill((30, 150, 0))
APPLE_IMAGE = pygame.Surface((30, 30))
APPLE_IMAGE.fill((150, 30, 0))
class Player:
def __init__(self):
self.x = display_rect.w // 2
self.y = display_rect.h - 30 - 5
self.image = SNAKE_IMAGE
# Create a rect with the size of the image at coords (0, 0).
self.rect = self.image.get_rect()
# Set the topleft coords of the rect.
self.rect.x = self.x
self.rect.y = self.y
self.x_change = 0
def update(self):
"""Move the player."""
self.x += self.x_change
# Always update the rect, because it's
# needed for the collision detection.
self.rect.x = self.x
def draw(self, display):
display.blit(self.image, self.rect)
class Enemy:
def __init__(self):
self.x = random.randint(0, display_rect.w - 30)
self.y = 1
self.image = APPLE_IMAGE
# You can also pass the coords directly to `get_rect`.
self.rect = self.image.get_rect(topleft=(self.x, self.y))
self.speed = 10
def change_x(self):
self.x = random.randint(0, display_rect.w - self.rect.w)
self.rect.x = self.x
def change_y(self):
self.y += self.speed
self.rect.y = self.y
def draw(self, display):
display.blit(self.image, self.rect)
def reset(self):
"""Reset self.y position."""
self.y = -30
player = Player()
enemy = Enemy()
dodged = 0
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
player.x_change = 5
if event.key == pygame.K_LEFT:
player.x_change = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or pygame.K_LEFT:
player.x_change = 0
# Game logic.
player.update()
enemy.change_y()
# Brings enemy back to top once it has gotten to th bottom
if enemy.y > display_rect.h:
enemy.change_x()
dodged += 1
enemy.reset()
# Check if the player and the rect collide.
if player.rect.colliderect(enemy.rect):
print('Collided!')
# Draw everything.
display.fill(BLACK)
enemy.draw(display)
player.draw(display)
pygame.display.update()
clock.tick(60)
pygame.quit()
I've changed some more things (btw, better call the images self.image not thing ;)) especially in the classes:
The attributes of the classes should be in __init__ methods to make them instance attributes instead of class attributes (which get shared by all instances). Also, check out how to create instances of your classes.
The methods should all have self as their first parameter (that's a reference to the instance). To access the attributes inside of the classes prepend them with self, e.g. self.x += self.x_change.
The player.x += x_change line fits better inside the Player class' update method, so you can just update the position and other player attributes by calling player.update().
There's a convention that class names should be uppercase MyClass whereas instances should have lowercase names my_instance.