Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rock paper scissors game
#1
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pygame
pygame.init()
  
  
# Did code to python pep 8 style guide.
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()

Attached Files

Thumbnail(s)
       
Image(s)
           
Reply
#2
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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.
Reply
#3
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import pygame
import random
pygame.init()
 
  
  
# Did code to python pep 8 style guide.
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'
Reply
#4
This is the correct code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import pygame
import random
pygame.init()
  
  
# Did code to python pep 8 style guide.
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'

Attached Files

Thumbnail(s)
   
Reply
#5
(Jan-29-2019, 04:43 PM)abscorpy Wrote:
1
2
def draw(self, surface):
    self.surface.blit(self.scissors.images[0], self.rect)
You're trying to blit self.scissors.images[0], but self.scissors doesn't exist.
Reply
#6
python classes has class variables and object variables. They also have static, class, and object methods.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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()
1
2
3
4
5
6
7
8
9
10
11
12
13
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)
99 percent of computer problems exists between chair and keyboard.
Reply
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import pygame
import random
pygame.init()
  
  
# Did code to python pep 8 style guide.
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
#8
That code is already there, in the Scissors.update() method. You just need to call it.
Reply
#9
Andreas how do i call it?
In the mainloop or after i call mainloop before quitting pygame?


Thanks forma the support
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rock/Paper/Scissors Game 1 abscorpy 6 5,205 Dec-01-2020, 10:08 PM
Last Post: metulburr
  Rock Paper Scissors Game, I need help. extraguac 2 3,353 Feb-14-2019, 04:34 AM
Last Post: extraguac
  [PyGame] Rock,paper,scissors Game(beginner) r0d 10 10,962 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