Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rock paper scissors game
#7
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()

Attached Files

Thumbnail(s)
       
Image(s)
           
Reply


Messages In This Thread
rock paper scissors game - by abscorpy - Jan-28-2019, 03:52 PM
RE: rock paper scissors game - by nilamo - Jan-28-2019, 07:20 PM
RE: rock paper scissors game - by abscorpy - Jan-29-2019, 03:35 PM
RE: rock paper scissors game - by abscorpy - Jan-29-2019, 04:43 PM
RE: rock paper scissors game - by nilamo - Jan-29-2019, 05:47 PM
RE: rock paper scissors game - by Windspar - Jan-29-2019, 11:49 PM
RE: rock paper scissors game - by abscorpy - Feb-11-2019, 01:22 AM
RE: rock paper scissors game - by nilamo - Feb-11-2019, 06:51 PM
RE: rock paper scissors game - by abscorpy - Feb-14-2019, 07:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Rock/Paper/Scissors Game 1 abscorpy 6 3,825 Dec-01-2020, 10:08 PM
Last Post: metulburr
  randomization code in rock, paper, scissors cel 4 2,708 May-28-2020, 08:48 PM
Last Post: cel
  Rock Paper Scissors Game, I need help. extraguac 2 2,559 Feb-14-2019, 04:34 AM
Last Post: extraguac
  [PyGame] Rock,paper,scissors Game(beginner) r0d 10 8,837 Jan-16-2018, 08:01 PM
Last Post: r0d

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020