Can't seem to select multiple circles at the same time - pygame
I'm working on this game called abalone and I can't seem to select multiple of the coins at the same time. If I select one coin and click on another the selection resets and only selects the latest coin selected.
I've attached my code below.
import pygame
from pygame.locals import *
import sys
import random
import math
pygame.init()
win=pygame.display.set_mode((1000,600))
colorname=(128,128,128)
display_width=1000
display_height=600
red=(255,0,0)
blue=(0,0,255)
green=(0,255,0)
pygame.display.set_caption(("Abalone"))
pygame.draw.polygon(win,(0,0,0),[(650,0),(1000,0),(1000,600),(600,600)],0)
pygame.display.update()
def abalone_board():
pygame.draw.polygon(win, (165,42,42),[(150,20),(450,20),(600,300),(450,580),(150,580),(0,300)],0)
pygame.draw.polygon(win, (0,0,0),[(150,20),(450,20),(600,300),(450,580),(150,580),(0,300)],5)
for i in range(172,434,64):
pygame.draw.circle(win,(237, 187, 153),(i, 60),30,0)
for i in range(140,499,64):
pygame.draw.circle(win,(237, 187, 153),(i, 120),30,0)
for i in range(110,499,64):
pygame.draw.circle(win,(237, 187, 153),(i, 180),30,0)
for i in range(80,563,64):
pygame.draw.circle(win,(237, 187, 153),(i, 240),30,0)
for i in range(50,563,64):
pygame.draw.circle(win,(237, 187, 153),(i, 300),30,0)
for i in range(80,563,64):
pygame.draw.circle(win,(237, 187, 153),(i, 360),30,0)
for i in range(110,499,64):
pygame.draw.circle(win,(237, 187, 153),(i, 420),30,0)
for i in range(140,499,64):
pygame.draw.circle(win,(237, 187, 153),(i, 480),30,0)
for i in range(172,434,64):
pygame.draw.circle(win,(237, 187, 153),(i, 540),30,0)
for i in range(172,434,64):
pygame.draw.circle(win,(0,0,0),(i, 60),30,2)
for i in range(140,499,64):
pygame.draw.circle(win,(0,0,0),(i, 120),30,2)
for i in range(110,499,64):
pygame.draw.circle(win,(0,0,0),(i, 180),30,2)
for i in range(80,563,64):
pygame.draw.circle(win,(0,0,0),(i, 240),30,2)
for i in range(50,563,64):
pygame.draw.circle(win,(0,0,0),(i, 300),30,2)
for i in range(80,563,64):
pygame.draw.circle(win,(0,0,0),(i, 360),30,2)
for i in range(110,499,64):
pygame.draw.circle(win,(0,0,0),(i, 420),30,2)
for i in range(140,499,64):
pygame.draw.circle(win,(0,0,0),(i, 480),30,2)
for i in range(172,434,64):
pygame.draw.circle(win,(0,0,0),(i, 540),30,2)
return abalone_board
def leader_board():
pygame.draw.polygon(win,(192,192,192),[(650,0),(1000,0),(1000,600),(650,600)],0)
pygame.draw.polygon(win,(0,0,0),[(650,5),(995,5),(995,595),(650,595)],10)
return leader_board
def marble_red():
pygame.draw.circle(win,(255,0,0),(172, 60),23,0)
return marble_red
def ini_pos_red():
r1=pygame.draw.circle(win,(255,0,0),(428,540),23,0)
r2=pygame.draw.circle(win,(255,0,0),(364,540),23,0)
r3=pygame.draw.circle(win,(255,0,0),(300,540),23,0)
r4=pygame.draw.circle(win,(255,0,0),(236,540),23,0)
r5=pygame.draw.circle(win,(255,0,0),(172,540),23,0)
r6=pygame.draw.circle(win,(255,0,0),(460,480),23,0)
r7=pygame.draw.circle(win,(255,0,0),(396,480),23,0)
r8=pygame.draw.circle(win,(255,0,0),(332,480),23,0)
r9=pygame.draw.circle(win,(255,0,0),(268,480),23,0)
r10=pygame.draw.circle(win,(255,0,0),(204,480),23,0)
r11=pygame.draw.circle(win,(255,0,0),(140,480),23,0)
r12=pygame.draw.circle(win,(255,0,0),(238,420),23,0)
r13=pygame.draw.circle(win,(255,0,0),(302,420),23,0)
r14=pygame.draw.circle(win,(255,0,0),(366,420),23,0)
return ini_pos_red
def ini_pos_blue():
b1=pygame.draw.circle(win,(0,0,255),(428,60),23,0)
b2=pygame.draw.circle(win,(0,0,255),(364,60),23,0)
b3=pygame.draw.circle(win,(0,0,255),(300,60),23,0)
b4=pygame.draw.circle(win,(0,0,255),(236,60),23,0)
b5=pygame.draw.circle(win,(0,0,255),(172,60),23,0)
b6=pygame.draw.circle(win,(0,0,255),(460,120),23,0)
b7=pygame.draw.circle(win,(0,0,255),(396,120),23,0)
b8=pygame.draw.circle(win,(0,0,255),(332,120),23,0)
b9=pygame.draw.circle(win,(0,0,255),(268,120),23,0)
b10=pygame.draw.circle(win,(0,0,255),(204,120),23,0)
b11=pygame.draw.circle(win,(0,0,255),(140,120),23,0)
b12=pygame.draw.circle(win,(0,0,255),(238,180),23,0)
b13=pygame.draw.circle(win,(0,0,255),(302,180),23,0)
b14=pygame.draw.circle(win,(0,0,255),(366,180),23,0)
return ini_pos_blue
def text_object(text,color,size):
if size=='small':
textSurface=smallfont.render(text,True,color)
if size=='medium':
textSurface=medfont.render(text,True,color)
if size=='large':
textSurface=largefont.render(text,True,color)
return textSurface, textSurface.get_rect()
def message_to_screen(msg,color,y_displace=0,size='small'):
textSurf,textRect=text_object(msg,color,size)
textRect.center=(display_width/2),(display_height/2)+y_displace
win.blit(textSurf,textRect)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_c:
intro = False
if event.key==pygame.K_q:
pygame.quit()
sys.exit(0)
quit()
win.fill((255,255,255))
message_to_screen("Welcome to Abalone",red,-100,"large")
message_to_screen("The Objective of this game is to push your opponents marbles outside",blue,-30)
message_to_screen("At a trun , you can either move one , two or three marbles",blue,10)
message_to_screen("you can push your opponent's marbles that are in a line ",blue,50)
message_to_screen("to their own with an in-line move only.you can only push ",blue,90)
message_to_screen("if the pushing line has more marbles than the pushed line ",blue,130)
message_to_screen("Press C to play and Q to exit ",green,200)
pygame.display.update()
x1=random.randint(0,61)
marble_red()
position=[(172,60),(236,60),(300,60),(364,60),(428,60),(140,120),(204,120),(268,120),(332,120),
(396,120),(460,120),(110,180),(174,180),(238,180),(302,180),(366,180),(430,180),
(494,180),(80,240),(144,240),(208,240),(272,240),(336,240),(400,240),(464,240),
(528,240),(50,300),(114,300),(178,300),(242,300),(306,300),(370,300),(434,300),
(498,300),(562,300),(80,360),(144,360),(208,360),(272,360),(336,360),(400,360),
(464,360),(528,360),(110,420),(174,420),(238,420),(302,420),(366,420),(430,420),
(494,420),(140,480),(204,480),(268,480),(332,480),(396,480),(460,480),(172,540),
(236,540),(300,540),(300,540),(364,540),(428,540)]
smallfont = pygame.font.SysFont("comicsansms", 25)
medfont = pygame.font.SysFont("comicsansms", 50)
largefont = pygame.font.SysFont("comicsansms", 80)
font = pygame.font.SysFont("comicsansms", 68)
text = font.render("ABALONE", True, (0, 0, 0))
grid=[
[1,1,1,1,1],
[1,1,1,1,1,1],
[0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,2,2,2,0,0],
[2,2,2,2,2,2],
[2,2,2,2,2]
]
class circle:
def __init__(self, pos, colour):
self.pos = pos
self.colour = colour
self.radius = 23
def circle_clicked_on(pos, circles):
for c in circles:
if distance(c.pos, pos) < c.radius:
return c
return False
def distance(p1,p2):
return math.sqrt(((p1[0] - p2[0])**2) + ((p1[1] - p2[1])**2))
circles=[]
for p in position:
circles.append(circle(p, red))
posn=pygame.mouse.get_pos()
def gameloop():
red=(255,0,0)
run=True
current_circle = False
while run:
pygame.time.delay(250)
ms=pygame.mouse.get_pressed()
for event in pygame.event.get():
if event.type==pygame.QUIT:
run=False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
run = False
elif event.type==pygame.MOUSEBUTTONDOWN :
current_circle = circle_clicked_on(pygame.mouse.get_pos(), circles)
keys=pygame.key.get_pressed()
win.fill((colorname))
abalone_board()
leader_board()
ini_pos_red()
ini_pos_blue()
if current_circle and win.get_at(current_circle.pos)==red :
pygame.draw.circle(win,(255,255,255),current_circle.pos,23,5)
win.blit(text, (659,0))
pygame.display.update()
pygame.quit()
sys.exit(0)
game_intro()
gameloop()
You store the selected coin in the variable current_circle, and every time the player clicks on a coin the variable is overwritten.
You could use a list instead, something like this:
...
run=True
clicked_circles = [] # use a list here
while run:
...
elif event.type==pygame.MOUSEBUTTONDOWN :
# store clicked coins in a list
current_circle = circle_clicked_on(pygame.mouse.get_pos(), circles)
if current_circle and win.get_at(current_circle.pos)==red:
clicked_circles.append(current_circle)
keys=pygame.key.get_pressed()
win.fill((colorname))
abalone_board()
leader_board()
ini_pos_red()
ini_pos_blue()
for current_circle in clicked_circles: # iterate over list
pygame.draw.circle(win,(255,255,255),current_circle.pos,23,5)
...
Related
User input recorded from multiple text boxes and put into MySQL
I'm running into a brick wall and have spent multiple hours trying to research how to do this and I'm at the point I'm not sure if I'm asking the right question. I have a page for recording user input in multiple boxes. I've made a separate file for my text box function which feeds into the main page. I want to be able to click next and record all the use input from the text boxes into a MySQL database. The problem I'm running into seems to be that the main page doesn't know there's user input just that there is a text box, so I can't record anything, while if I change the text input file it defeats the purpose of creating one so that I can use text boxes anywhere. Main page import pygame import Test import button import sys import mysql.connector pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Main Page") icon = pygame.image.load('icons8-robber-32.png') pygame.display.set_icon(icon) colour_active = pygame.Color('orange') colour_inactive = pygame.Color('Yellow') font = pygame.font.Font(None, 32) start_image = pygame.image.load('play.png').convert_alpha() exit_image = pygame.image.load('exit.png').convert_alpha() menu_image = pygame.image.load('menu.png').convert_alpha() back_image = pygame.image.load('back.png').convert_alpha() next_image = pygame.image.load('next.png').convert_alpha() def register(): input_box1 = Test.inputBox(210, 105, 140, 32, "First Name: ") input_box2 = Test.inputBox(210, 160, 140, 32) input_boxes = [input_box1, input_box2] back_button = button.Button(100, 450, back_image, 0.2) next_button = button.Button(450, 450, next_image, 0.2) done = False while not done: for event in pygame.event.get(): for box in input_boxes: box.handle_event(event) for box in input_boxes: box.update() screen.fill((30, 30, 30)) for box in input_boxes: box.draw(screen) if back_button.draw(screen): print("no") if next_button.draw(screen): sys.exit() pygame.display.update() start_button = button.Button(100, 150, start_image, 0.2) exit_button = button.Button(250, 150, exit_image, 0.2) menu_button = button.Button(400, 150, menu_image, 0.2) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((150, 255, 100)) if start_button.draw(screen): print("no") if exit_button.draw(screen): sys.exit() if menu_button.draw(screen): register() pygame.display.update() pygame.QUIT() sys.exit() Text box file import pygame pygame.init() colour_active = pygame.Color('orange') colour_inactive = pygame.Color('Yellow') font = pygame.font.Font(None, 32) class inputBox(): def __init__(self, x, y, w, h, text=''): self.rect = pygame.Rect(x,y,w,h) self.color = colour_inactive self.text = text self.text_surface = font.render(text, True, self.color) self.active = False def handle_event(self,event): if event.type == pygame.MOUSEBUTTONDOWN: if self.rect.collidepoint(event.pos): self.active = not self.active else: self.active = False self.color = colour_active if self.active else colour_inactive if event.type == pygame.KEYDOWN: if self.active: if event.key == pygame.K_RETURN: print(self.text) elif event.key == pygame.K_BACKSPACE: self.text = self.text [:-1] else: self.text += event.unicode self.text_surface = font.render(self.text, True, self.color) def update(self): self.rect.w = max(100, self.text_surface.get_width() + 10) def draw(self, screen): screen.blit(self.text_surface, (self.rect.x+5, self.rect.y+5)) pygame.draw.rect(screen, self.color, self.rect, 2)
You do not need the self.text file in the main code. So instead of writing input_box1.self.text, it was just input_box1.text.
pygame.error: video system not initialized (Alien Invasion)
I'm currently following a python tutorial on a book and have finally coming to creating a pygame. After manually typing each code, The Game (Alien Invasion) import sys import pygame from settings import Settings from ship import Ship def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") ship = Ship(screen) bg_color = (230, 230, 230) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(ai_settings.bg_color) ship.blitme() pygame.display.flip() run_game() Settings: class Settings(): def __init__(self): self.screen_width = 1200 self.screen_height = 800 self.bg_color = (230, 230, 230) Ship: import pygame class Ship(): def __init__(self, screen): self.screen = screen self.image = pygame.image.load('images/ship.bmp') self.rect = self.image.get_rect() self.screen_rect = screen.get_rect() self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom def blitme(self): self.screen.blit(self.image, self.rect) I keep getting this error Traceback (most recent call last): File "/Users/ff/Desktop/alien invasion/alien_invasion.py", line 21, in <module> for event in pygame.event.get(): pygame.error: video system not initialized I've made sure each line and indentation is properly inserted. Does anyone know how to fix this? -----edit----- The pygame initialization error is fixed after I moved pygame.init() outside the while loop, but now it says Traceback (most recent call last): File "/Users/ff/Desktop/alien invasion/alien_invasion.py", line 27, in <module> screen.fill(ai_settings.bg_color) NameError: name 'screen' is not defined
Your function run_game() is called after your while loop so your pygame modules have not been initialized before you start looking for events. Try moving it before the while loop. Edit: If you want to access screen, ai_settings, and ship outside of your function run_game() then you will need to edit your code like this: def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") ship = Ship(screen) return screen, ai_settings, ship bg_color = (230, 230, 230) screen, ai_settings, ship = run_game() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(ai_settings.bg_color) ship.blitme() pygame.display.flip() Notable parts are the lines return screen, ai_settings, ship and screen, ai_settings, ship = run_game()
TypeError: Rect argument is invalid pygame
i am new to pygame and wanted to make a game i found a playlist online at https://www.youtube.com/playlistlist=PLQVvvaa0QuDdLkP8MrOXLe_rKuf6r80KO t i followed it but i got a error like this Traceback (most recent call last): File "/home/pi/firstgamepyg.py", line 78, in <module> game_loop() File "/home/pi/firstgamepyg.py", line 65, in game_loop things(thing_startx, thing_starty, thing_width, thing_height, black) File "/home/pi/firstgamepyg.py", line 15, in things pygame.draw.rect(gamedisplay, color,(thingx, thingy, thingw, thingh)) TypeError: Rect argument is invalid this is my code import pygame import time import random pygame.init() dw = 800 dh = 600 black = (0,0,0) white = (255,255,255) red = (255,0,0) cw = 96 gamedisplay = pygame.display.set_mode((dw,dh)) cimg = pygame.image.load('/home/pi/Downloads/car.jpeg') def things(thingx, thingy, thingw, color, thingh): #error on next line pygame.draw.rect(gamedisplay, color,(thingx, thingy, thingw, thingh)) def car(x,y): gamedisplay.blit(cimg,(x,y)) def text_objects(text, font): textsurface = font.render(text, True, red) return textsurface, textsurface.get_rect() def message_display(text): largeText = pygame.font.Font('freesansbold.ttf',115) TextSurf, TextRect = text_objects(text, largeText) TextRect.center = ((dw/2),(dh/2)) gamedisplay.blit(TextSurf, TextRect) pygame.display.update() time.sleep(2) game_loop() def crash(): message_display('wasted') def game_loop(): x = (dw * 0.45) y = (dh * 0.65) x_change = 0 thing_startx = random.randrange(0,800) thing_starty = -600 thing_speed = 7 thing_width = 100 thing_height = 100 pygame.display.set_caption('A bit Racey') clock = pygame.time.Clock() gameexit = False while not gameexit: for event in pygame.event.get(): if event.type == pygame.QUIT: gameexit = True pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x_change = -5 elif event.key == pygame.K_RIGHT: x_change = 5 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key==pygame.K_RIGHT: x_change = 0 x += x_change gamedisplay.fill(white) things(thing_startx, thing_starty, thing_width, thing_height, black) thing_starty += thing_speed car(x,y) if x > dw - cw or x < 0: crash() gameexit = True pygame.display.update() clock.tick(100) game_loop() pygame.quit() quit() i was trying to make a game where you are a car and avoid blocks but i cant add the blocks because i get an error
your things function is formatted incorrectly. you have the parameter list as (thingx, thingy, thingw, color, thingh) but then you call it as (thingx, thingy, things, thingh, color). to make the errors go away all that you need to do is in the function definition switch color with thingh
Pygame: Creating bullets - 'Bullet' object is not callable
I am trying to have pygame create a new Bullet object every time the mouse is pressed. It works on the first bullet, but on the second Bullet I am getting a "'Bullet' object is not callable' error. class Bullet(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((20,40)) self.image.fill(black) self.rect = self.image.get_rect() self.rect.x = player_1.rect.x self.rect.y = player_1.rect.y #self.image = pygame.image.load("C:/Users/Thomas/Desktop/rocket.png") self.width = self.image.get_width() self.height = self.image.get_height() self.speed = 20 self.damage_radius = 0 self.damage = 0 self.angle = 0 def draw(self): self.image.fill(black) screen.blit(self.image, (self.rect.x, self.rect.y)) def delete(self): pass def fire(self, mouse): """Determine agnle of which rocket is fired""" pass def update(self): self.rect.y -= self.speed def game_intro(): intro = True while intro: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() screen.fill(intro_bg_color) large_text = pygame.font.Font("C:/Users/Thomas/Desktop/Python Sketches/Fonts/Quesat Black Demo.OTF", 100) player_1 = Player((100,100), 20, blue) while True: pygame.display.update() clock.tick(FPS) mouse = pygame.mouse.get_pos() gameEvent = pygame.event.get() """Draw""" """This wil be for advancing bullets in playing field""" screen.fill(bg_color) player_1.draw(mouse) for Bullet in bullet_list: Bullet.update() if Bullet.rect.y < 0: bullet_list.remove(Bullet) Bullet.draw() for event in gameEvent: if event.type == pygame.QUIT: pygame.quit() elif event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]: mouse_is_pressed = True # Used to determine if the mouse is held or not print("Mouse pressed") """Generate new bullet""" bullet = Bullet() all_sprites_list.add(bullet) bullet_list.add(bullet) bullet.rect.x = player_1.rect.x bullet.rect.y = player_1.rect.y elif event.type == pygame.MOUSEBUTTONUP and pygame.mouse.get_rel()[0]: mouse_is_pressed = False print("Mouse button released") When mouse is pressed, generate new Bullet, add it to bullet_list. This is down how an example I looked at is done and I just can't solve the issues. Help is greatly appreciated!
I took this block of code: for Bullet in bullet_list: Bullet.update() Bullet.draw() and change the "Bullet" to "bullet" and the code finally started working. for bullet in bullet_list: bullet.update() bullet.draw()
How can I make the triangle in this program rotate?
Please be very specific , because it`s my first time working in pygame :D #!/usr/bin/env python import pygame pygame.init() #colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) PI = 3.141592653 size = (400, 500) screen = pygame.display.set_mode(size) pygame.display.set_caption("Professor Craven's Cool Game") done = False clock = pygame.time.Clock() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True screen.fill(WHITE) pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5) font = pygame.font.SysFont('Calibri', 25, True, False) text = font.render("Text", True, BLACK) screen.blit(text, [250, 250]) pygame.display.flip() Yes , if I have an error in this code also please tell me . I am open to learn new things about pygame . clock.tick(60) pygame.quit()
Try: pygame.transform.rotate() to rotate a pygame.Surface