rock paper scissors game - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: rock paper scissors game (/thread-15708.html) |
rock paper scissors game - abscorpy - Jan-28-2019 Hi Im making a rock, paper, scissors game... I need some help, what im trying to do is the following: -Im trying to make some random scissors appear from top to bottom of screen(if you hit the scissors you loose life) -Another thing im trying to do, is that some rocks appear from top to bottom of screen(if you catch a rock you get one point) Thanks a lot for the support import pygame pygame.init() # Did code to python pep 8 style guide. # https://www.python.org/dev/peps/pep-0008/ class Player: @classmethod def load_images(cls): # convert image to pygame format will help with speed # convert_alpha() will not show invisable parts with pngs and jpg cls.images = [ pygame.image.load('napkin.png').convert_alpha(), pygame.image.load('napkin2.png').convert_alpha(), pygame.image.load('napkin3.png').convert_alpha()] def __init__(self, x, y, w, h): self.rect = pygame.Rect(x, y, w, h) self.velocity = 5 self.is_jumping = False self.jump_count = 5 self.walk_left = 0, 2 # self.walk_left == (0, 2) self.walk_right = 0, 1 self.walk_count = 0 self.walk_pos = 0 self.tick = 100 self.next_tick = 100 def can_move(self, ticks): if ticks > self.tick: self.tick += self.next_tick return True return False def draw(self, surface): surface.blit(Player.images[self.walk_pos], self.rect) def move_left(self): if self.rect.x > self.velocity: self.walk_count = (self.walk_count + 1) % len(self.walk_left) self.walk_pos = self.walk_left[self.walk_count] self.rect.x -= self.velocity def move_right(self, width): if self.rect.x < width - self.rect.width - self.velocity: self.walk_count = (self.walk_count + 1) % len(self.walk_right) self.walk_pos = self.walk_right[self.walk_count] self.rect.x += self.velocity class Scene: def __init__(self): # basic pygame setup pygame.display.set_caption('Napkin Move') self.rect = pygame.Rect(0, 0, 1364, 500) self.surface = pygame.display.set_mode(self.rect.size) self.clock = pygame.time.Clock() # Scene setup Player.load_images() self.background = pygame.image.load('bg1.png').convert_alpha() self.background = pygame.transform.scale(self.background, self.rect.size) self.player = Player(300, 410, 64, 64) def mainloop(self): self.running = True while self.running: for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_a: self.player.move_left() elif event.key == pygame.K_d: self.player.move_right(self.rect.width) ticks = pygame.time.get_ticks() keys = pygame.key.get_pressed() if self.player.can_move(ticks): if keys[pygame.K_LEFT]: self.player.move_left() elif keys[pygame.K_RIGHT]: self.player.move_right(self.rect.width) # drawing self.surface.blit(self.background, (0,0)) self.player.draw(self.surface) # draw code here pygame.display.flip() self.clock.tick(30) if __name__ == '__main__': scene = Scene() scene.mainloop() pygame.quit() RE: rock paper scissors game - nilamo - Jan-28-2019 Your code is really clean looking. Are you following a tutorial? Mind sharing a link? The keywords you're looking for here, is "particle emitter". You're going to create an object every few seconds, and then destroy it later (probably once it's not on the screen anymore). You can make that fairly generic, by writing a particle emitter that uses the starting x/y values and the direction and speed that the particles move each frame, as well as the image to use. To get you started, here's a basic example. import pygame pygame.init() class ScissorEmitter: # spawn_delay = how many seconds before a new scissors is spawned def __init__(self, screen, spawn_delay=500, base_left=10, base_top=0): self.screen = screen self.scissors = [] self.spawn_delay = spawn_delay self.last_spawn = 0 self.img = pygame.image.load("scissors.png").convert_alpha() self.base_left = base_left self.base_top = base_top def should_spawn(self, ticks): if self.last_spawn + self.spawn_delay <= ticks: return True return False def spawn(self, ticks): self.last_spawn = ticks last_x = self.base_left if self.scissors: last_x = self.scissors[-1].x self.scissors.append(pygame.Rect(last_x + 10, self.base_top, self.img.get_width(), self.img.get_height())) def update(self, ticks): living_scissors = [] for rect in self.scissors: new_x = 0 new_y = 10 # only keep scissors that are still visible if rect.y + new_y < self.screen.get_height(): living_scissors.append(rect.move(new_x, new_y)) self.scissors = living_scissors def draw(self): for rect in self.scissors: self.screen.blit(self.img, rect) screen = pygame.display.set_mode((800, 600)) scissors = ScissorEmitter(screen) clock = pygame.time.Clock() running = True while running: for event in pygame.event.get(): if pygame.QUIT == event.type: running = False ticks = pygame.time.get_ticks() scissors.update(ticks) if scissors.should_spawn(ticks): scissors.spawn(ticks) screen.fill((100,100,100)) scissors.draw() pygame.display.flip() clock.tick(30)This isn't great code, but should hopefully be enough to help you figure out how to proceed. RE: rock paper scissors game - abscorpy - Jan-29-2019 im not followinga tutorial, though someone from this forum helped me with my game.... thanks for the support This is my code so far, but now i have a new issue, im trying to draw some scissors coming from top to bottom of screen(im trying to use a radom function as you can see in my code but im not getting any result) anyone can help me??? thanks a lot import pygame import random pygame.init() # Did code to python pep 8 style guide. # https://www.python.org/dev/peps/pep-0008/ class Player: @classmethod def load_images(cls): # convert image to pygame format will help with speed # convert_alpha() will not show invisable parts with pngs and jpg cls.images = [ pygame.image.load('napkin.png').convert_alpha(), pygame.image.load('napkin2.png').convert_alpha(), pygame.image.load('napkin3.png').convert_alpha()] def __init__(self, x, y, w, h): self.rect = pygame.Rect(x, y, w, h) self.velocity = 5 self.is_jumping = False self.jump_count = 5 self.walk_left = 0, 2 # self.walk_left == (0, 2) self.walk_right = 0, 1 self.walk_count = 0 self.walk_pos = 0 self.tick = 100 self.next_tick = 100 def can_move(self, ticks): if ticks > self.tick: self.tick += self.next_tick return True return False def draw(self, surface): surface.blit(Player.images[self.walk_pos], self.rect) def move_left(self): if self.rect.x > self.velocity: self.walk_count = (self.walk_count + 1) % len(self.walk_left) self.walk_pos = self.walk_left[self.walk_count] self.rect.x -= self.velocity def move_right(self, width): if self.rect.x < width - self.rect.width - self.velocity: self.walk_count = (self.walk_count + 1) % len(self.walk_right) self.walk_pos = self.walk_right[self.walk_count] self.rect.x += self.velocity class Scene: def __init__(self): # basic pygame setup pygame.display.set_caption('Napkin Move') self.rect = pygame.Rect(0, 0, 1364, 500) self.surface = pygame.display.set_mode(self.rect.size) self.clock = pygame.time.Clock() # Scene setup Player.load_images() self.background = pygame.image.load('bg1.png').convert_alpha() self.background = pygame.transform.scale(self.background, self.rect.size) self.player = Player(300, 410, 64, 64) def mainloop(self): self.running = True while self.running: for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_a: self.player.move_left() elif event.key == pygame.K_d: self.player.move_right(self.rect.width) ticks = pygame.time.get_ticks() keys = pygame.key.get_pressed() if self.player.can_move(ticks): if keys[pygame.K_LEFT]: self.player.move_left() elif keys[pygame.K_RIGHT]: self.player.move_right(self.rect.width) # drawing self.surface.blit(self.background, (0,0)) self.player.draw(self.surface) # draw code here pygame.display.flip() self.clock.tick(30) class scissors(object): def __init__(self, x, y, w, h): r = random.randint(10, 1200) print(r) if __name__ == '__main__': scene = Scene() scene.mainloop() pygame.quit()Traceback (most recent call last): File "E:\Users\Usuario1\Documents\abscorp\paper\napkin2.py", line 113, in <module> scene.mainloop() File "E:\Users\Usuario1\Documents\abscorp\paper\napkin2.py", line 102, in mainloop self.scissors.draw(self.surface) File "E:\Users\Usuario1\Documents\abscorp\paper\napkin2.py", line 58, in draw self.surface.blit(self.scissors.images[0], self.rect) AttributeError: 'Scissors' object has no attribute 'scissors' RE: rock paper scissors game - abscorpy - Jan-29-2019 This is the correct code: import pygame import random pygame.init() # Did code to python pep 8 style guide. # https://www.python.org/dev/peps/pep-0008/ class Player: @classmethod def load_images(cls): # convert image to pygame format will help with speed # convert_alpha() will not show invisable parts with pngs and jpg cls.images = [ pygame.image.load('napkin.png').convert_alpha(), pygame.image.load('napkin2.png').convert_alpha(), pygame.image.load('napkin3.png').convert_alpha()] def __init__(self, x, y, w, h): self.rect = pygame.Rect(x, y, w, h) self.velocity = 5 self.is_jumping = False self.jump_count = 5 self.walk_left = 0, 2 # self.walk_left == (0, 2) self.walk_right = 0, 1 self.walk_count = 0 self.walk_pos = 0 self.tick = 100 self.next_tick = 100 def can_move(self, ticks): if ticks > self.tick: self.tick += self.next_tick return True return False def draw(self, surface): surface.blit(Player.images[self.walk_pos], self.rect) def move_left(self): if self.rect.x > self.velocity: self.walk_count = (self.walk_count + 1) % len(self.walk_left) self.walk_pos = self.walk_left[self.walk_count] self.rect.x -= self.velocity def move_right(self, width): if self.rect.x < width - self.rect.width - self.velocity: self.walk_count = (self.walk_count + 1) % len(self.walk_right) self.walk_pos = self.walk_right[self.walk_count] self.rect.x += self.velocity class Scissors(object): def __init__(self, x, y, w, h): self.rect = pygame.Rect(x, y, w, h) self.velocity = 5 self.surface = pygame.display.set_mode(self.rect.size) def load_images(cls): cls_images=[pygame.image.load('scissors.png').convert_alpha()] def draw(self, surface): self.surface.blit(self.scissors.images[0], self.rect) r = random.randint(10, 1200) print(r) class Scene: def __init__(self): # basic pygame setup pygame.display.set_caption('Napkin Move') self.rect = pygame.Rect(0, 0, 1364, 500) self.surface = pygame.display.set_mode(self.rect.size) self.clock = pygame.time.Clock() # Scene setup Player.load_images() self.background = pygame.image.load('bg1.png').convert_alpha() self.background = pygame.transform.scale(self.background, self.rect.size) self.player = Player(300, 410, 64, 64) self.scissors = Scissors(10, 100, 64, 64) def mainloop(self): self.running = True while self.running: for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_a: self.player.move_left() elif event.key == pygame.K_d: self.player.move_right(self.rect.width) ticks = pygame.time.get_ticks() keys = pygame.key.get_pressed() if self.player.can_move(ticks): if keys[pygame.K_LEFT]: self.player.move_left() elif keys[pygame.K_RIGHT]: self.player.move_right(self.rect.width) # drawing self.surface.blit(self.background, (0,0)) self.player.draw(self.surface) self.scissors.draw(self.surface) # draw code here pygame.display.flip() self.clock.tick(30) if __name__ == '__main__': scene = Scene() scene.mainloop() pygame.quit()And this is the error message im getting: Traceback (most recent call last): File "E:\Users\Usuario1\Documents\abscorp\paper\napkin2.py", line 114, in <module> scene.mainloop() File "E:\Users\Usuario1\Documents\abscorp\paper\napkin2.py", line 103, in mainloop self.scissors.draw(self.surface) File "E:\Users\Usuario1\Documents\abscorp\paper\napkin2.py", line 59, in draw self.surface.blit(self.scissors.images[0], self.rect) AttributeError: 'Scissors' object has no attribute 'scissors' RE: rock paper scissors game - nilamo - Jan-29-2019 (Jan-29-2019, 04:43 PM)abscorpy Wrote:You're trying to blitdef draw(self, surface): self.surface.blit(self.scissors.images[0], self.rect) self.scissors.images[0] , but self.scissors doesn't exist.
RE: rock paper scissors game - Windspar - Jan-29-2019 python classes has class variables and object variables. They also have static, class, and object methods. class MyClass: image = 'image' # class variable # class method, passes the class. @classmethod def something(cls): # create a class variable cls.image2 = 'image 2' # initialize an object. def __init__(self): self.value = 1 # object variable # print class variables print(MyClass.image) print(MyClass.image2) # object my = MyClass() # print object variable. print(my.value )Don't forget to load image at line 74 or after Player.load_images() class Scissors: # load image once @classmethod def load_image(cls): cls.image = pygame.image.load('scissors.png').convert_alpha() def __init__(self): x = random.randint(10, 1200) self.rect = pygame.Rect(x, 20, 64, 64) self.velocity = 5 def draw(self, surface): surface.blit(Scissors.image, self.rect) RE: rock paper scissors game - abscorpy - Feb-11-2019 This is my code so far, im trying to make a rock/paper/scissors game in which you have to catch the rocks and you need to avoid the scissors Thanks for the support What i need to do is to make the som top to cissors appear fromo top to bottom and the also the rocks so that you can catch the rocks. thanks a lot see you soon import pygame import random pygame.init() # Did code to python pep 8 style guide. # https://www.python.org/dev/peps/pep-0008/ class Player: @classmethod def load_images(cls): # convert image to pygame format will help with speed # convert_alpha() will not show invisable parts with pngs and jpg cls.images = [ pygame.image.load('napkin.png').convert_alpha(), pygame.image.load('napkin2.png').convert_alpha(), pygame.image.load('napkin3.png').convert_alpha()] def __init__(self, x, y, w, h): self.rect = pygame.Rect(x, y, w, h) self.velocity = 5 self.is_jumping = False self.jump_count = 5 self.walk_left = 0, 2 # self.walk_left == (0, 2) self.walk_right = 0, 1 self.walk_count = 0 self.walk_pos = 0 self.tick = 100 self.next_tick = 100 def can_move(self, ticks): if ticks > self.tick: self.tick += self.next_tick return True return False def draw(self, surface): surface.blit(Player.images[self.walk_pos], self.rect) def move_left(self): if self.rect.x > self.velocity: self.walk_count = (self.walk_count + 1) % len(self.walk_left) self.walk_pos = self.walk_left[self.walk_count] self.rect.x -= self.velocity def move_right(self, width): if self.rect.x < width - self.rect.width - self.velocity: self.walk_count = (self.walk_count + 1) % len(self.walk_right) self.walk_pos = self.walk_right[self.walk_count] self.rect.x += self.velocity class Scissors(pygame.sprite.Group): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = 'scissors.png' self.rect = self.image.get_rect() self.rect.x = random.randrange(WIDTH - self.rect.width) self.rect.y = random.randrange(-100, -40) self.speedy = random.randrange(1, 8) self.speedx = random.randrange(-3, 3) def update(self): self.rect.y += self.speedy if self.rect.top > HEIGHT + 10: self.rect.x = random.randrange(WIDTH - self.rect.width) self.rect.y = random.randrange(-100, -40) self.speedy = random.randrange(1, 8) mobs = pygame.sprite.Group() for i in range(8): m=Scissors() all_sprites.add(m) mobs.add(m) class Scene: def __init__(self): # basic pygame setup pygame.display.set_caption('Napkin Move') self.rect = pygame.Rect(0, 0, 1364, 500) self.surface = pygame.display.set_mode(self.rect.size) self.clock = pygame.time.Clock() # Scene setup Player.load_images() self.background = pygame.image.load('bg1.png').convert_alpha() self.background = pygame.transform.scale(self.background, self.rect.size) self.player = Player(300, 410, 64, 64) self.scissors = Scissors(10, 100, 64, 64) def mainloop(self): self.running = True while self.running: for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_a: self.player.move_left() elif event.key == pygame.K_d: self.player.move_right(self.rect.width) ticks = pygame.time.get_ticks() keys = pygame.key.get_pressed() if self.player.can_move(ticks): if keys[pygame.K_LEFT]: self.player.move_left() elif keys[pygame.K_RIGHT]: self.player.move_right(self.rect.width) # drawing self.surface.blit(self.background, (0,0)) self.player.draw(self.surface) self.scissors.draw(self.surface) # draw code here pygame.display.flip() self.clock.tick(30) if __name__ == '__main__': scene = Scene() scene.mainloop() pygame.quit() RE: rock paper scissors game - nilamo - Feb-11-2019 That code is already there, in the Scissors.update() method. You just need to call it. RE: rock paper scissors game - abscorpy - Feb-14-2019 Andreas how do i call it? In the mainloop or after i call mainloop before quitting pygame? Thanks forma the support |